Opening a new frame with a CommandButton in Excel - excel

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.

Related

Programmatically click a button in Excel through Macro (VBA)

I have a custom-button, linked to a macro which does a few things. After they are done, I want the code to programmatically click the below highlighted button (more clearly, open the import XML dialog), so my user could see the file selection window and it would proceed for it's further tasks.
Representative image
Here's my current macro code:
Option Explicit
Sub Button1_Click()
' ... All my existing macro code
' ---------- programmatically click the Import button (from Developer) here ------------
End Sub
What should I add in my macro to achieve this?
I have found the solution to my question -
% - Alt
l - Developer
t - Import
are the shortcut keys to import an XML into the sheet, and this command runs the shortcut keys programmatically, opening my desired file selection box. -
Application.SendKeys ("%lt")
CommandBars.ExecuteMso method:
https://learn.microsoft.com/en-us/office/vba/api/Office.CommandBars.ExecuteMso
"Executes the control identified by the idMso parameter."
Hover over the button where you would add it to the ribbon, you will see the text includes "XmlImport"
"This method is useful in cases where there is no object model for a particular command. Works on controls that are built-in buttons, toggleButtons, and splitButtons."
Private Sub ExecuteMso_MoveToFolder()
' https://learn.microsoft.com/en-us/office/vba/api/Office.CommandBars.ExecuteMso
' Hover over the button where you would add it to the ribbon,
' you will see the text includes "XmlImport"
CommandBars.ExecuteMso ("XmlImport")
End Sub

User Form action in Excel Sheets

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.

vba code to add a command button in current sheet

What is the vba code to add a command button on currentworksheet, then insert a code to run whenever that button is clicked?
advance thanks.
Note: the task for the command button click is : select workbook name 123(which is already opend), select worksheet name ABC then select range a123.
I have an example here to add buttons and assign macros to them.
Create Buttons and assign macros to them

How can I tie a VBA macro to a button in Excel

I have created a macro and a button but I can't see where to edit the button setting to invoke the macro. I'm using Excel 2003.
If you've used a Form Control. The chance to assign a macro will come up right after you add the button to the sheet. Or, if the button is already on the sheet, you can right-click on it and select Assign Macro.
If you've used an ActiveXControl, right-click on the button and select View Code. This will open up the VBE with
Private Sub myCommandButtonName_Click()
End Sub
From this sub call your macro like so...
Private Sub myCommandButtonName_Click()
Call myMacro
End Sub

How to capture the "Print" button from the menu bar in a macro

I have a sheet with a custom button on it from where I control the printing process.
Now the user clicks on the menu bar's print icon and this produces an "undefined" output.
How can I intercept this menu bar button?
Thanks
Handle the Workbook_BeforePrint event.
private sub Workbook_BeforePrint (cancel as boolean)
'//g_MyFlag is set when the user clicks you toolbar button.
'//It must get cleared in the end of your procedure.
if not g_MyFlag then cancel = true: exit sub
end sub
In MS Word, it's also possible to redefine the system macro itself. You'd have to create a macro named FilePrint(), and Word would call it instead its own. A pity you can't do that in Excel.

Resources