Looping Through Worksheets

markkramer

Board Regular
Joined
May 8, 2002
Messages
162
I need to create a macro that loops through all the worksheets in a workbook. Each workbook will have an unknown number of worksheets and each worksheet will have an unknown name. The macro will need to:

1. Loop to the next worksheet
2. Open the worksheet (then I'll add code to do things in the worksheet)
3. Store the name of the worksheet in a variable
4. Repeat through all the worksheets

I know what to do once I get the worksheet open but I haven't figured out how to loop through each worksheet nor save the worksheet name in a variable.

Would appreciate help!

Thanks!

M
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Try this

Code:
Dim ws as Worksheet
Dim WorksheetName as String
 
For Each ws in Worksheets
 
WorksheetName = ws.Name ' This will store the sheetname in the variable
 
< your code here >
 
 
Next 'Go to next worksheet
 
Upvote 0

Forum statistics

Threads
1,215,003
Messages
6,122,655
Members
449,091
Latest member
peppernaut

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