calling VBA forms from a button in Excel - excel

I have a macro that parses through a large file in Excel. I created a form that allows me to search for and provide a summary of specific messages. I can put
FormName.Show
at the end of the macro and it works. The form displays as the parsing macro completes. But I don't always want the form to appear and sometimes I want to call it again after I've saved the spreadsheet. So I wrote another function that can create a button that can open the form. In the .OnAction statement I have OnAction = "FormName.Show"
Sub Create_Button()
ActiveSheet.Buttons.Add(437.25, 72, 125.25, 47.25).Select
Selection.OnAction = "FormName.Show"
Selection.Characters.Text = "Search Messages"
End Sub
This doesn't work, it created the button but when I click on the button I get
"Cannot run the macro "xxxx.xlam'!FormName.Show' The macro may not be available in this workbook or all macros may be disabled.
Why does it work in the main macro but not in the button OnAction?
Thanks

Per MSDN, Shape.OnAction requires a macro name, not VBA code. You have tried to pass it VBA code. Instead try this:
Selection.OnAction = "showForm"
....
End Sub
Public Sub showForm()
FormName.Show
End Sub

.OnAction will not only run Macros not it will not evaluate code. Paste thiss in a standard module.
Sub ShowUserform()
FormName.Show
End Sub
Sub Create_Button()
With ActiveSheet.Buttons.Add(437.25, 72, 125.25, 47.25)
.OnAction = "ShowUserform"
.Characters.Text = "Search Messages"
End With
End Sub

Related

Private Sub Opens Hyperlink Twice instead of Once

I have a pretty simple macro, I thought would be cheeky to add a macro to a Resume where upon opening the document it also pulls up my LinkedIn. However, the macro opens the same link on two tabs, is there something I'm missing?
Private Sub Document_Open()
ActiveDocument.FollowHyperlink ("[My link would be here]")
End Sub
The Excel Version of this Macro:
Sub Workbookbook_Open()
ActiveWorkbook.FollowHyperlink ("[My link would be here]")
End Sub
From the docs:
ActiveDocument.FollowHyperlink _
Address:="https://www.Microsoft.com", _
NewWindow:=True, AddHistory:=True
only opens one tab.

VBA does not active

I suggest that my workbook contains a VBA code below. But It does not run when I opened my workbook too.
Sub Appl_StatusBar()
Application.StatusBar = "SupportABC"
End Sub
If you put your code in the Workbook Open Event it will do what you need. To do this click the top dropdown where it says "(General)" and hit "Workbook". In the right dropdown select "Open" and save your code there". See below
Private Sub Workbook_Open()
Application.StatusBar = "SupportABC"
End Sub

Edit control text in custom toolbar is blank in Excel 2013

I've got Excel addin with a vba edit control on a toolbar. This has worked for a long time under Excel 2003-2010. But under Excel 2013, the text property is always empty regardless of what gets typed into the edit control. Code is similar to the code below.
If I load the Addin as an .XLS, it works. But if I save it as a .XLA and try that, then it fails.
Sub test()
Set myControl = CommandBars("test").Controls.Add(Type:=msoControlEdit)
With myControl
.Caption = Search
.OnAction = "tester"
End With
End Sub
Sub tester()
MsgBox "I am gonna serach for: " & CommandBars("Test").Controls(1).Text
CommandBars("Test").Controls(1).Text = ""
End Sub

where will I put the codes when button is clicked?

i've created a macro to create a button when the sheet is activated. then i called it using the codes below: But everytime i go to different worksheet then back again to the sheet containing this button the macro does its job. i just want the macro to work when i clicked the button
Private Sub Worksheet_Activate()
Call sortData
End Sub
here's the code for macro that i've created:
Sub sortData()
'
'
'
'
ActiveSheet.Buttons.Add(689.25, 59.25, 133.5, 30).Select
Selection.OnAction = "sortData"
Selection.Characters.Text = "Sort Data"
With Selection.Characters(Start:=1, Length:=28).Font
.Name = "Times New Roman"
.FontStyle = "Bold"
.Size = 12
End With
Range("A1").Select
End Sub
now my problem is where will i put the codes shown below when this button is click? or simply how can i make this button worked?? i tried to put the codes in the same sheet where i called the macro but it is not working.
Sub sortData_Click() 'did i call the button right? but it is not working when i us it
'codes here
End Sub
in the VBA Editor, click insert new module (or you can just put this in the same module where your sortData is) then write this code into your new module:
Sub procedureName() 'procedure name is exactly the same as what you named in Selection.OnAction = "sortData"
'codes here
End Sub
what you actually done is making the sortData() call itself because you have set the Selection.OnAction = "sortData" ->>> change this name to whatever procedure name you want to perform

Excel VBA: Workbook_Open

I'm using Workbook_Open to call a userform when the application is opened, this is working fine. However I would like it to only run the first time it is opened.
I tried this and it work if I run the sub from the editor but not when I open the file.
Sub Workbook_Open()
If Worksheets("DataSheet").Range("A1").Value = "" Then
QuickStartForum.Show
End If
End Sub
Note: A1 contains a value that will be populated after the user form has run
It appears that the problem is that it opens the user form before the data is loaded into the worksheet.
Is this there a way around this or do I need to take a different approach ?
I think it is because you have this code in a Module. You need to put the code within 'ThisWorkBook'.
I tried this following code and had no issues when it was in the 'ThisWorkBook' it failed to run within 'Module1'
Private Sub Workbook_Open()
If Worksheets("DataSheet").Range("A1").Value = "" Then
QuickStartForum.Show
Worksheets("DataSheet").Range("A1").Value = "filled" ' <-- this fills the cell with data for testing, so that when you reopen the file it should not re-open the userform
Else
MsgBox ("not shown because the A1 cell has data")
End If
End Sub

Resources