为什么调用多工作表的vba代码在7个工作表后无法按预期工作?

时间:2017-10-11 14:00:29

标签: vba excel-vba excel

我有一些代码,可以让用户选择在工作簿中调用7个工作表中的1个。代码确实按预期工作,直到第7页。当我添加一个附加工作表I时,收到运行时错误代码#13类型不匹配。它挂在If MyValue = False Then上。我检查了表格编号和工作表名称。两者都是正确的,&还试过复制/过去的名字。我想知道7个工作表是否是允许的最大工作表数。如果可能的话,我需要去9个工作表。以下是附加表单的代码:

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

2 个答案:

答案 0 :(得分:1)

您的输入框提示文字太长。我相信它有255个字符的限制(当前提示是277个字符),否则你会收到一个错误。此外,您应该使用它将要保留的数据声明MyValue变量,看起来应该是Byte

如果您想保留确切的格式,我建议您将此InputBox更改为UserForm。

以下是仅适用于开头部分的工作代码。

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

答案 1 :(得分:0)

我认为问题是InputBox提示不能接受超过254个字符。这对我有用

 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")