简体   繁体   中英

Why is vba code for calling multi worksheet not working as expected after 7 worksheets?

I have code that gives the user an option of calling 1 of 7 worksheets in a workbook. The code does work as expected up to sheet number 7. When I add an additional sheet I, receive a Run time error code #13 type mismatch. It hangs on If MyValue = False Then . I have checked the sheet numbers & the sheet names. Both are correct, & have also tried copy/past of the names. I am wondering if 7 worksheets is the maximum number of sheets allowed. I need to go to 9 worksheets if Possible. Here is the code with the additional sheet:

Sub First_Half_Reports()
   Dim MyValue
     Dim i As String

    'MsgBox prompt:="1st 6 Months of Reports?", Title:="Referral Workbook - Data Entry"
    i = MsgBox("Continue to 1st 6 Months of Reports?", vbYesNo, " Referral Workbook - Data Entry")

    If Not i = vbYes Then Exit Sub

    'First message shows in the body of the box, message 2 shows at the top of the box.
    Do
        MyValue = Application.InputBox("Only Click Ok or Cancel after your Selection!!!!!!!" & vbCrLf & _
                               "Enter 1 for October Report" & vbCrLf & _
                               "Enter 2 for November Report" & vbCrLf & _
                               "Enter 3 for December Report" & vbCrLf & _
                               "Enter 4 for January Report" & vbCrLf & _
                               "Enter 5 for February Report" & vbCrLf & _
                               "Enter 6 for March Report" & vbCrLf & _
                               "Enter 7 for 1st Quarter" & vbCrLf & _
                               "Enter 8 for 1st 6 Month Report", "Walk In Training Data Entry")
        ' Sub messaage box exit.
        If MyValue = False Then
            Exit Sub
        ElseIf (MyValue = 1) Or (MyValue = 2) Or (MyValue = 3) Or (MyValue = 4) Or (MyValue = 5) Or (MyValue = 6) Or (MyValue = 7) Or (MyValue = 8) Then
            Exit Do
        Else
            MsgBox "You have not made a valid entry.  Please try again.", vbInformation, "Referral Workbook - Data Entry"
        End If
    Loop    'Code to Execute When Condition = value_1
    Select Case MyValue
        Case 1
                     If ActiveSheet.CodeName = "Sheet45" Then
               ' The message below only shows when you are on the active sheet.
                        MsgBox "You are already on October Report!", vbInformation, "Referral Workbook - Data Entry"
                    Else
                        Sheets("October_Report").Activate
                        Range("A1").Select
                    End If
        'Code to Execute When Condition = value_2
        Case 2
                     If ActiveSheet.CodeName = "Sheet46" Then
               ' The message below only shows when you are on the active sheet.
                        MsgBox "You are already on November Report!", vbInformation, "Referral Workbook - Data Entry"
                    Else
                        Sheets("November_Report").Activate
                        Range("A1").Select
                    End If

        'Code to Execute When Condition = value_3
        Case 3
                     If ActiveSheet.CodeName = "Sheet54" Then
               ' The message below only shows when you are on the active sheet.
                        MsgBox "You are already on December Report!", vbInformation, "Referral Workbook - Data Entry"
                    Else
                        Sheets("WI_DT_1ST").Activate
                        Range("A1").Select
                End If
                'Code to Execute When Condition = value_4
       Case 4
                     If ActiveSheet.CodeName = "Sheet48" Then
               ' The message below only shows when you are on the active sheet.
                        MsgBox "You are already on January Report!", vbInformation, "Referral Workbook - Data Entry"
                    Else
                        Sheets("January_Report").Activate
                        Range("A1").Select
                End If
      'Code to Execute When Condition = value_5
       Case 5
                     If ActiveSheet.CodeName = "Sheet49" Then
               ' The message below only shows when you are on the active sheet.
                        MsgBox "You are already on February Report!", vbInformation, "Referral Workbook - Data Entry"
                    Else
                        Sheets("February_Report").Activate
                        Range("A1").Select
                End If
       'Code to Execute When Condition = value_6
       Case 6
                     If ActiveSheet.CodeName = "Sheet50" Then
               ' The message below only shows when you are on the active sheet.
                        MsgBox "You are already on March Report!", vbInformation, "Referral Workbook - Data Entry"
                    Else
                        Sheets("March_Report").Activate
                        Range("A1").Select
                End If
                'Code to Execute When Condition = value_7
       Case 7
                     If ActiveSheet.CodeName = "Sheet11" Then
               ' The message below only shows when you are on the active sheet.
                        MsgBox "You are already on 1st Quarter Report!", vbInformation, "Referral Workbook - Data Entry"
                    Else
                        Sheets("1St_Qtr").Activate
                        Range("A1").Select
                End If
       'Code to Execute When Condition = value_8
       Case 8
                     If ActiveSheet.CodeName = "Sheet43" Then
               ' The message below only shows when you are on the active sheet.
                        MsgBox "You are already on 1st 6 Month Report!", vbInformation, "Referral Workbook - Data Entry"
                    Else
                        Sheets("1st_6_Month_Report").Activate
                        Range("A1").Select
                End If

    End Select 
End Sub

Your inputbox prompt text is too long. I believe it has a limit of 255 characters (current prompt is 277 characters), otherwise you will get an error. Additionally, you should be declaring the MyValue variable with the data it's going to hold, it looks like a Byte should do.

If you want to retain the exact formatting, I'd recommend changing this InputBox into a UserForm.

Here is working code just for the beginning portion.

Sub SOExample()
    Dim MyValue       As Byte
    Const InputBoxTxt As String = "Only Click Ok or Cancel after your Selection!" & vbCrLf & _
                                  "Enter 1 for Oct Report" & vbCrLf & _
                                  "Enter 2 for Nov Report" & vbCrLf & _
                                  "Enter 3 for Dec Report" & vbCrLf & _
                                  "Enter 4 for Jan Report" & vbCrLf & _
                                  "Enter 5 for Feb Report" & vbCrLf & _
                                  "Enter 6 for March Report" & vbCrLf & _
                                  "Enter 7 for 1st Quarter" & vbCrLf & _
                                  "Enter 8 for 1st 6 Month Report"

    MyValue = Application.InputBox(InputBoxTxt, "Walk In Training Data Entry")

    If MyValue = 0 Then
        Exit Sub
    ElseIf MyValue >= 1 Or MyValue <= 8 Then
        'Exit Do
    Else
        MsgBox "You have not made a valid entry.  Please try again.", _
                vbInformation, _
                "Referral Workbook - Data Entry"
    End If
End Sub

I think the problem is that InputBox prompt can't accept more than 254 characters. This worked for me

 MyValue = Application.InputBox("Only Click Ok or Cancel after your Selection!!!!!!!" & vbCrLf & _
                           "Enter 1 for October" & vbCrLf & _
                           "Enter 2 for November" & vbCrLf & _
                           "Enter 3 for December" & vbCrLf & _
                           "Enter 4 for January" & vbCrLf & _
                           "Enter 5 for February" & vbCrLf & _
                           "Enter 6 for March" & vbCrLf & _
                           "Enter 7 for 1st Quarter" & vbCrLf & _
                           "Enter 8 for 1st 6 Month", "Walk In Training Data Entry")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM