My "If Not" Statement is Not Accomplishing My Desired Result

Ark68

Well-known Member
Joined
Mar 23, 2004
Messages
4,564
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I have this code:
Rich (BB code):
Sub staffintegrity()
'Stop
    Dim rngpda As Range
    Dim rwpdaed As Long 'last row of pda range
    Dim bunm As String
    
'hide unstaffed crews
    For Each ws9 In wb_data.Worksheets
        Application.DisplayAlerts = False
        Debug.Print ws9.Name
        bunm = ws9.Name
        If ws9.Name Like "Sheet*" Then
            ws9.Delete
            Debug.Print bunm & " deleted."
            'Exit For
        ElseIf ws9.Name = "Services" Then
            ws9.Delete
            Debug.Print bunm & " deleted."
            'Exit For
        ElseIf ws9.Tab.Color = vbRed Then
            ws9.Visible = xlhiddensheet
            Debug.Print bunm & " hidden."
            'Exit For
        End If
        Application.DisplayAlerts = True
    Next ws9

    cntsh = 0 'count of visible sheets
    For Each ws9 In wb_data.Worksheets
        If ws9.Visible = xlSheetVisible Then
            If Not ws9.Name Like "EV*" Then
                cntsh = cntsh + 1
                With ws9
                    . . . <code> . . .
                End With
            End If
            ttsnr = ttsnr + tsnr
            MsgBox ccnt & " cells analysed in worksheet: " & ws9.Name & Chr(13) & tsnr & " invalid names were replaced.", vbInformation, "INTEGRITY: OK    Worksheet: " & ws9.Name
            tccnt = tccnt + ccnt
        End If
    Next ws9
    MsgBox tccnt & " cells analysed in " & cntsh & " worksheets." & Chr(13) & ttsnr & " invalid names were replaced.", vbInformation, "INTEGRITY: OK    Worbkbook: " & wb_data.Name
End Sub

In an effort to exclude worksheets named "EVE1", "EVE", EVL", I added the if/endif in blue. But this doesn't appear to be working as those sheets are still being included.
 
Is there any chance your sheet names actually start with "Ev" not "EV" ?
"Like" is case sensitive.

If that is the case try:
VBA Code:
If Not UCase(ws9.Name) Like "EV*" Then
 
Upvote 0

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Not really as it doesn't appear to be the workbook with the relevant sheets in it...
Sorry Rory, at the time, providing that workbook would have been providing data.
Anyway, here is that file you need stripped of any data.

Link to Worksheet
Is there any chance your sheet names actually start with "Ev" not "EV" ?
"Like" is case sensitive.

No. I don't think so.
 
Upvote 0
It's not processing sheets whose names match that criterion. However, your Msgbox statement is outside the End If statement, so it probably looks like it is.
 
Upvote 0
Solution
Oh, I see I think. But the message box does include the number of cells checked, which I believe matches the actual cell count of the sheets it's supposed to get. Maybe. Perhaps that value (ccnt) remains from the previously processed sheet.

I'll start by moving my Msgbox to a hopefully more appropriate spot.

Thanks again Rory!
 
Upvote 0
Hi Rory, felt I should say that your suggestion was the solution.
Thank you!
 
Upvote 0

Forum statistics

Threads
1,216,932
Messages
6,133,596
Members
449,817
Latest member
TaylorGall

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