In VBA for Excel, how do I assign an existing function or sub, say myFun(), in a module to a newly added ActiveX click button directly?
I understand I can do the following on the worksheet code page where the ActiveX command (click) button is located.
Private Sub myFun_Click()
myFun()
End Sub
By "directly" I mean naming the button "myFun" and point the button directly to the function myFun() with having to placing myFun() in another sub as shown above.
There are 2 types of controls you can place on a worksheet:
ActiveX controls
You handle their Click event in the code-behind, by double-clicking the control and generating a handler for it.
Form controls
You assign a macro to these controls, exactly like you would assign a macro to any other shape. The macro can be an existing one (any parameterless Public Sub procedure in a non-private standard module).
If you want to assign a button to MyFun, assuming the signature looks like this:
Public Sub MyFun()
Then you can try using a form control instead of an ActiveX control.
Note: MyFun() has to be a Sub to be exposed as a Macro. A 'Sub' is a procedure, not a function. A 'Function' has a return value, a 'Sub' doesn't. A 'Function in a standard code module (.bas) can be exposed to be used in worksheet formulas as a UDF, a 'Sub' in a standard code module (.bas) can be exposed to be used as a macro.
VBA events bound to control are "local" to the worksheet by defintion, therefore if you put button "A" on a worksheet "w1" the resulting click event handler function will be named:
Private Sub A_Click()
But if you put button "A" on worksheet "w2" the click event handler signature will be the same but actually trigger the click event of the button placed on "w2".
If you want to implement the same behavior when the user clicks on button "A" from different worksheets, then you need to do this by adding a Sub/Function on a VBA Module, which is shared among all the worksheets of the workbook object.
Private Sub A_Click() {
CommonFunction()
}
Related
I'm creating the game War in Excel using VBA. Everything is working, the only thing I need to do is be able to reset everything to it's original values when the game is over or if they press a reset button. After the game is over is easy because I just save the values in a variable at the beginning of the module. However, I don't know how to get the original values for the reset button. How do I make the values from the first button public so I can access them in the reset button? Or do I need to set up a Workbook_Open event and record the values when the project is open and then reference them in the other modules? I have no idea how to do this however so any help will be appreciated.
When you declare a variable outside a Sub or Function, it will be publicly available within your project. These public variables also must be declared in either a Module or Class Module, and not inside ThisWorkbook code or one of the Worksheets code.
You could then have a Sub SetInitialValues that sets the original values and call it in Workbook_Open and when the reset button is pressed.
Example:
Option Explicit
Public player1Name As String
Public player2Name As String
Sub SetInitialValues()
player1Name = "John"
player2Name = "Peter"
End Sub
I'm creating an excel module to export any table inside a workbook as .csv
My module generate a UserForm where the user can chose which table to export inside a combobox and then click 'export'
Everything is working perfectly, but I have to create a button in my workbook and call my module on click.
However I want to keep it very simple to setup inside any workbook by just import the module and run it with a keyboard shortcut '''Ctrl+R''' for example.
I search a lot about that but I never find something, Is it something possible?
Yes you can what you want to. First import your module to your new excel workbook and then assign keyboard shortcut to Workbook_Open event by copying code to ThisWorkbook area.
Sub MyMacro()
MsgBox ("ok")
End Sub
In ThisWorkbook area (which is in Microsoft Excel Objects / Project Window)
Private Sub Workbook_Open()
Application.MacroOptions Macro:="MyMacro", Description:="", ShortcutKey:= _
"a"
End Sub
This code assign Ctrl+"Defined_Letter" shortcut to your MyMacro macro. In this case, the code's shortcut is [Ctrl+a].
I want to call/ activate a button at the end of a Sub. I know the cell address of the command button, but I don't know the name/ID of the button.
How do I select/activate the button without looping through each button on active sheet?
https://stackoverflow.com/a/30600479/13049793
I have created my buttons on multiple rows with each assigned to the same macro, from my understanding, I cannot call the macro the button is assigned to because the macro uses the button's relative position, below is a simple example to illustrate the use of the buttons relative position:
Sub ExampleButtonClick()
Dim Cellvalue As String
Cellvalue = ActiveSheet.Buttons(Application.Caller).TopLeftCell.Offset(0, -1).Value
Msgbox (Cellvalue)
End Sub
i assume you want to run one macro that at the end initiates a different macro, i also assume the reason the button is rather not as a sub that is just called is because it has its independent function that can be used without the other sub
assuming that you used a command button as an activeX control, why not just use a private sub that the button performs and place that in an individual module and at the end of your current sub;
call module1 'assuming the private sub exists in module 1
or rather include the code even if duplicated in the first sub, searching for a button seems the circular route unless there is another intention you wish to pursue.
maybe elaborate on the task at hand or post the current code you are working on?
I got confused about something. How can i control the excel sheet via userform?
For example:
Open user form by clicking form button on excel sheet
Create new excel sheet by clicking the button in user form.
Do i need to write the required code in the "module" section of vba or the "userform" section?
First, Create a new Module, and inside add a Sub, like the code below:
Public Sub Main()
' call a Use_ Form1
User_Form1.Show
End Sub
Second , in your Sheet, once adding a Button, you will get a message box to add Macro Name, Select Main. Now, once you click on your Sheet's Button, your Main module will run, and it will Call the User_Form1.Show.
Third, in your User_Form, add a button (in this example it's Btn_1), once you click it, add the simple code below (just for testing purposes).
Private Sub Btn_1_Click()
' add a Sheet to this workbook, after the last one, and name it "Test"
ThisWorkbook.Sheets.Add(After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)).Name = "Test"
End Sub
And that's an example how to connect between the 2 objects.
I have assigned a macro to a button in worksheet 2. I have created a new button in a new worksheet and now i want the same macro to run or call from worksheet 4.
...hmm....
Put your code in a module:
You can then call it from anywhere. If you were to add an ActiveX control to a sheet then you could just double click the button in design mode and amend the code to the following and it should work:
Private Sub CommandButton1_Click()
Call CallMeFromAnyWorkSheet
End Sub