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

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN

sijpie

Well-known Member
Joined
Nov 1, 2008
Messages
3,980
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
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.
 

alex_a

Board Regular
Joined
Feb 27, 2012
Messages
51
Sorry. I get an error message. Something wrong with the code.
 

sijpie

Well-known Member
Joined
Nov 1, 2008
Messages
3,980
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
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.
 

alex_a

Board Regular
Joined
Feb 27, 2012
Messages
51

ADVERTISEMENT

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.

It gives me a syntax error.
 

sijpie

Well-known Member
Joined
Nov 1, 2008
Messages
3,980
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
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.
 

dmt32

Well-known Member
Joined
Jul 3, 2012
Messages
7,345
Office Version
  1. 2019
Platform
  1. Windows

ADVERTISEMENT

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
 

alex_a

Board Regular
Joined
Feb 27, 2012
Messages
51
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!:)
 

sijpie

Well-known Member
Joined
Nov 1, 2008
Messages
3,980
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
Ah, sorry for not spotting that. Good thing there are others with sharper eyes...
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,164,489
Messages
5,837,634
Members
430,506
Latest member
TonyIbbs

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
Top