Worksheet loop

trimmer69

Active Member
Joined
May 22, 2004
Messages
419
Office Version
  1. 2013
  2. 2007
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
 

Some videos you may like

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)

maabadi

Well-known Member
Joined
Oct 22, 2012
Messages
1,563
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
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:
Solution

trimmer69

Active Member
Joined
May 22, 2004
Messages
419
Office Version
  1. 2013
  2. 2007
Platform
  1. Windows
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
 

trimmer69

Active Member
Joined
May 22, 2004
Messages
419
Office Version
  1. 2013
  2. 2007
Platform
  1. Windows
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
 

maabadi

Well-known Member
Joined
Oct 22, 2012
Messages
1,563
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
You're Welcome & thanks for Feedback.
 

Watch MrExcel Video

Forum statistics

Threads
1,126,957
Messages
5,621,823
Members
415,859
Latest member
Vain

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