Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditionally run VBA code

Tags:

excel

vba

Is it possible to have multipe buttons, lets say 'Button 1' and 'Button 2' run the same VBA code but yield a different result based on the button that was pressed?

For instance when I press button 1 I want it to go to a website, load data and put it on to Sheet 1. But when I press button 2, it goes to the same site and loads it to Sheet 2.

I know I can have multiple instances of the same VBA code (with different names) however I am hoping to simplify the code and prevent it from being overly complicated.

like image 528
user1664305 Avatar asked Jun 22 '26 19:06

user1664305


1 Answers

If you are using a Forms button you can assign the same macro and use Application.Caller to return the name/id of the calling button.

Sub Test()
MsgBox Application.Caller & " was pressed"
End Sub
like image 68
brettdj Avatar answered Jun 26 '26 16:06

brettdj