vba help, on error goto label

Mallesh23

Well-known Member
Joined
Feb 4, 2009
Messages
976
Office Version
  1. 2010
Platform
  1. Windows
Hi Team,

I am trying to learn on Error goto Label,

Code is working ...... not sure whether its right approach.
Also can we Combine all labels at the end of code into if else stament to shorten code.

Err_First_Loop:
On Error Resume Next
MsgBox "Error in First Loop in cell " & c.Address, vbCritical
On Error GoTo 0
Exit Sub


VBA Code:
[CODE=vba]Sub test()

Dim c As Range
On Error GoTo Err_First_Loop
    For Each c In Range("a2:a4")
        MsgBox c / 2
    Next c


On Error GoTo Err_Second_Loop
    For Each c In Range("c2:c4")
        MsgBox c / 2
    Next c


On Error GoTo Err_Third_Loop
    For Each c In Range("e2:e5")
        MsgBox c / 2
    Next c


Exit Sub

Done:

Err_First_Loop:
    On Error Resume Next
    MsgBox "Error in First Loop in cell " & c.Address, vbCritical
On Error GoTo 0
Exit Sub


Err_Second_Loop:
    On Error Resume Next
    MsgBox "Error at Second loop in Cell " & c.Address, vbCritical
    On Error GoTo 0
Exit Sub


Err_Third_Loop:
    On Error Resume Next
        MsgBox "Error at Third loop in Cell" & c.Address, vbCritical
    On Error GoTo 0

End Sub
[/CODE]


Below is a Sample data on which I am testing.

Book11
ABCDE
1Situation1Situation2Situation3
2202020
3#N/A90#N/A
460#N/A60
588
Sheet1


Thanks
mg
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Try this
VBA Code:
Sub Test()
    Dim c As Range, msg As String
      
    For Each c In Range("a2:a4")
        On Error Resume Next
        Debug.Print c / 2
        If Err.Number <> 0 Then msg = msg & vbCr & "Error at First loop in Cell " & c.Address(0, 0)
        On Error GoTo 0
    Next c
    If Err.Number <> 0 Then msg = "Error at Second loop in Cell " & c.Address

    For Each c In Range("c2:c4")
        On Error Resume Next
        Debug.Print c / 2
        If Err.Number <> 0 Then msg = msg & vbCr & "Error at Second loop in Cell " & c.Address(0, 0)
        On Error GoTo 0
    Next c

    For Each c In Range("e2:e5")
        On Error Resume Next
        Debug.Print c / 2
        If Err.Number <> 0 Then msg = msg & vbCr & "Error at Third loop in Cell " & c.Address(0, 0)
        On Error GoTo 0
    Next c
    If Len(msg) > 0 Then GoTo ErrorInLoop
Exit Sub

ErrorInLoop:
    MsgBox msg, vbCritical, ""
End Sub

NOTE
Debug.Print writes to immediate window in VBA - avoids all those message boxes popping up
To Display Immediate Window click on View \Immediate Window when in VBA editor
 
Upvote 0
Hi Yongle,

Wowww ! Perfect !, It worked !

Appreciate your help and Millions of thanks for it! ? (y)



Thanks
mg
 
Upvote 0

Forum statistics

Threads
1,214,520
Messages
6,120,008
Members
448,935
Latest member
ijat

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top