jeudi 20 mai 2021

VBA conflicting sheet name checking via an IF function

I have two subroutines that seem to conflict one with each other. Each one of them is for creating and formatting a new worksheet if it doesn't already exist and I ran into a problem where I can only create one of them and not the other (whichever one is the first one I call to create). I found taht it seems to skip the end sub function in an if function:

    For Each Worksheet In Application.ActiveWorkbook.Worksheets
    Application.DisplayAlerts = False
        
    If Worksheet.Name = "Machine Layout" Then
    Exit Sub
    
    Else:
    Sheets.Add.Name = "Machine Layout"
    End If
    Next

And gives an error that sheet with such name already exists.

Both subroutines use this same code except the worksheet name changes.

I'm wondering what's teh problem with that? Could someone, please, help?

Aucun commentaire:

Enregistrer un commentaire