Conditionally run VBA code - 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.

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

Create one sub to do the work and pass the sheetname as an argument to that sub. I did it with a string variable, but you can do it with a worksheet variable as well.
Sub Button1_Click()
LoadWebsiteToSheet "Sheet1"
End Sub
Sub Button2_Click()
LoadWebsiteToSheet "Sheet2"
End Sub
Sub LoadWebsiteToSheet(sName as String)
'... code to load website to Worksheets(sName)
End Sub

I was leaning more towards brettdj's solution as well
If you assign the same macro to many buttons, you will get different results based on the button name.
You could name the button to the sheet you wanted. Practice with this. Add several buttons and assign this code to them. Click each button to see what happens.
Sub GetButtonName()
Dim Btn As String
Btn = ActiveSheet.Shapes(Application.Caller).Name
MsgBox Btn
End Sub

Related

How can I make my excel-vba macro click a button?

Here is the situation:
I have several buttons in different worksheets (but in same workbook) and the only thing those buttons do is print the worksheet. So imagine 6 different worksheets that all have a print button that quickly selects the relevant information and sends it to the printer.
Here is what I want to accomplish:
I now want to have a single button that will effectively click on all of the six buttons at the same time. So instead of clicking on the six buttons to send every indivudual worksheet to the printer, I would click one button and all of the worksheets would be sent to the printer
I would thus need help with creating a macro that essentially clicks on other macro-buttons which is what I am struggling with.
Thank you for your help.
My situation in image
Call each subroutine from your main subroutine. Something like this as an example:
Private Sub CommandButton1_Click()
MsgBox "Button 1"
End Sub
Private Sub CommandButton2_Click()
MsgBox "Button 2"
End Sub
Private Sub CommandButton3_Click()
MsgBox "Button 3"
End Sub
Private Sub Main()
CommandButton1_Click
CommandButton2_Click
CommandButton3_Click
End Sub

Passing control to button in vba

I have a sub routine (named WBR) in "WBR45" sheet, and I have created a button in "Main" sheet. I have assigned macro also to that button.
And i have added the below code in the module1 where my sub WBR is present:
Sub Button2_Click()
WBR
End Sub
My Sub which i want to run through the button (in module1):
Sub WBR()
Dim Count1Criteria As Variant
Dim Count3Criteria As Variant
Dim test As Variant
Dim wf As WorksheetFunction
Set wf = Application.WorksheetFunction
End Sub
(PS: the code is too huge so i have just given the beginning)
But I when I click on the button, it doesnt run or show any result.
I suspect your code is not connected to the code you think is under Sub Button2_Click, try the code below to "debug" (when pressing your button)
Sub Button2_Click()
MsgBox "Test here" ' <-- to test if you are getting here
Sheets("WBR45").WBR
End Sub
You must insert the code on the WBR subroutine example:
Sub WBR()
'do something
End Sub
The macro on the button should look something like this
Sub Button2_Click()
WBR
End Sub
It looks like your macro isn't assigned to the button, the code you post looks correct. Right click the button > Assign macro. Then select the correct macro.
If you want to debug, place a red dot on the grey bar directly left to the code, this will insert a pause when the macro is executed.
(The code in the TS actually doesn't do anything, so it might be that the code does get executed but you don't notice anything)

how to prompt sheet selection in excel vba

When a workbook has many sheets such as 100, you can activate the sheet quickly by right-clicking the arrows on the bottom left and then a prompts shows up by lets you choose which sheet you want to select. However, I want to be able to reach this prompt without clicking and using just the keyboard. So I want to create a simple macro where I can quickly pull up this prompt by assigning to something like ctrl+g. However, I do not know how to do this in vba.
Build your replica Activate form: Insert a userform into your project. Set its caption to "Activate". Add a label to it that says "Activate:". Add a Listbox and two buttons. Label one button Ok and the other Cancel.
In the UserForm_Initialize sub of the userform, Loop through the names of your sheets and add them to the listbox....something like:
Private Sub UserForm_Initialize()
Dim oSheet As Worksheet
For Each oSheet In Worksheets
ListBox1.AddItem oSheet.Name
Next
End Sub
Private Sub CommandButton1_Click()
Sheets(ListBox1.Text).Activate
Unload Me
End Sub
Private Sub CommandButton2_Click()
Unload Me
End Sub
Now in the ThisWorkbook section of the VBA Project, add a sub to show your userform:
Private Sub ShowActivateForm()
UserForm1.Show
End Sub
Go back to Excel and click the Macros button on the developer tab. You should see a macro named ShowActivateForm. Select it and click Options to assign it your desired hotkey.

Userform not triggering Initialize or Activate event

I kept a userform control button in my worksheet to fire up a macro, which in turn shows a user form, In the form I wish to display the opened files in checkboxes(using the Workbooks collection).I wish to run a macro that performs action for the user selected files only.
So for the button in my worksheet, I have assigned the following macro
Private Sub Button2_Click()
Load MyForm
MyForm.Show
End Sub
At first I kept the below code in the module where my macro sub is there.Since it's not working, I right clicked on user form and selected view code and kept the below code there.But still it's showing the same static designed user form, not the dynamic.I kept breakpoint at both load Myform and MYform.Show() and I stepped through code.It never went into intialize or activate method at all.
Private Sub MyForm_Activate()
'for checking the whether this method is called or not I am trying to change caption
MyForm.LabelSelectFile.Caption = "dhfdfldkfldzjf;zdfkz;d"
Dim mymyWorkBook As Workbook
For Each mymyWorkBook In Workbooks
'code for creating checkbox based on the file name displayed by the workbook collection
Next mymyWorkBook
End Sub
I can't understand why that event is not getting triggered.Please help me to overcome this.Thanks in advance
Even though the name of the form is MyForm, you still need to use userform.
'~~> in your worksheet
Private Sub Button2_Click()
MyForm.Show
End Sub
'~~> In the userform code area
Private Sub UserForm_Initialize()
'~~> Your code here
End Sub
or
Private Sub UserForm_Activate()
End Sub
The best is to always select the event from the drop down, rather than typing it

How to programmatically click an ActiveX button in another workbook?

I have a workbook Wbk1 where a sheet has an ActiveX button. I want to run the button's associated Sub from another workbook,Wbk2. It's not practical to just copy the Sub's code into Wbk2 because it in turn calls a bunch of functions from Wbk1. I tried the following:
Sub pushButton()
Dim obj As OLEObject
Dim btn As MSForms.CommandButton
For Each obj In Wkb1.Sheets("testSheet").OLEObjects
If TypeOf obj.Object Is MSForms.CommandButton Then
set btn=obj.Object
debug.print btn.Caption 'used to test whether or not For loop is picking up button
obj.Activate
SendKeys " "
End If
Next
End Sub
It's not only an inelegant SendKeys() hack, but it doesn't work; the button gets focus but it doesn't get pushed. I know that the For loop is correctly iterating through the objects, including the CommandButton, because I can pick up the caption for the button. How can I click on this button (and thereby run its private Sub) from Wbk2?
EDIT: The actual filename in question is in the format 123A_1.0-2.0.xlsm. I think the periods are causing some trouble with regard to the solutions posted in comments and responses below, because when I remove the periods these techniques are successful.
How about just
Application.Run (wb1.Name & "!MacroName")