Change this line of code

mahmed1

Well-known Member
Joined
Mar 28, 2009
Messages
2,302
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hi all

I have created a loop to go though all sheets except a few
but im concerned that more sheets might get added where ill need to ignore them so i have created a range on my refsheet that list all the sheetnames i want to loop through and NOT avoid

how do i change this line of code:
Code:
dim ws as worksheet

for each ws in thisworkbook.workheets
if ws.name <>”sheetname” and ws.name <> “sheetname2” then
’do something with that worksheet
A = ws.name ‘etc
end if
next ws
change to
Code:
for each ws in array(SheetLookupRange)
A = ws.name etc
next ws
this way i can always loop through sheets that i want that are listed in my sheetlookup range
 
Last edited by a moderator:

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Maybe...

Code:
Dim rCell As Range, ws As Worksheet

For Each rCell In Range("SheetLookupRange")
    Set ws = Sheets(rCell.Value)
    With ws
        'Do something with ws
    End With
Next rCell

M.
 
Upvote 0
I generally like to list the sheets I want to exclude. So, I will either create a separate sheet called ExclusionList, or an array string of sheet names - really depending on how often I add or modify the exclusion list.

For example, I might have

Code:
msEXCLUSION_SHEET_LIST As String = "Sheet 1;Sheet 2;Sheet 3"

Private Function vGetExclusionList() As Variant
  vGetExclusionList = Split$(msEXCLUSION_SHEET_LIST, ";"))
End Function

Private Sub Example()
  Dim i As Long
  Dim j As Long
  Dim bExclude as Boolean
  
  Dim wsh As Excel.Worksheet

  For Each wsh In ThisWorkbook.Worksheets
    For i = lbound(vGetExclusionList) to UBound(vGetExclusionList)
      If wsh.Name = vGetExclusionList(i) Then
        bExclude = True
        Exit For
      End If
    Next i
    If not bExclude Then
      Debug.Print wsh.Name
    End If
  Next wsh
End Sub

Or else you put them in a named range, and read them at runtime. The end result is the same.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,898
Messages
6,175,272
Members
452,628
Latest member
dd2

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