VBA使用If-Then - Goto调用子程序

时间:2015-05-06 16:39:37

标签: vba excel-vba calling-convention excel

    Sub Testcopy()

    X = MsgBox("Press 1 to clear sheet or press 2 to copy")

        If X = 1 Then GoTo clearsheet
        If X = 2 Then GoTo Copysheet

        'MsgBox ("Incorrect entry")
    End Sub

    Sub clearsheet()

    sheetclearname = InputBox(" Enter the sheet you want to clear")
    Worksheets(sheetclearname).Select
    Worksheets(sheetclearname).UsedRange.Clear
    End Sub


    Sub Copysheet()
    Worksheets("Inputs").Activate
    Worksheets("Inputs").Range("Expenses").Copy

    Worksheets("Sheet3").Activate
     ActiveSheet.Range("A2").PasteSpecial Paste:=xlPasteValues
     ActiveSheet.Range("A2").PasteSpecial Paste:=xlPasteFormats


End Sub

专家

我正在尝试制作一个主程序,要求用户输入选项1或2 选项1采用子程序清除表格,它清除所选的表格

选项2激活子例程CopySheet,并将此选项卡输入范围费用复制到工作表3

当我运行宏时,我得到的标签未定义

  1. 这里未定义的标签是什么以及如何使其工作
  2. 由于

2 个答案:

答案 0 :(得分:2)

$ telnet ::1 8080
Trying ::1...
Connected to ::1.
Escape character is '^]'.

答案 1 :(得分:0)

    Sub expensecopy()

    X = InputBox("Press 1 to clear sheet or press 2 to copy")

        If X = 1 Then Call clearsheet
        If X = 2 Then Call copysheet
        If X <> 1 Or 2 Then Call usererrorhandler
   End Sub



Sub usererrorhandler()

    Z = MsgBox("Incorrect entry, Please enter 1 or 2")
    End Sub

Sub clearsheet()

    sheetclearname = InputBox(" Enter the sheet you want to clear")
    Worksheets(sheetclearname).Select
    Worksheets(sheetclearname).UsedRange.Clear
    End Sub


Sub copysheet()
    Worksheets("Inputs").Activate
    Worksheets("Inputs").Range("Expenses").Copy

    Worksheets("Sheet3").Activate
     ActiveSheet.Range("A2").PasteSpecial Paste:=xlPasteValues
     ActiveSheet.Range("A2").PasteSpecial Paste:=xlPasteFormats

     End Sub

谢谢大家,这里是更新的代码,它完美无缺。只是好奇作为一个附加问题。

我想在用户输入1或2以外的任何内容时发送给用户,从例程usererrorhandler返回到输入选项1或2的选项,而不是重新运行宏。

有什么建议吗?