Global list of worksheets for other modules to use

GeeWhiz7

Board Regular
Joined
Nov 22, 2021
Messages
214
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
Hi folks,

I have a workbook with many worksheets (54) and I am searching for an easier way to clear the cell content on some of them while leaving others untouched prior to running macros that refresh information on the cleared sheets.

Here is a simplified version of what I do currently. Since I don't clear all worksheets, I'm setting each one individually rather than a loop.

VBA Code:
Sub ClearIt()
Dim aSh, bSh, cSh, dSh As Worksheet '*********imagine here there are about 30 additional ones

Set aSh = Worksheets("a")
Set bSh = Worksheets("b")
Set cSh = Worksheets("c")
Set dSh = Worksheets("d")

With .aSh
.cells.clear
End With

'...*******repeat for each sheet
End Sub

So its not super efficient and when I add some worksheets I have to go back into a few different modules and add Worksheet variables, Set, With, etc.
I also tried setting an array with all the worksheet names and then looping through the array, but it really doesn't decrease the steps that much.

Is there a way to have a global list of worksheets at the top of all my code that then individual modules can access to do things like .cells.clear, etc?
This would allow me to only have to add new worksheet names in one place.
 

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,)
I think that using a userform which lists all the sheets that you could choose from could possibly work for you.
clear the cell content on some of them
By this do you mean clear everything on the chosen sheets? Please clarify.
 
Upvote 0
You could so something like

VBA Code:
Dim sheetNames as Variant, oneSheetName as Variant

SheetNames = Array("Sheet1", "Sheet2", "Sheet5")

For Each oneSheetName in sheetNames
    ThisWorkbook.Sheets(oneSheetName).Cells.Clear
Next oneSheetName
 
Upvote 0
Hi,
Rather than hard code the names, create a list in a worksheet which will be easier to maintain & have your code read from that.

try this update to your code

VBA Code:
Sub ClearIt()
    Dim ws      As Worksheet
    Dim wsNames As Variant, wsName As Variant
   
    wsNames = ThisWorkbook.Worksheets("Sheet Names").Range("A1").CurrentRegion.Value
   
    For Each wsName In wsNames
        Set ws = ThisWorkbook.Worksheets(wsName)
       
        'do stuff
        ws.Cells.Clear
       
        Set ws = Nothing
    Next wsName

End Sub

note: no error checking included.

You will need to add all required worksheets to sheet below named "Sheet Names"
This sheet can be hidden.

Book1
A
1Sheet1
2Sheet2
3Sheet3
4Sheet4
5Sheet5
Sheet Names


Dave
 
Upvote 0
Solution
Click here to download a sample file. Run the macro in Module1.
 
Upvote 0
You could so something like

VBA Code:
Dim sheetNames as Variant, oneSheetName as Variant

SheetNames = Array("Sheet1", "Sheet2", "Sheet5")

For Each oneSheetName in sheetNames
    ThisWorkbook.Sheets(oneSheetName).Cells.Clear
Next oneSheetName
I can say that all the methods proposed worked and to mumps, yes it is to delete everything.
So I can think of uses for all of the examples, but wondering if a variation of mikerickson's approach would work...I can't get it to so far, but

Is there a way to declare the list public and then use it in any subsequent macros?
Maybe that looks like this, but a working version?

VBA Code:
Option Explicit

Public SheetNames As Variant = Array("Sheet1","Sheet2","Sheet4")
Public oneSheetName As Variant


Sub ClearIt()

For Each oneSheetName In SheetNames
    ThisWorkbook.Sheets(oneSheetName).Cells.Clear
Next oneSheetName

End Sub
 
Upvote 0
Have you tried the sample file I posted in Post#5?
 
Upvote 0
Have you tried the sample file I posted in Post#5?
I did and it works perfectly. My only catch is that I would have more than 30 check-boxes (of 50 worksheets) to check so would either need to initialize them as checked to start with or hand check them each time. So my thinking was if I put the 30 names in a list (array) in the declarations as public, I could loop using that variable. But, as usual for me, what seems like it might work, isn't working.
1660156066123.png
 
Last edited:
Upvote 0
So my thinking was if I put the 30 names in a list (array) in the declarations as public, I could loop using that variable. But, as usual for me, what seems like it might work, isn't working.

Did you try the suggestion in #post 4?

Dave
 
Upvote 0
Did you try the suggestion in #post 4?

Dave
I did and that worked very well too. I was hoping to park it all at the top of my code as public, but i think that having a separate list on a worksheet will work and in the end probably will be clearer to me what is going on 6 months down the line when I am looking back at my code for tweaks. Thank you guys for the solutions! It's always interesting to see how different people approach the solution in a different way.
 
Upvote 0

Forum statistics

Threads
1,214,909
Messages
6,122,189
Members
449,072
Latest member
DW Draft

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