mercredi 6 septembre 2017

Excel VBA - If Else still performing Else

My code is fairly simple but a bit puzzling. I might be committing a minor error - pardon my newbie-ness. The Sheets.Add.Name line still gets executed despite having Boolean = True, thus a new worksheet is created with the Sheet# naming convention.

Sharing my code:

Private Sub create_analyst_btn_Click()

Dim strUser As String
Dim DateToday As String
Dim ws As Worksheet
Dim boolFound As Boolean

strUser = newanalyst_form.user_User.Value

For Each ws In Worksheets

    If ws.Name Like strUser Then boolFound = True: Exit For
    Next

    If boolFound = True Then
        MsgBox ("User already exists.")
    Else
        DateToday = Format(Date, "-yyyy-mm-dd")
        Sheets.Add.Name = strUser & DateToday
        Unload Me
    End If

End Sub

Aucun commentaire:

Enregistrer un commentaire