Simple problem in checking multiple sheets written cells

Klash Ville

Board Regular
Joined
Sep 19, 2017
Messages
83
For some reason, the code below, only the first variable ( i ) works, while the other one ( o ) doesn't, is simply ignored.

All I want to do is check if 2 sheets are empty or not (with no written cells).
Code:
For i = 1 To Worksheets.Count        
If Worksheets(i).Name = "Feuil1" Then
    exists = True
End If
    Next i
    If Not exists Then
        MsgBox "Apparently the sheet where you were suppose to upload an example of (Feuil1) doesn't exist" & vbNewLine & vbNewLine & _
        "• This is because it was either deleted or renamed into something different than its original name...", vbExclamation
        
        Sheets("Parameters").Select
        Exit Sub
    End If
    For o = 1 To Worksheets.Count
        If Worksheets(o).Name = "Country" Then
            exists = True
        End If
    Next o
    If Not exists Then
        MsgBox "Apparently the sheet where you were suppose to upload an example of (Country) doesn't exist" & vbNewLine & vbNewLine & _
        "• This is because it was either deleted or renamed into something different than its original name...", vbExclamation
        
        Sheets("Parameters").Select
        Exit Sub
    End If
 
Last edited:

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Try:
Code:
Sub test()
    Application.ScreenUpdating = False
    Dim exists As Boolean
    For i = 1 To Worksheets.Count
        If Worksheets(i).Name = "Feuil1" Then
            exists = True
        End If
    Next i
    If Not exists Then
        MsgBox "Apparently the sheet where you were suppose to upload an example of (Feuil1) doesn't exist" & vbNewLine & vbNewLine & _
        "• This is because it was either deleted or renamed into something different than its original name...", vbExclamation
        Sheets("Parameters").Select
    End If
    exists = False
    For o = 1 To Worksheets.Count
        If Worksheets(o).Name = "Country" Then
            exists = True
        End If
    Next o
    If Not exists Then
        MsgBox "Apparently the sheet where you were suppose to upload an example of (Country) doesn't exist" & vbNewLine & vbNewLine & _
        "• This is because it was either deleted or renamed into something different than its original name...", vbExclamation
        Sheets("Parameters").Select
        Exit Sub
    End If
    Application.ScreenUpdating = True
End Sub
You have to set exist to false before the second loop.
 
Last edited:
Upvote 0
Hi
Give this a go
Code:
    Dim Fexists As Boolean
    Dim Cexists As Boolean
    Dim i As Long
    
    Fexists = False
    Cexists = False
    
    For i = 1 To Worksheets.Count
        If Worksheets(i).Name = "Feuil1" Then Fexists = True
        If Worksheets(i).Name = "Country" Then Cexists = True
    Next i
    
    If Not Fexists Then
        MsgBox "Apparently the sheet where you were suppose to upload an example of (Feuil1) doesn't exist" & vbNewLine & vbNewLine & _
            "• This is because it was either deleted or renamed into something different than its original name...", vbExclamation
    
        Sheets("Parameters").Select
    End If
    If Not Cexists Then
        MsgBox "Apparently the sheet where you were suppose to upload an example of (Country) doesn't exist" & vbNewLine & vbNewLine & _
            "• This is because it was either deleted or renamed into something different than its original name...", vbExclamation
    
        Sheets("Parameters").Select
    End If
    If Not Fexists Or Not Cexists Then Exit Sub
    MsgBox "OK"
If the first sheet doesn't exist you exit the sub before checking the second sheet
 
Upvote 0
Hi,
another way perhaps

Code:
Dim sheetname As Variant
    Dim i As Integer


    sheetname = Array("Feuil1", "Country")
    
    On Error Resume Next
    For i = LBound(sheetname) To UBound(sheetname)
        If IsError(Worksheets(sheetname(i)).Name) Then Exit For
    Next i


    If Err > 0 Then
        MsgBox "Apparently the sheet where you were suppose to upload an example of (" & sheetname(i) & ") doesn't exist" & vbNewLine & vbNewLine & _
        "• This is because it was either deleted or renamed into something different than its original name...", vbExclamation
    End If
    
    On Error GoTo 0

Dave
 
Upvote 0
@dmt32
If the 1st sheet doesn't exist, your code won't go on to check if the 2nd sheet exists, which I think is what the Op was complaining about.
 
Last edited:
Upvote 0
@Klash Ville
FYI
All I want to do is check if 2 sheets are empty or not (with no written cells).
This isn't what your code is doing. All you are doing is checking if the sheet exists, not if there is any data.
 
Upvote 0
@dmt32
If the 1st sheet doesn't exist, your code won't go on to check if the 2nd sheet exists, which I think is what the Op was complaining about.

suggestion is directed at the OP & may or may not be what they want - just have to wait & see Ops response.

Dave
 
Last edited:
Upvote 0
Sorry for the delay everyone. The problem has been solved, thank you all =)

And yes, ignore the part of where I mentioned about written cells, all I want is to check if they exists in the workbook. I already having working code in checking for any written cells.

I will also try your code Dave later, I like the idea of having an array and then just put all the worksheets name into them.

Also, the problem was not in the code stopping if the first sheet didn't existed. It was the second variable connected on checking the 2nd sheet was simply ignored no matter what. Like @mumps said, I had to had something like a Exist = false

Sorry for the missunderstanding, it was a clumsy post from my part. But it's all solved thanks to you guys =)
 
Upvote 0
Glad we could help & thanks for the feedback.
 
Upvote 0
Sorry for the delay everyone. The problem has been solved, thank you all =)
I will also try your code Dave later, I like the idea of having an array and then just put all the worksheets name into them.

Hi glad you have a resolution - arrays are useful & can save repeating code like loop tests

as an example to include both worksheets being reported when missing you could update my suggestion something like this:

Code:
Dim sheetname As Variant
    Dim Missingsheet As String
    Dim i As Integer


    sheetname = Array("Feuil1", "Country")
    
    On Error Resume Next
    For i = LBound(sheetname) To UBound(sheetname)
        If IsError(Worksheets(sheetname(i)).Name) Then Missingsheet = Missingsheet & sheetname(i) & Chr(10)
    Next i
    On Error GoTo 0


    If Len(Missingsheet) > 0 Then
        MsgBox "Apparently the sheet where you were suppose to upload an example of " & Chr(10) & Missingsheet & Chr(10) & "doesn't exist" & vbNewLine & vbNewLine & _
        "• This is because it was either deleted or renamed into something different than its original name...", vbExclamation
    End If

This will build a string & should report all missing sheets in MsgBox.

Hope Helpful

Dave
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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