vba help - on error goto multiple label

Mallesh23

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

I am trying to learn applying multiple label, below is my attempted code.

please assist how to apply properly with message. thanks in advance for your help !

VBA Code:
Sub test()

Dim i As Long

For i = 2 To 14

    If Cells(i, 1).Value = 30 Then
        On Error GoTo Amt_30
       
     ElseIf Cells(i, 1).Value = 40 Then
        On Error GoTo Amt_40
       
    ElseIf Cells(i, 1).Value = 50 Then
        On Error GoTo Amt_50
       
    Else
        Cells(i, 1).Font.Bold = True
           
    End If


Next i


Done:
Exit Sub


Amt_30:
MsgBox "We found value 30"

Amt_40:
MsgBox "We found value 40"

Amt_50:
MsgBox "We found value 50"

End Sub



Book13
A
1SR No
23
34
420
58
61
730
83
940
105
118
121
Sheet1


Thanks
mg
 

Some videos you may like

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.

Yongle

Well-known Member
Joined
Mar 11, 2015
Messages
6,977
Office Version
  1. 365
Platform
  1. Windows
Not sure what it is you are trying to do

Perhaps

VBA Code:
Sub FindSomething()
    Dim i As Long
    For i = 2 To 14
        Select Case Cells(i, 1).Value
            Case 30
                GoTo Amt_30
            Case 40
                GoTo Amt_40
            Case 50
                GoTo Amt_50
            Case Else
                Cells(i, 1).Font.Bold = True
        End Select
    Next i

Done:
Exit Sub

Amt_30:
MsgBox "We found value 30"

Amt_40:
MsgBox "We found value 40"

Amt_50:
MsgBox "We found value 50"

End Sub

If this is not what you want ... explain what VBA is required to achieve
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
52,886
Office Version
  1. 365
Platform
  1. Windows
Or maybe
VBA Code:
Sub FindSomething()
    Dim i As Long
    For i = 2 To 14
        Select Case Cells(i, 1).Value
            Case 30
                GoTo Amt_30
            Case 40
                GoTo Amt_40
            Case 50
                GoTo Amt_50
            Case Else
                Cells(i, 1).Font.Bold = True
        End Select
MyNext:
    Next i

Done:
Exit Sub

Amt_30:
MsgBox "We found value 30"
GoTo MyNext

Amt_40:
MsgBox "We found value 40"
GoTo MyNext

Amt_50:
MsgBox "We found value 50"
GoTo MyNext
End Sub
But this is called spaghetti code & is best avoided (IMO).
 

Mallesh23

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

Perfect ! It worked ! Thanks for your help with multiple answer.

Thanks
mg
 

Watch MrExcel Video

Forum statistics

Threads
1,122,437
Messages
5,596,130
Members
414,043
Latest member
thomas Stein

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
Top