Search code examples
exceluserformvba

Editing VBA script Userform


Private Sub CommandButton1_Click()
Dim Sheetname As String
whichSheet = InputBox("In which sheet do you wish to enter data? Type in sheet name as Toner, Copy Paper, Alteration, Mail Package, Gloves, and Special Requests.", "Input")
Dim ws As Worksheet
For Each ws In Worksheets
    Select Case ws.CodeName
        Case "Toner", "Copy Paper", "Mail Package", "Alteration", "Gloves", "Special Requests"
            Worksheets(whichSheet).Activate ws.Name
        Case Else
            If ws.CodeName <> ("Toner")("Copy Paper"), "Mail Package", "Alteration", "Gloves", "Special Requests") Then
            MsgBox "Please use another Sheet name"
            Exit Sub
            End If
    End Select
Next ws

Hello guys, I'm trying to make this code open specific sheets in excel using the exact sheet name as an input to get to it. If the sheet name is not there or spelled incorrectly it should display the "Please use another Sheet name" message. I have this so far but it won't even recognize the right sheet name. I appreciate your help.


Solution

  • If I've understood correctly, I think you are overcomplicating and this will suffice. You just need to check the sheet name entered via the input box and if it falls in the first list, activate it (I assume).

    Private Sub CommandButton1_Click()
    
    Dim whichSheet As String
    whichSheet = InputBox("In which sheet do you wish to enter data? Type in sheet name as Toner, Copy Paper, Alteration, Mail Package, Gloves, and Special Requests.", "Input")
    
    Select Case whichSheet
        Case "Toner", "Copy Paper", "Mail Package", "Alteration", "Gloves", "Special Requests"
            Worksheets(whichSheet).Activate
        Case Else
            MsgBox "Please use another Sheet name"
            Exit Sub
    End Select
    
    End Sub