syntax for ranges in macro

jrnyman

Board Regular
Joined
Mar 10, 2002
Messages
105
Can anyone help me with the syntax here?

Sub Extract()
SpreadsRow = 2
For DataRow = 2 To LastRow 'defined earlier
Set ADataRow = Sheets("Data").Range("J" & DataRow)
Set BDataRow = Sheets("Data").Range("J" & DataRow)
Set JDataRow = Sheets("Data").Range("J" & DataRow)
Set ASpreadsRow = Sheets("Spreads").Range("A" & SpreadsRow)
Set BSpreadsRow = Sheets("Spreads").Range("C" & SpreadsRow)
Set CSpreadsRow = Sheets("Spreads").Range("C" & SpreadsRow)
Set KDataRow = Sheets("Data").Range("J" & DataRow)


If JDataRow.Value = "Average" Then
If ASpreadsRow.Value = "" Then
Range("BDataRow").Select
Selection.Copy
Range(ASpreadsRow).Select
Selection.Paste
End If
If ASpreadsRow = BDataRow Then
If ADataRow.Value = "L" Then
Range(KDataRow).Select
Selection.Copy
Range(CSpreadsRow).Select
Selection.Paste
End If
If ADataRow.Value = "B" Then
Range(KDataRow).Select
Selection.Copy
Range(BSpreadsRow).Select
Selection.Paste
End If
SpreadsRow = SpreadsRow + 1
End If
End If
Next DataRow

End Sub
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
What's your problem?

I'm assuming from your variable names that there might be a problem with these lines of code:

<pre>
Set ADataRow = Sheets("Data").Range("J" & DataRow)
Set BDataRow = Sheets("Data").Range("J" & DataRow)
Set JDataRow = Sheets("Data").Range("J" & DataRow)
Set ASpreadsRow = Sheets("Spreads").Range("A" & SpreadsRow)
Set BSpreadsRow = Sheets("Spreads").Range("C" & SpreadsRow)
Set CSpreadsRow = Sheets("Spreads").Range("C" & SpreadsRow)
Set KDataRow = Sheets("Data").Range("J" & DataRow) </pre>

If you were wanting to set "ADataRow", "BDataRow" etc to actually be in those columns (A, B etc) then you'd better changed the "J" in each of these to be "Range("A" & DataRow), etc...
 
Upvote 0

Forum statistics

Threads
1,213,536
Messages
6,114,215
Members
448,554
Latest member
Gleisner2

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