VBA to loop and copy

Ody

Board Regular
Joined
Oct 14, 2010
Messages
215
Hello all,

Can someone assist me in creating a macro that loops through all my worksheets (not a static number) and pulls data from cell BY36 (from all those worksheets) onto my master sheet? It would be helpful if it also listed the sheet name next to that cell containing the data from BY36.

Thanks for any assistance and saving me from doing this by cut/paste. :)
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Untested, but try this
Code:
Sub Copy()
Dim ws As Worksheet
Dim M As Worksheet
Dim LR As Long
Set M = Sheets("Master")
For i = 1 To Sheets.Count
LR = M.Cells(Rows.Count, "A").End(xlUp).Row
If ws(i).Name <> "Master" 
    Then ws(i).Range("BY36").Copy M.Cells(LR + 1, "A")
    M.Cells(LR + 1, "B") = ws(i).Name
End If
Next i
End Sub

lenze
 
Upvote 0
Hi Lenze,

The code you provided didn't work. I received an error stating the following "End If without Block If"
Thanks
 
Last edited:
Upvote 0
Possibly it is supposed to be:
Code:
Sub Copy()
Dim ws As Worksheet
Dim M As Worksheet
Dim LR As Long
Set M = Sheets("Master")
For i = 1 To Sheets.Count
LR = M.Cells(Rows.Count, "A").End(xlUp).Row
If ws(i).Name <> "Master" Then
    ws(i).Range("BY36").Copy M.Cells(LR + 1, "A")
    M.Cells(LR + 1, "B") = ws(i).Name
End If
Next i
End Sub

Just a guess though.

Or you should be able to write it:

Code:
Sub Copy()
Dim ws As Worksheet
Dim M As Worksheet
Dim LR As Long
Set M = Sheets("Master")
For i = 1 To Sheets.Count
LR = M.Cells(Rows.Count, "A").End(xlUp).Row
If ws(i).Name <> "Master" Then ws(i).Range("BY36").Copy M.Cells(LR + 1, "A")M.Cells(LR + 1, "B") = ws(i).Name
Next i
End Sub
 
Upvote 0
Thanks for the reply klarowe,

Sadly, I'm still getting a 'run time error 91: Object variable or With Block variable not set' using the following:

Sub Copy()
Dim ws As Worksheet
Dim M As Worksheet
Dim LR As Long
Set M = Sheets("Summary")
For i = 1 To Sheets.Count
LR = M.Cells(Rows.Count, "A").End(xlUp).Row
If ws(i).Name <> "Summary" Then
ws(i).Range("BY36").Copy M.Cells(LR + 1, "A")
M.Cells(LR + 1, "B") = ws(i).Name
End If
Next i
End Sub

Any thoughts?
 
Upvote 0

Forum statistics

Threads
1,224,524
Messages
6,179,308
Members
452,904
Latest member
CodeMasterX

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