繁体   English   中英

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

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

我有代码,使用户可以选择在工作簿中调用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

您的输入框提示文本太长。 我相信它的限制为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

我认为问题在于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")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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