VBA code for a IF Function

Closed
Report
-
 Mukesh -
Hello,

I need help on the following:

Selecting a range on data e.g A1:A14
Searching for a certain value e.g 3000
if query is true then copy a value on the same row and paste to another cell e.g: 3000 is located in cell A5, copy values in D5 and paste into F5. (All the copying and pasting must happen on the same row as the value)

If query is false then put the value 0 in F5

i need this query to work with several different values e.g 3021, 3041...

Can anyone help with this?

Thanks

PMI

4 replies

I cant get the If function to do what I want. Everytime I put a range into the code, it comes up with a error. If I work with a single cell then the below works.

If Range("B2") = 3401 Then "ERROR ON THIS LINE WHEN I PUT A RANGE IN B2:B23"
Range("C2").Select
Selection.Copy
Range("D2").Select
ActiveSheet.Paste
Range("D2").Select
EndIf
1
Thank you

A few words of thanks would be greatly appreciated. Add comment

CCM 2821 users have said thank you to us this month

after IF and Before EndIf
i.e. where you are typing your macro give space, I mean tab
If Range("B2") = 3401 Then
Range("C2").Select
Selection.Copy
Range("D2").Select
ActiveSheet.Paste
Range("D2").Select
EndIf

this is working I checked myself
Hi PM

Why do you want to use a vba code, when the IF function works perfectly???
Hi

i need to add it as part of a macro that already exists. I just cant get it to work, thats why im asking.
Record a macro and input the function.
Then look at the code using VBA.

If your macro doesn't work, copy-paste it into your next reply.