Calling a Procedure

dave8

Active Member
Joined
Jul 8, 2007
Messages
275
Is it possible to call a label in a procedure? In other words, I want to run part of the code in a procedure rather than running the entire procedure.


Call LabelA

Public sub ProcA

do something...


LabelA:

do something with LabelA..

End sub
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Probably it would be simplest to put the code in "LabelA" in a separate sub that can be called by either procedure:

Example: Both Macro1 an Macro3 here call Macro2. Macro2 is like your "LabelA"
Code:
Sub Macro1()
    '//Some Code
    Call Macro2
    '//More Code
End Sub

Sub Macro2()
    '//Code
End Sub

Sub Macro3()
    '//Some Code
    Call Macro2
    '//More Code
End Sub
 
Upvote 0
As a point of interest, you could do something like this:-
Code:
[FONT=Courier New]Call ProcA("")       [COLOR=green]' runs everything[/COLOR][/FONT]
 
[FONT=Courier New]Call ProcA("LabelA") [COLOR=green]' jumps to labelA[/COLOR][/FONT]
Code:
[FONT=Courier New]Public Sub ProcA(RunWhat as String)[/FONT]
 
[FONT=Courier New]If RunWhat="LabelA" The GoTo LabelA[/FONT]
 
[FONT=Courier New]' do something...[/FONT]
 
[FONT=Courier New]LabelA:[/FONT]
 
[FONT=Courier New]' do something with LabelA[/FONT]
 
[FONT=Courier New]End Sub[/FONT]
I've never done this in {cough} years of programming - just because it's possible doesn't make it sensible. Xenou's suggestion is much better.
 
Upvote 0

Forum statistics

Threads
1,216,117
Messages
6,128,937
Members
449,480
Latest member
yesitisasport

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