Clear used range from array of sheets

FryGirl

Well-known Member
Joined
Nov 11, 2008
Messages
1,366
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I have an array of worksheets that need to have the used ranges cleared. This works fine, but now can I use it when you use a variable and Set the names of the worksheets?

This does not work for me -- Dim ShtArr: ShtArr = Array("wsone", "wstwo", "wsthree", "wsfour")

Code:
Sub ClearSheets()
    
    Dim wsone      As Worksheet: Set wsone = Sheets("Inventory")
    Dim wstwo      As Worksheet: Set wstwo = Sheets("Sales")
    Dim wsthree    As Worksheet: Set wsthree = Sheets("Work Hours")
    Dim wsfour     As Worksheet: Set wsfour = Sheets("Misc")

    Dim ShtArr: ShtArr = Array("Inventory", "Sales", "Work Hours", "Misc")
    Dim ShtArrCounter As Long
    For ShtArrCounter = 0 To UBound(ShtArr): Sheets(ShtArr(ShtArrCounter)).UsedRange.Clear: Next
    
End Sub
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Tested it, does work except for locked sheets. What error does it display and where.
Or do you mean this:
VBA Code:
'...
Dim ShtArr: ShtArr = Array(wsone, wstwo, wsthree, wsfour)
Dim ShtArrCounter As Long
For ShtArrCounter = 0 To UBound(ShtArr): Sheets(ShtArr(ShtArrCounter).Name).UsedRange.Clear: Next
 
Last edited:
Upvote 0
Solution
That does it. So it's the .Name piece that was needed. Thank you
 
Upvote 0
Not only, you needed to change your Array ;).
Also, you could have done some debugging to see what was 'passed' by ShtArr(ShtArrCounter) when using a variable in the array.
Thanks for the positive feedback(y), glad having been of some help.
 
Upvote 0
As you have declared the variables as worksheets, you could also use
VBA Code:
    Dim ShtArr: ShtArr = Array(wsone, wstwo, wsthree, wsfour)
    Dim ShtArrCounter As Long
    For ShtArrCounter = 0 To UBound(ShtArr): ShtArr(ShtArrCounter).UsedRange.Clear: Next
 
Upvote 0
Thank you Fluff for the additional help. In the end, I'm thinking about using the below which kind of cuts out the middleman.

I'll just change the code name in the VBE that way it doesn't matter what is the name on the worksheet tab. Eliminates potential error I suppose.

VBA Code:
Sub ClearSheets()
    Dim ShtArrCounter As Long
    Dim ShtArr: ShtArr = Array(Inventory, Sales, WorkHours, Misc)
    For ShtArrCounter = 0 To UBound(ShtArr): ShtArr(ShtArrCounter).UsedRange.Clear: Next ShtArrCounter
End Sub
 
Upvote 0
Glad we could help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,223,239
Messages
6,170,947
Members
452,368
Latest member
jayp2104

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