Worksheet loop

FROGGER24

Well-known Member
Joined
May 22, 2004
Messages
704
Office Version
  1. 2013
  2. 2010
Platform
  1. Windows
Hi Everyone, I requesting some help to modify some code. How can I modify "Worksheets("Sheet1")." so the code will run on numerous worksheets in workbook (up to 50 worksheets). I would also need the code to stop running if on the current worksheet cells C7 & C11 are blank.

Worksheets("Summary Sheet").Cells(Rows.Count, 1).End(xlUp).Offset(1).Value = Worksheets("Sheet1").Range("D34").Value
Worksheets("Summary Sheet").Cells(Rows.Count, 2).End(xlUp).Offset(1).Value = Worksheets("Sheet1").Range("c7").Value
Worksheets("Summary Sheet").Cells(Rows.Count, 5).End(xlUp).Offset(1).Value = Worksheets("Sheet1").Range("e30").Value
Worksheets("Summary Sheet").Cells(Rows.Count, 6).End(xlUp).Offset(1).Value = Worksheets("Sheet1").Range("e28").Value
Worksheets("Summary Sheet").Cells(Rows.Count, 7).End(xlUp).Offset(1).Value = Worksheets("Sheet1").Range("e15").Value
Worksheets("Summary Sheet").Cells(Rows.Count, 8).End(xlUp).Offset(1).Value = Worksheets("Sheet1").Range("f28").Value
Worksheets("Summary Sheet").Cells(Rows.Count, 9).End(xlUp).Offset(1).Value = Worksheets("Sheet1").Range("e29").Value
Worksheets("Summary Sheet").Cells(Rows.Count, 10).End(xlUp).Offset(1).Value = Worksheets("Sheet1").Range("e24").Value
Worksheets("Summary Sheet").Cells(Rows.Count, 11).End(xlUp).Offset(1).Value = Worksheets("Sheet1").Range("f29").Value
Worksheets("Summary Sheet").Cells(Rows.Count, 12).End(xlUp).Offset(1).Value = Worksheets("Sheet1").Range("AA1").Value

Thanks for any help, and Merry Christmas
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
VBA Code:
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
if ws.name = "Summary Sheet" Then
Else
If ws.Range("C7").Value = ""  And ws.Range("C11").Value = "" Then
Else
Worksheets("Summary Sheet").Cells(Rows.Count, 1).End(xlUp).Offset(1).Value = ws.Range("D34").Value
Worksheets("Summary Sheet").Cells(Rows.Count, 2).End(xlUp).Offset(1).Value = ws.Range("c7").Value
Worksheets("Summary Sheet").Cells(Rows.Count, 5).End(xlUp).Offset(1).Value = ws.Range("e30").Value
Worksheets("Summary Sheet").Cells(Rows.Count, 6).End(xlUp).Offset(1).Value = ws.Range("e28").Value
Worksheets("Summary Sheet").Cells(Rows.Count, 7).End(xlUp).Offset(1).Value = ws.Range("e15").Value
Worksheets("Summary Sheet").Cells(Rows.Count, 8).End(xlUp).Offset(1).Value = ws.Range("f28").Value
Worksheets("Summary Sheet").Cells(Rows.Count, 9).End(xlUp).Offset(1).Value = ws.Range("e29").Value
Worksheets("Summary Sheet").Cells(Rows.Count, 10).End(xlUp).Offset(1).Value = ws.Range("e24").Value
Worksheets("Summary Sheet").Cells(Rows.Count, 11).End(xlUp).Offset(1).Value = ws.Range("f29").Value
Worksheets("Summary Sheet").Cells(Rows.Count, 12).End(xlUp).Offset(1).Value = ws.Range("AA1").Value
End If
End If
Next ws
 
Last edited:
Upvote 0
Solution
Thanks for the reply. I am getting Runtime error 1004, "Application-defined or object-defined error"

Worksheets("Summary Sheet").Cells(Rows.Count, 1).End(xlUp).Offset(1).Value = ws.Range("D34").Value
 
Upvote 0
VBA Code:
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
if ws.name = "Summary Sheet" Then
Else
If ws.Range("C7").Value = ""  And ws.Range("C11").Value = "" Then
Else
Worksheets("Summary Sheet").Cells(Rows.Count, 1).End(xlUp).Offset(1).Value = ws.Range("D34").Value
Worksheets("Summary Sheet").Cells(Rows.Count, 2).End(xlUp).Offset(1).Value = ws.Range("c7").Value
Worksheets("Summary Sheet").Cells(Rows.Count, 5).End(xlUp).Offset(1).Value = ws.Range("e30").Value
Worksheets("Summary Sheet").Cells(Rows.Count, 6).End(xlUp).Offset(1).Value = ws.Range("e28").Value
Worksheets("Summary Sheet").Cells(Rows.Count, 7).End(xlUp).Offset(1).Value = ws.Range("e15").Value
Worksheets("Summary Sheet").Cells(Rows.Count, 8).End(xlUp).Offset(1).Value = ws.Range("f28").Value
Worksheets("Summary Sheet").Cells(Rows.Count, 9).End(xlUp).Offset(1).Value = ws.Range("e29").Value
Worksheets("Summary Sheet").Cells(Rows.Count, 10).End(xlUp).Offset(1).Value = ws.Range("e24").Value
Worksheets("Summary Sheet").Cells(Rows.Count, 11).End(xlUp).Offset(1).Value = ws.Range("f29").Value
Worksheets("Summary Sheet").Cells(Rows.Count, 12).End(xlUp).Offset(1).Value = ws.Range("AA1").Value
End If
End If
Next ws
Your solution worked perfectly, thank you, Merry Christmas to all
 
Upvote 0

Forum statistics

Threads
1,214,551
Messages
6,120,159
Members
448,948
Latest member
spamiki

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