Accessing worksheets in a loop

bsb2013

New Member
Joined
Jul 16, 2010
Messages
31
I'm trying to access each one of the worksheets in my workbook and pass the variable through the workbook to access each. Sorry if that was unclear. Basically

Dim mysheet as worksheet

For each mysheet in worksheets
Sheets(mysheet.name).select
next mysheet

but the line: sheets(mysheet.name).select has an error. I think it is because I need a string where I say mysheet.name.... Unfortunately, I have little experience with VBA and am used to Java. Is there an parallel action in VBA to retrieving a name from an object and passing it as a string parameter?
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
You normally don't need to select sheets to perform actions on them. Use something like this:

Code:
Dim mysheet as worksheet

For each mysheet in worksheets
With mysheet
.Range("A1").Font.Bold = True 'example - change to suit your requirements
End With
next mysheet
 
Upvote 0
What if I was trying to do an action like hide all of the sheets?

Well that would be impossible, since at least one sheet must be visible, but here's the code that you would need...

Code:
Dim mysheet as worksheet

For each mysheet in worksheets
mysheet.Visible = False
next mysheet
 
Upvote 0

Forum statistics

Threads
1,224,505
Messages
6,179,152
Members
452,891
Latest member
JUSTOUTOFMYREACH

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