Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VBA have a sub run another sub

how to run a sub stored in another worksheet's module?

worksheet1

sub endDay()
    'something here
end sub

worksheet2

sub reCalc()
    'something here
end sub

I want recalc to be able to run on its own but I want to be able to press the button for "endDay", have it do its thing, and then preform "recalc" at the end instead of pressing one and then go to sheet2 to press the other.

Can someone give me a sample so I can have an idea where to begin?

like image 448
Stephan Daudt Avatar asked Jan 28 '13 23:01

Stephan Daudt


3 Answers

Take a look at the code names for the worksheets - you can see them in the tree view in the Visual Basic Editor. Usually they are named Sheet1, Sheet2, etc. (independent of the actual worksheet names, which are shown in brackets). Use this name in your code )instead of Worksheets("Sheet1")and you'll also get an autocomplete list - with your sub!

Thus, this will do the job:

Sheet1.reCalc
like image 168
Peter Albert Avatar answered Oct 09 '22 08:10

Peter Albert


sheets("worksheet2").reCalc

might be what you are after

like image 39
Rick Avatar answered Oct 09 '22 06:10

Rick


I know it is 2 years later, but today I was looking for the same thing. As far as the subs in the worksheets may be private, this is a good solution:

Sub Maina()
    Run "tbl_Input.btn_main_Click"
End Sub

tbl_Input is the VBA name of the worksheet, not the caption.

like image 23
Vityata Avatar answered Oct 09 '22 08:10

Vityata