Search code examples
vbaexcelcalling-convention

VBA Calling subroutine using If-Then - Goto


    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

Guys

I am trying to make a main routine which ask the user to enter option 1 or 2 Option 1 takes to subroutine clearsheet and it clears the sheet chosen

option 2 activates sub routine CopySheet and this copies from tab input range expenses to sheet 3

when I run the macro I get label not defined

  1. What is label not defined here and how to make this works

Thanks


Solution

  • Sub Testcopy()
    
    X = MsgBox("Press 1 to clear sheet or press 2 to copy")
    
        If X = 1 Then Call clearsheet
        If X = 2 Then Call Copysheet
    
        'MsgBox ("Incorrect entry")
    End Sub