Programmatically click a button in Excel through Macro (VBA) - excel

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

Related

Excel hiding columns [duplicate]

Is there a way, to trigger an event (call a sub) in Excel VBA, when i manually hide a row/column?
I want the same row to be hidden in all following sheets, when it is hidden in a particular sheet.
Is that possible?
Thanks in advance
There is no direct event trigger to capture hiding or unhiding columns. There are clumsy workarounds, using formulae in cells but those feel like a kludge when using and not really flexible.
However, there is an indirect way to capture this event if you use Excel 2007 or newer. This is neat and extremely flexible.
Modify the Ribbon XML (if it's present): You need to be able to modify the Ribbon's customUI14.xml (for Excel 2010) or customUI.xml (for Excel 2007).
Create the custom Ribbon UI XML file (if not present): You may use Ron De Bruin's excellent Custom UI Editor from http://www.rondebruin.nl/win/s2/win001.htm (which is also endorsed in quite a few official Microsoft examples).
XML changes for capturing events: Open the Excel file in the Custom UI Editor and add the XML code as shown below. That will enable a column hide or unhide event to trigger a macro in your code.
VBA code to execute your actions: Add the code listed below to take action once the event is captured.
Reference: This excellent solution was provided by Andy Pope here (MSDN link).
Custom XML code:
<customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui" >
<commands >
<command
idMso="ColumnsHide"
onAction="Column_Hide_Macro"/>
<command
idMso="ColumnsUnhide"
onAction="Column_UnHide_Macro"/>
</commands >
</customUI>
Custom UI Editor screenshot:
VBA code:
Sub Column_Hide_Macro(control As IRibbonControl, ByRef CancelDefault)
MsgBox ("You have hidden a column")
' You may put your code here
' to check if your monitored row is hidden
CancelDefault = False ' This enables the default action to continue
End Sub
Sub Column_UnHide_Macro(control As IRibbonControl, ByRef CancelDefault)
MsgBox ("You have unhidden a column")
' You may put your code here
' to check if your monitored row is unhidden
CancelDefault = False ' This enables the default action to continue
End Sub

I want to create an add button

I am newbie in excel and I really need a button that if pressed adds a certain value to the cells I selected, is that possible?
You can add buttons to Excel by enabling developer mode:
On the File tab, go to Options > Customize Ribbon.
Under Customize the Ribbon and under Main Tabs, select the Developer check box.
Then on the developer ribbon you can click insert, then select a button to draw onto the sheet.
You will then be given the option to assign a macro to the button (Also later accessible by right clicking the button).
As for the VBA code of the macro, you would need to be more specific about what functionality you require, is the 'certain value' to be added to the selected cells always the same value?
Someone answered me in two minutes on another site with this perfect code for what I wanted, thanks a lot sir. It works like magic.
Sub Add_to_Selection()
If Not IsNumeric([E4]) Then
Exit Sub
Else
Dim cell As Range
For Each cell In Selection
cell.Value = cell.Value + [E4].Value
Next cell
End If
End Sub

Opening a new frame with a CommandButton in 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.

Stop macro to allow the workbook to be edited

I have written a code to open a workbook and filter data by the number in a particular column. I need to filter "0" first and then edit the data and then filter between "+8" and "-8".
I added a message box to pause the macro but I am not able to edit the opened workbook while the message box is displayed. I tried with "Application.waiting" option.
I need to pause the macro automatically and start it manually.
Sub Filter_data()
Workbooks.open"D:\Reposrts\AAA.csv"
Activesheet.Range("I1:I100").Autofilter field:=1,Criterial:="0"
Activesheet.columns("A:Z").Autofit
MsgBox"Task ok" 'Here I need to pause the macro and allow for edit opened wb and then manually start macro for below line'
Activesheet.Range("I1:I100").Autofilter field:=1,Criterial:=">8", Opersator:=xlAnd, Criterial:="<-8"
End Sub
Split your existing Macro into two parts
The first part contains everything up to have the MsgBox"Task ok" line
The second part contains everything after that point
Create a Modeless (or "Non-Modal") User Form with your label and an "OK" button to call the second part of your split macro
"Modeless" means you can edit the workbook while the User Form is open. MsgBox is "Modal", which is why you can't edit anything. More details available from Microsoft
Replace the MsgBox"Task ok" line in the first part of your split macro with a line that opens your User Form.
Once the first half-macro has finished, and the User Form is waiting for you to click "OK", you will be able to edit workbooks. Once you click "OK", the second part will start.
It is probably best to first consider if the "edits" you need the user to make are possible via VBA, a User Form, or a DialogBox (which include the likes of the "Select Range" DialogBox, or the "Colour Picker" DialogBox)
I would recommend you not to try to edit the Workbook while a macro is running- I don't even think that is possible at all. Furthermore, try not to refer to ranges by activating/selecting ranges.
Activesheet.Range("I1:I100").Autofilter 'Instead of this use the code below
With Workbooks("AAA.csv")
.Sheets("NAME").Range("I1:I100).Autofilter
'More code
End With
Like Chillin mentioned, you could assign hotkeys to your filter macros- split them in two. Other than that, you can use buttons to activate the macros.
To assign a keyboard shortcut to a macro:
Press ALT+F8 to open the macro dialog box. Select the macro, and click on Options. In the window that opens you can assign a keyboard shortcut to the macro you selected.

Locating Command Buttons in Excel VBA

I am maintaining a large spread sheet at work and there are ActiveX command buttons throughout the code. One sheet I am currently working on has almost 100 of these. I have made sure the button itself is visible as some of the buttons are hidden/unhidden depending on the flow of the code.
Is there a way I can find whereabouts on the sheet itself the button is located that the VBA code is pointing to? Here is a snippet:
enter code here
Private Sub CommandButton4_Click()
Application.Goto Range("Add_Trainees"), True
CommandButton3.Visible = True ' unhides menu button
CommandButton81.Visible = True ' hides SC1
CommandButton97.Visible = True ' hides SC2
End Sub
I am trying to find where on the sheet where command buttons 3, 81 and 97 are located.
TIA for any help/suggestions.
I think pnuts provides the correct way to find out where the buttons are.
I would also suggest to open the panel from "Developer" -> "Properties". In that panel, you can check out all the ActiveX Controls, including the command buttons, in the list.

Resources