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)
Related
I am try to determine the row of a clicked command button automatically.
I am using the below code:
Private Sub CommandButton2_Click ()
Dim b As Object, r As Integer
Set b = ActiveSheet.Buttons(Application.Caller)
With b.TopLeftCell
r= .row
End With
MsgBox "Row Number " & r
End Sub
But I keep getting an error message when I run the code saying "Run Time ERROR 1004 -Unable to get Buttons Property of the Worksheet class"
Can you please advise?
As #BigBen notes, you probably have an ActiveX button, so you can use:
Private Sub CommandButton2_Click ()
MsgBox Me.CommandButton2.TopLeftCell.Row
End Sub
If you have a lot of ActiveX buttons and you don't want to write a handler for each of then then you can use this approach: https://bettersolutions.com/excel/macros/vba-control-arrays.htm
You might find it easier though to use forms buttons instead (or just shapes) and a single "OnAction" sub using the Application.Caller approach.
I have the following code to activate a macro when a change is made to cell A1
Class Module
Option Explicit
Private WithEvents App As Application
Private Sub Class_Initialize()
Set App = Application
End Sub
Private Sub App_SheetChange(ByVal Sh As Object, ByVal Target As Range)
If Sh.Name = "S" Then
Dim rngKeyCells As Range
Set rngKeyCells = Sh.Range("A1")
If Intersect(rngKeyCells, Target) Is Nothing Then
Exit Sub
End If
Application.Run "a"
End If
End Sub
This_Workbook Code
Private OurEventHandler As EventHandler
Private Sub Workbook_Open()
'Initiates the data change when the filter is changed
Set OurEventHandler = New EventHandler
End Sub
This works absolutely fine usually, however an issue occurs if i try making a change in A1 after i open VBA.
It will work fine 90% of the time but if during one of the previous macro's that i run, there is an error, it won't work.
Example - I run a macro that deletes the Worksheet to the left of the active one. If there is no worksheet to the left of the active one it will error. I press end and that's fine. Now if i try to change the cells A1 and expect the macro above to run, nothing happens.
Is this the kind of thing that is solvable without showing the entire macro? Or could it be something that is inbuilt into the rest of the macro that is causing the issue?
Thanks
In the programming there is something named Design Patterns. In your case, it would be really useful to make a Singleton for the App variable.
Here is a good example of it for VBA:
How to create common/shared instance in vba
As already mentioned in the comments: When an error happens and end is pressed, the whole VBA-Context is reset, the content of global Vars is lost (so your variable OurEventHandler is nothing).
If you can't catch all errors to ensure that this reset isn't happening (and I think you never really can), maybe it is easiest to implement the event handler in the worksheet itself:
Private Sub Worksheet_Change(ByVal Target As Range)
' Ne need to check Worksheet as the Hander is valid only for the Worksheet
if target.row = 1 and target.column = 1 then
Application.Run "AIMS_and_eFEAS_Report.AIMS_Criteria"
end if
End Sub
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
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
I have a userform with a basic combobox and command button. When the user hits the command button, I want the UserForm to close, and the value of the combobox to be saved in a variable that can be accessed by a subroutine contained within "ThisWorkbook".
In the UserForm code:
Public employee_position As String
Public Sub CommandButton1_Click()
employee_position = Me.ComboBox1.Value
Unload Me
End Sub
In the "ThisWorkbook" Code
Private Sub GetUserFormValue()
Call Userform_Initialize
EmployeePosition.Show
MsgBox employee_position
End Sub
When "GetUserFormValue()" runs, the UserForm comes up, you can select a value in the combobox and press the command button, but when the MsgBox comes up, it displays "" (Nothing)
What am I doing wrong here?
When you Unload Me, I think you lose all information associated with the module (including the global variable). But if you use Me.Hide rather than Me.Unload, then you can access the value of the form after the routine returns. So try this:
-- userform code includes:
Public Sub CommandButton1_Click()
Me.Hide
End Sub
-- main module includes:
Private Sub GetUserFormValue()
Call Userform_Initialize
EmployeePosition.Show
MsgBox EmployeePosition.ComboBox1.Value
Unload EmployeePosition
End Sub
I think that should work.
I had the same problem, and this is how I resolved it:
If the main code is in a worksheet, and the variable is declared as public in that worksheet (e.g. in Microsoft Excel Objects -> Sheet1 (Sheet1)), the result from "Unload Me" cannot be passed from a UserForm to the worksheet code.
So to solve my problem, I inserted a new Module, and declared my public variable there. I didn't even have to move my code from the worksheet to the module... just the declaration of the public variable.
I hope this works for you too!
Andrew