vba help - on error goto multiple 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 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
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
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
 
Upvote 0
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).
 
Upvote 0
Hi Yongle,

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

Thanks
mg
 
Upvote 0

Forum statistics

Threads
1,214,917
Messages
6,122,233
Members
449,075
Latest member
staticfluids

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