Excel VB Macro question

vtapia

New Member
Joined
Jun 22, 2011
Messages
5
I have a quick question regarding some VB syntax. Basically, I want to check the 1st if statement and its true, and go to BPNum, I want to skip the rest of the code and go to the end of the subroutine, I do not want it to go back and execute the last bit of code.
Here is my code:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)

Dim ws As Worksheet
'I have to validate the sheet change before doing anything
Set ws = s_BPSummary

If ws.Range("E3") <> ws.Range("A1") Then
    GoTo BPNum
ElseIf ws.Range("E4") <> ws.Range("B1") Then
    GoTo BPName
End If

BPNum:
    Application.EnableEvents = False
    ws.Range("A1") = ws.Range("E3")
    Call Move_Details_To_Summary
    Application.EnableEvents = True
    GoTo finish

BPName:
    Application.EnableEvents = False
    ws.Range("B1") = ws.Range("E4")
    Call Move_Name_Details_To_Summary
    Application.EnableEvents = True
    
finish:
End Sub

I know that with GoTo, after it is done executing wherever you "WentTo" it goes back and executes the next line of code, which is why it's executing BPName, is there any way I can make this skip BPName??
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Welcome to the board...

I would avoid using GoTo statements
You end up with what is effectionately referred to as "Spaghetti Code"
The more you do it, the more difficult it is to read and understand what the code is doing.

The purpose of the If structure is to do exactly what you want.
Do 1 thing if the expression is true, and do a different thing (or nothing at all) if it's false...

Try

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim ws As Worksheet
'I have to validate the sheet change before doing anything
Set ws = s_BPSummary
 
If ws.Range("E3") <> ws.Range("A1") Then
    Application.EnableEvents = False
    ws.Range("A1") = ws.Range("E3")
    Call Move_Details_To_Summary
    Application.EnableEvents = True
ElseIf ws.Range("E4") <> ws.Range("B1") Then
    Application.EnableEvents = False
    ws.Range("B1") = ws.Range("E4")
    Call Move_Name_Details_To_Summary
    Application.EnableEvents = True
End If
End Sub


Hope that helps.
 
Last edited:
Upvote 0
GoTo is best avoided. Does this work

Code:
Private Sub Worksheet_Change(ByVal Target As Range)

Dim ws As Worksheet
'I have to validate the sheet change before doing anything
Set ws = s_BPSummary

If ws.Range("E3") <> ws.Range("A1") Then
    Application.EnableEvents = False
    ws.Range("A1") = ws.Range("E3")
    Call Move_Details_To_Summary
    Application.EnableEvents = True
    GoTo finish
ElseIf ws.Range("E4") <> ws.Range("B1") Then
 Application.EnableEvents = False
    ws.Range("A1") = ws.Range("E3")
    Call Move_Details_To_Summary
    Application.EnableEvents = True
End If
End Sub
 
Upvote 0
Thanks for your help, I actually figured out it was some code in one of my macros that I forgot to comment out. I did change my code to what you suggested, it's much easier to read/understand :)
 
Upvote 0

Forum statistics

Threads
1,224,517
Messages
6,179,242
Members
452,898
Latest member
Capolavoro009

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