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.
Related
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?
Hello I just started programming and I have a question: I want to open a new frame with the command button, it worked on the UserForm but not on the sheet in Excel. Can anyone help me?
With an ActiveX button:
Insert a button into your sheet from the Developer tab -> Insert -> ActiveX Controls.
Double-click on the button and it will take you to the VBA editor, in the Click event of that button.
Add the code UserForm.Show in the event code where UserForm is the name of your form.
With a form control:
Create a Sub in a module where you call the Show method of your UserForm (or frame as you call it).
Then insert a button into your sheet from the Developer tab -> Insert -> Form Controls. It will ask you what macro you want to assign to that button. Choose the Sub from step 1.
Sub showForm()
UserForm1.Show
End Sub
I think what could be done is to create a kind of shape or button on your worksheet, then, attach a vba script to activate the frame when clicked. It's just the same way on the UserForm only that you are using a shape or button in which you have attached a macro to on your worksheet.
I already created a workbook with Macro to have 40 people to use it. So the end user just need to click the button, and the worksheet will give the end user the next available client information. However, as this is a shared file, if one person click the button while another person is opening it, that person could not open it. So I am thinking to create another workbook as end user's interface. The original workbook will be the back end.
We call the original work as A, the new one as B. I want to use Marco in B to trigger the Macro in A. When the end user click a button in B, it is the same as clicking the button in A. How should I do that?
You can use this if you are trying to call macro test in Module1 of worksheet A,
Application.Run "'C:\Users\Yui\Desktop\A.xlsm'!Module1.test"
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()
}
I have a already existing excel file with some headers and single worksheet, I want that whenever a user click on add new worksheet button headers should be repeated on the it.Is it possible, if yes please tell be the steps to create one excel like this.
You could use a template, although that's a question for the superuser site.
As a macro answer try this:
Create a new worksheet with only the header on it, Rename this sheet Template.
Go into the Developer tab and insert an ActiveX button, double click it to go into the vba editor (you may need to save your document as a macro enabled document first)
Change the name of your button to btnNewWorkSheet and the caption to something like "Click here for a new worksheet"
Go into the sheets page in the VBA editor and copy this code:
Private Sub btnNewWorkSheet_Click()
Sheets("Template").Copy After:=ActiveSheet
End Sub
Now you can click on the button on any page where it appears to create a copy of the Template page
You can paste the following code to "ThisWorkbook" module:
Private Sub Workbook_NewSheet(ByVal Sh As Object)
If TypeName(Sh) = "Worksheet" Then
Sheet1.Range("A1:K1").Copy Sh.Range("A1")
End If
End Sub
Sheet1 is the name of the single worksheet object, replace A1:K1 with the range of your headers, replace A1 with the desired destination.