Worksheet Loop Help Please

My Aswer Is This

Well-known Member
Joined
Jul 5, 2014
Messages
19,526
Office Version
  1. 2021
Platform
  1. Windows
I want to put the word "George" in cell A1 of every Worksheet in my Workbook. Why does this script not work for me?
Code:
Sub WorksheetLoop()
Dim WS_Count As Integer
Dim I As Integer
         WS_Count = ActiveWorkbook.Worksheets.Count
         For I = 1 To WS_Count
         Cells(1, 1) = "George"
         Next I
End Sub
 

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.
Hi,

You need to ref the sheet...
Code:
Sheets(I).Cells(1,1).value = "George"

Cheers,
Alan.
 
Upvote 0
Also, a simpler way is to reference the worksheets directly rather than through the use of the counter I

Code:
Sub WorksheetLoop()
  Dim WS As Worksheet
  
  For Each WS In Worksheets
    WS.Cells(1, 1).Value = "George"
  Next WS
End Sub
 
Upvote 0
Thanks Peter.
I'm always looking for the best way to do things. I previously had been using loops and activate sheet but this works a lot better I'm sure. I enjoy learning more here every day.
 
Upvote 0
Peter, one last question. How would I do this if I had 20 worksheets in my workbook and only want to run the loop on the first 10 sheets?
 
Upvote 0
Try-

Code:
Sub WorksheetLoop()
Dim WS_Count As Integer
Dim I As Integer
         WS_Count = 10
         For I = 1 To WS_Count
         Sheets(I).Cells(1, 1) = "George"
         Next I
End Sub

Note: this will operate on the first 10 sheets found on your tab order.

Hope this helps,

FarmerScott
 
Upvote 0
Again, a little simpler would be to not bother with the WS_Count variable as it takes two lines to set up and only gets used once.

Note also that I have declared I as Long, not Integer.
vba converts Integer Variables to Long Variables before working with them so you might as well ..
- Save vba that conversion process, and
- Save typing 3 letters in your declaration line. ;)

Code:
Sub WorksheetLoop()
  Dim I As Long
  
  For I = 1 To 10
    Sheets(I).Cells(1, 1).Value = "George"
  Next I
End Sub
 
Upvote 0

Forum statistics

Threads
1,216,075
Messages
6,128,668
Members
449,463
Latest member
Jojomen56

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