VBA: Goto statement not working inside For-Loop ?

VBA: Goto statement not working inside For-Loop ?

Asked on October 26, 2018 in VBA.
Add Comment


  • 2 Answer(s)

       This code can be helpful to solve the Excel VBA:Goto statement not working inside For-Loop:

      Dim myCol As ListColumn
      For Each myCol In myTable.ListColumns
        On Error GoTo ErrCol
        Dim myDate As Date
        myDate = CDate(myCol.Name)
        On Error GoTo 0
          ' MORE CODE HERE '
    NextCol:
      Next myCol
      Exit Sub ' or something '
    ErrCol:
      Resume NextCol
    
    Answered on October 26, 2018.
    Add Comment

      The following code is to explain For Loop,

    Public Sub test()
      On Error GoTo Err1:
      Debug.Print 1 / 0
      ' more code
    

    Err1:

      On Error GoTo -1 ' clears the active error handler
      On Error GoTo Err2: ' .. so we can set up another
      Debug.Print 1 / 0
      ' more code
    

    Err2:

      MsgBox "Got here safely"
    End Sub
    
    Answered on October 26, 2018.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.