VBA Help: Loop thru each tab EXCEPT those that start with letters...

losamfr17

Board Regular
Joined
Jun 10, 2016
Messages
149
Good morning dear wizards.

Can I please get some guidance on this dilemma?

I want to run the code below ONLY on worksheets whose names DO NOT START with "ULT".
Some tab names are all letters, numbers, or both letters and numbers. Is there a way to do that please?

Code:
Sub UNprotect_All_Sheets()


    Dim ws As Worksheet
        
    For Each ws In ActiveWorkbook.Worksheets
        ws.Unprotect Password:="adminf18"
    Next ws
    
End Sub
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
The below code should do the trick. This link explains what the Left function does.

Code:
Sub UNprotect_All_Sheets()


    Dim ws As Worksheet
       
    For Each ws In ActiveWorkbook.Worksheets
        If Left(ws.Name, 3) <> "ULT" Then
            ws.Unprotect Password:="adminf18"
        End If
    Next ws
    
End Sub
 
Upvote 0
This is not looping thru each tab. Any idea why?

Code:
Sub ETS_All_Sheets()


    Dim ws As Worksheet
       
    For Each ws In ActiveWorkbook.Worksheets
        If ws.Name <> "ULT124" Then
            Range("M4").Value = "Maybe"
        End If
    Next ws
    
End Sub
 
Upvote 0
You need to qualify the range, otherwise it works on the active sheet
Code:
ws.Range("M4").Value = "Maybe"
 
Upvote 0
This is not looping thru each tab. Any idea why?

Code:
Sub ETS_All_Sheets()


    Dim ws As Worksheet
       
    For Each ws In ActiveWorkbook.Worksheets
        If ws.Name <> "ULT124" Then
            Range("M4").Value = "Maybe"
        End If
    Next ws
    
End Sub


Are you including the Left Function?

Code:
If Left(ws.Name, 3) <> "ULT" Then
    ws.Range("A1").Value = 1
End If
 
Upvote 0
You need to qualify the range, otherwise it works on the active sheet
Code:
ws.Range("M4").Value = "Maybe"

Awesome, it works now! Thank you.
How would you add the OR operator to the code, say you want to change tabs that don't start with "ULT" OR "HOME" OR "ROLL"?
 
Upvote 0
VBA: If with OR code won't work - Please help

Hello,

Could someone please explain to me why this code won't work?
I want to tell the code to skip worksheets with names starting with "ULT" OR "HOME" OR other names as well.

Code:
Sub ETS_All_Sheets()


    Dim ws As Worksheet
       
    For Each ws In ActiveWorkbook.Worksheets
        If (Left(ws.Name, 3) <> "ULT" Or Left(ws.Name, 3) <> "HOME") Then
            ws.Range("M4").Value = "Oui"
        End If
    Next ws
    
End Sub
 
Upvote 0
Re: VBA: If with OR code won't work - Please help

Try this.

Code:
Sub ETS_All_Sheets()


    Dim ws As Worksheet
       
    For Each ws In ActiveWorkbook.Worksheets
        If Not ws.Name Like "ULT*" And Not ws.Name Like "HOME*" Then
            ws.Range("M4").Value = "Oui"
        End If
    Next ws
    
End Sub
 
Upvote 0
Re: VBA: If with OR code won't work - Please help

It actually worked like a charm, thank you very much!!
I've learning VBA and getting better thanks to people like you!
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,814
Messages
6,121,711
Members
449,049
Latest member
THMarana

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