Copy information based on criteria using VBA

alex_a

Board Regular
Joined
Feb 27, 2012
Messages
51
Greetings,

I have the follwoing code developed:
For i = 35 To rcnt
If LCase(Range("D" & i).Value) = "no" Or LCase(Range("D" & i).Value) = "no-rq" Then
Range("A" & i).Copy (Sheets("1").Range("B" & j))
Range("C" & i).Copy (Sheets("1").Range("C" & j))
Range("F" & i).Copy (Sheets("1").Range("D" & j))
j = j + 1
End If
Next
End Sub
This code transfer infromation from one sheet to a sheet called "sheet 1" based on "no" answers. I am trying to transfer infromation to another sheet, called "sheet 2" if there is a "yes" answer. Your help is greatly appreciated.
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Code:
[COLOR=#333333]For i = 35 To rcnt[/COLOR]
[COLOR=#333333]     If LCase(Range("D" & i).Value) = "no" Or LCase(Range("D" & i).Value) = "no-rq" Then[/COLOR]
[COLOR=#333333] [/COLOR][COLOR=#333333]           Sheets("1").Range("B" & j).value = [/COLOR][COLOR=#333333]Range("A" & i).value[/COLOR]
[COLOR=#333333] [/COLOR][COLOR=#333333]           Sheets("1").Range("C" & j).value = [/COLOR][COLOR=#333333]Range("C" & i).value[/COLOR]
[COLOR=#333333]            Sheets("1").Range("D" & j).value = [/COLOR][COLOR=#333333]Range("F" & i).value[/COLOR]
[COLOR=#333333]            j = j + 1[/COLOR]
[COLOR=#333333]     Else if [/COLOR][COLOR=#333333]LCase(Range("D" & i).Value) = "yes"
[/COLOR][COLOR=#333333] [/COLOR][COLOR=#333333]           Sheets("2").Range("B" & j).value = [/COLOR][COLOR=#333333]Range("A" & i).value[/COLOR]
[COLOR=#333333] [/COLOR][COLOR=#333333]           Sheets("2").Range("C" & j).value = [/COLOR][COLOR=#333333]Range("C" & i).value[/COLOR]
[COLOR=#333333]            Sheets("2").Range("D" & j).value = [/COLOR][COLOR=#333333]Range("F" & i).value[/COLOR]
[COLOR=#333333]     End If[/COLOR]
[COLOR=#333333]Next[/COLOR]
[COLOR=#333333]End Sub[/COLOR]

Copy paste is slow. Here you can just set the values of one range to the other. Really fast.
 
Upvote 0
which line gives the error?
You can step through your code by placing the cursor in the sub, and pressing F8 as an alternative to see how things do.
 
Upvote 0
Can you post your full code please? (use code tags around your code please)

Also indicate which line is highlighted by the vba editor when the syntax error is shown.
 
Upvote 0
Try changing this line:

Rich (BB code):
Else if LCase(Range("D" & i).Value) = "yes"

to this

Rich (BB code):
ElseIf LCase(Range("D" & i).Value) = "yes" Then

and see if that helps

Dave
 
Upvote 0
Try changing this line:

Rich (BB code):
Else if LCase(Range("D" & i).Value) = "yes"

to this

Rich (BB code):
ElseIf LCase(Range("D" & i).Value) = "yes" Then

and see if that helps

Dave

Thanks DAVE. It worked! Problem Solved!:)
 
Upvote 0
Ah, sorry for not spotting that. Good thing there are others with sharper eyes...
 
Upvote 0

Forum statistics

Threads
1,214,919
Messages
6,122,260
Members
449,075
Latest member
staticfluids

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top