Edit control text in custom toolbar is blank in Excel 2013 - excel

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

Related

Excel CustomUI Button Links

Ok, I know this is an ongoing problem, and I have searched seemingly everywhere for an answer. I apologize if I somehow missed an answer on here, so if you know of one, please advise!
I created a Custom Ribbon in Excel using the 'CustomUI Editor". My macros currently only run from buttons directly on the sheet, not on the UI. I read that it is because of the links to the original file. So I created a fresh new workbook, created new macros (not imported), created custom UI buttons, and linked the macros to the new buttons. The buttons still give me the error that the macro cannot be run and may not be available. What could I be missing here? I have also read that its possible to create a macro to update the links when the workbook is opened. Has anyone had success with this? I would love to have buttons on the ribbon and remove them from my sheet! Thanks for any help that you can provide!
You can use the following sub to add custom menu items. Just replace the .OnAction value with the names of the macros you'd like the control to run.
Sub AddCustomMenu()
Dim cbMainMenuBar As CommandBar
Dim iHelpMenu As Integer
Dim cbcCutomMenu As CommandBarControl
'Delete the menu item if it already exists. Use On Error Resume Next in case it doesn't exist.
On Error Resume Next
Application.CommandBars("Worksheet Menu Bar").Controls("&My Tools").Delete
On Error GoTo 0
Set cbMainMenuBar = Application.CommandBars("Worksheet Menu Bar") 'Set a CommandBar variable to Worksheet menu bar
iHelpMenu = cbMainMenuBar.Controls("Help").Index 'Return the Index number of the Help menu. We can then use this to place a custom menu before.
Set cbcCutomMenu = cbMainMenuBar.Controls.Add(Type:=msoControlPopup, Before:=iHelpMenu) 'Add a Control to the "Worksheet Menu Bar" before Help.
cbcCutomMenu.Caption = "&My Tools" 'Give the control a caption
'Working with our new Control, add a sub control and give it a Caption and tell it which macro to run (OnAction).
With cbcCutomMenu.Controls.Add(Type:=msoControlButton)
.Caption = "Run Macro 1"
.OnAction = "Macro1"
End With
With cbcCutomMenu.Controls.Add(Type:=msoControlButton)
.Caption = "Run Macro 2"
.OnAction = "Macro2"
End With
End Sub
If you'd like it to add your custom menu whenever you open the workbook, go to the ThisWorkbook module and add the following sub:
Private Sub Workbook_Open()
AddCustomMenu
End Sub

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

calling VBA forms from a button in 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

Userform showing when not wanted, not called for

im having some weird things happen with some code in excel
Private Sub CommandButton2_Click()
Dim thiswb As Workbook
Set thiswb = ActiveWorkbook
Call EnterDataToSS(thiswb)
Me.Hide
BeamNoFind.Show vbModal
End Sub
the called code basically drops some values into a spreadsheet.
any left over values are populated to LISTBOX1 on BeamNoFind Userform.
Then on the BeamNoFind userform there is a button and the LISTBOX1. when you select and item from listbox1, and click the button, a third userform opens (VBMODELESS) to allow placement of the value.
below is the code of the button to show the third userform.
Private Sub CommandButton2_Click()
Dim Selected_Length As String
Dim Selected_Name As String
Dim Selected_Length_index As Integer
Selected_Length_index = BeamNoFind.ListBox1.ListIndex
With BeamNoFind.ListBox1
If .ListIndex > -1 Then
Selected_Name = .Column(0, .ListIndex)
Selected_Length = .Column(1, .ListIndex)
CellInputForm.beam_length_label.Caption = Selected_Name & " [ " & Selected_Length & " ] "
BeamNoFind.Hide
'ChngDataSrc.Hide
'Unload ChngDataSrc
CellInputForm.Show vbModeless
Else
MsgBox "No selection", vbExclamation, "Oops!"
End If
End With
End Sub
the weird thing is, when i click the button to show my modeless userform, to place the data in a cell, the initial macro is being triggered that drops you into the first userform.
Where i have the commented code 'ChngDataSrc.Hide' and 'Unload ChngDataSrc' are my attempts to stop the userform from displaying when i dont want it to.
When i unload the form, i then get an error instead of the form displaying, the error is with the initial macro:
Sub get_scheduling_data(control As IRibbonControl)
ChngDataSrc.Show
End Sub
It has something to do with vbModeless because if i replace "vbModeless" from "CellInputForm.Show vbModeless" line with "vbModal", it shows correctly, without the unwanted form (ChngDataSrc). but then the function of the form (select cell, press ok button, value placed in selected cell) is gone.
I found a solution, but its not a real solution,
i placed
ChngDataSrc.Hide in the Activate sub of the CellInputForm userform.
So when CellInputForm.show vbModeless is run, and the ChngDataSrc userform pops up unwantedly, it is then hidden again.
Id rather find out why it is being showed in the first place, but this fix seems to work for now.

VBA to stop printing worksheet in excel 2010

I am preparing journal voucher in excel 2010. I want to minimize the error. Hence i need a VBA code which helps to stop printing voucher if to cells are not equal. Please help.
What you want to do is create an event. In the VBA editor, double click ThisWorkbook tab in the Project Explorer and enter this code:
Private Sub Workbook_BeforePrint(Cancel As Boolean)
If Sheets("Sheet1").Range("A1") <> _
Sheets("Sheet2").Range("A1") Then
MsgBox "Cannot print. Values do not match"
Cancel = True
End If
End Sub
You can edit the cells (A1 on both sheet1 and sheet2 in my example) to be whatever you need. Make sure you save the workbook and open it with macros enabled for it to be active.

Resources