Excel - macro attached to form Control Button not activating - excel

I have an odd issue with a single command button refusing to run the macro attached. I get the message "Cannot run the macro "". The macro may not be available in this workbook or all macros may be disabled."
I have three buttons created by code on a new sheet and the macros assigned. The first two buttons work perfectly but the third does not. If I re-assign the same macro to the button (right click - assign macro) it works fine but this will not accomplish what I need.
All macros etc have been enabled. I hope someone can help, this is doing my head in!
Sub AddButton()
Dim ButtonName1, ButtonName2, ButtonName3 As String
ActiveSheet.Buttons.Add(200, 5, 81, 36).Select
ButtonName1 = Selection.Name
Selection.OnAction = "CopyBack"
ActiveSheet.Shapes(ButtonName1).TextFrame.Characters.Text = "Modify Scenario (Copy back)"
ActiveSheet.Buttons.Add(285, 5, 81, 36).Select
ButtonName2 = Selection.Name
Selection.OnAction = "GotoPlanner"
ActiveSheet.Shapes(ButtonName2).TextFrame.Characters.Text = "Return To Shift Planner"
ActiveSheet.Buttons.Add(370, 5, 81, 36).Select
ButtonName3 = Selection.Name
Selection.OnAction = "DellCurrSheet"
ActiveSheet.Shapes(ButtonName3).TextFrame.Characters.Text = "Delete This Scenario"
ActiveSheet.Range("A1").Select
End Sub

Apologies if I have wasted anyones time on this but I have identified the typo which was causing the issue with the macro name assignment.

Related

Hide/unhide multiple shapes Excel VBA

I am very new to excel VBA & macros and I am trying to do something that I think is very simple, but I cannot for the life of me figure it out.
I have a shape ("shape 1") that when clicked should show/unhide two shapes ("shape 2" and "shape 3").
By default "shape 2" and "shape 3" should be hidden and only appear when "shape 1" is selected.
Any help will be much appreciated, remembering I am a complete novice here!
Edit:
I have managed to use the below, essentially a copy paste from here, I dont know what it means but it works for the single button. I cant figure out how to extend the code to include multiple objects being shown/hidden. An example of a second object to be shown/hidden at the same time as "july_2022" is "august_2022".
Public HIDE As Boolean
Sub fy ()
ActiveSheet.Shapes("july_2022").Visible = HIDE
If ActiveSheet.Shapes("july_2022").Visible = False Then
HIDE = True
Else
HIDE = False
End If
End Sub
ActiveSheet.Shapes("july_2022").Visible = HIDE is the part that sets the visibility of
a shape (july_2022). Another identical line but with something other than july_2022 would affect a second shape. The rest of the code (If.. Then.. Else.. End If) could be replaced with HIDE=Not(HIDE).
For example, the following code when run will 'toggle' the visibility of two shapes on the Active Sheet called 'Shape2' and 'Shape3'.
Public HIDE As Boolean
Sub fy()
ActiveSheet.Shapes("Shape2").Visible = HIDE
ActiveSheet.Shapes("Shape3").Visible = HIDE
HIDE = Not (HIDE)
End Sub

Excel Interop - Display full screen error?

I'm using Excel Introp for a .net application to embed an Excel workbook in a windows userform.
excApp.Visible = False
wb = excApp.Workbooks.Open(directOpenPath) ' , [ReadOnly]:=False, IgnoreReadOnlyRecommended:=True)
wb.Saved = True ' necessary to prevent Excel has stopped working error and to not actually do a save
hwnd1 = CType(excApp.Hwnd, IntPtr)
SetParent(excApp.Hwnd, pnlExcel.Handle)
SendMessage(excApp.Hwnd, WM_SYSCOMMAND, SC_MAXIMIZE, 0)
MoveWindow(hwnd1, 0, 0, pnlExcel.Width, pnlExcel.Height, True)
excApp.Visible = True
There are ActiveX buttons on the worksheet that work fine when I open using the above code.
However, it is my desire to make the object look less like Excel so I wanted to hide the ribbon, formula bar, etc.
I added the line: excApp.DisplayFullScreen=True. Upon doing that, clicking on a button no longer kicked off the associated macro. Additionally, I could no longer click on any cell in the worksheet. This seems like odd behavior but I have traced it down to the DisplayFullScreen line.
Also I am trying to include the following:
excApp.ActiveWindow.DisplayWorkbookTabs = False
excApp.ActiveWindow.DisplayHeadings = False
excApp.ActiveWindow.DisplayGridlines = False
But I get a object not set to reference error. If I add excApp.ActiveWindow.Activate, the worksheet "locks up" again and I can't click on any cells.
Any ideas?

Set Button to Unlocked

SO I have a sheet that protect and unprotects in VBA. This works fine for the most part EXCEPT for my form reset button, which does not allow anyone to click the button (coded below).
Is there a way to set a property in the button to unlocked? So that when the macro prior to it creates it and then locks the sheet, you will still be able to click it?
With ActiveSheet.Buttons.Add(545, 42, 72, 18)
.Name = "NEWNCR"
.Caption = "New NCR #"
End With
ActiveSheet.Shapes.Range(Array("NEWNCR")).Select
Selection.OnAction = "populatencrnumber"
Maybe something like
ActiveSheet.Shapes.Range(Array("NEWNCR")).Locked = False
This exact example doesn't work of course. But something like that exists, right? Any help would be appreciated.

Excel VBA Calling the Calendar via Command Button in a form

I have just added in Calendar 12.0 from the tools > Additional Controls. Calendar works fine and I have it spitting the date out to the right cells. What I would like, however, is to really make the calendar visible from a command button as my form contains a bunch of fields and I don't want to bog up the form with this calendar. I have tried Calendar1.show but the .show isn't an option.
So ultimately I need a command button to show the calendar, allow the user to select (I have that) and then close the calendar. Any help? I thank you in advance!!
bdubb
In this snippet, CommandButton1 is from the ActiveX controls, not the form controls. It requires that you click the button to show the calendar (which pops up next to the button you clicked), and click the button again to hide the calendar.
Private Sub CommandButton1_Click()
If Not Calendar1.Visible Then
Calendar1.LinkedCell = "A1"
Calendar1.Top = Sheet1.CommandButton1.Top
Calendar1.Left = Sheet1.CommandButton1.Left + Sheet1.CommandButton1.Width + 1
Calendar1.Visible = True
Else
Calendar1.Visible = False
End If
End Sub
Obviously, different buttons would require different linked cells, but it does mean that you could have a single calendar control that it displyed by multiple buttons (if that is what you want).
Unfortunately, it would appear that you cannot hide the control while any of its events are firing (e.g AfterUpdate). It just doesn't want to disappear!!
Hide/Close a calendar control still not works (new year 2015 = almost four years later) but I think I found a workaround to hide the control after firing events.
I have a Calendar1_AfterUpdate(), which launches before Calendar1_Click(). Code is placed directly in a worksheet and NOT in a module.
Private Sub Calendar1_AfterUpdate()
Range("a1") = Me.Calendar1.Value
' Next two lines does not work within AfterUpdate
' When running step by step it seems to work but the control is
' visible when End Sub has run
Me.Calendar1.Visible = True
Me.Calendar1.Visible = False
End Sub
To that I simply added
Private Sub Calendar1_Click()
Me.Calendar1.Visible = True
Me.Calendar1.Visible = False
End Sub
Please note that the control for some reason must be made visible before hiding.
Why it does not work directly in Calendar1_AfterUpdate() is a mystery to me.
Next problem is to hide the control when I remove the mouse. Mouse-events seems to be impossible in a calendar control ...

Can't close userform

Let me set up the environment.
This is VBA code running in Excel.
I have a userform that contains a msflexgrid. This flexgrid shows a list of customers and the customer', salesperson, csr, mfg rep, and territories, assignments. When you click in a column, let's say under the Territory column, another userform opens to show a list of Territories. You then click on the territory of your choice, the userform disappears and the new territory takes the place of the old territory.
This all works great until you click on the territory of your choice the 'Territory' userform does not disappear (it flickers) and the new territory does not transfer the underlying userform.
I should mention that when I'm stepping through the code it works great.
I'm assuming it has something do to with the flexgrid as all the other userform (that don't have flexgrids) that open userform work just fine.
Following is the some code sample:
** Click event from flexgrid that shows Territory userform and assignment of new territory when territory userform is closed.
Private Sub FlexGrid_Customers_Click()
With FlexGrid_Customers
Select Case .Col
Case 0
Case 2
Case 4
Case 6
UserForm_Territories.Show
Case Else
End Select
If Len(Trim(Misc1)) > 0 Then
.TextMatrix(.Row, .Col) = Trim(Misc1)
.TextMatrix(.Row, .Col + 1) = Trim(Misc2)
End If
End With
End Sub
** The following Subs are used in the Territory userform
Private Sub UserForm_Activate()
Misc1 = ""
Misc2 = ""
ListBox_Territory.Clear
Module_Get.Territories
End Sub
Private Sub UserForm_Terminate()
Set UserForm_Territories = Nothing
End Sub
Private Sub ListBox_Territory_Click()
With ListBox_Territory
Misc1 = Trim(.List(.ListIndex, 0))
Misc2 = Trim(.List(.ListIndex, 1))
End With
Hide
UserForm_Terminate
End Sub
I know this a long winded explanation but I'm a fairly decent VBA programmer and this has me stumped.
Any help would be greatly appreciated.
I'm not going to say what you're doing is wrong (in that it won't ever work), but it scares the heck out of me. This is not the way I'd deal with forms.
Firstly, you're using UserForm_Territories (the class/form name) to refer to an implicitly-created instance of the form. This is something I've always avoided doing. I would always create an instance of a form explicitly, so instead of:
UserForm_Territories.Show
I would do:
Dim oTerritoriesForm As UserForm_Territories
Set oTerritoriesForm = New UserForm_Territories
oTerritoriesForm.Show vbModal
' get the values from the form here
Unload oTerritoriesForm
Next, and much more worryingly, you're subverting the UserForm_Terminate behaviour by calling it explicitly. Why you're doing this I can't imagine, unless you thought that it would work around your stated problem. My advice: don't do that.
Worse, you're attempting to assign to the implicitly-created instance of the form within that Terminate method. You shouldn't be doing that, either. I'm surprised that even compiles.
It seems like you're trying to force the implicitly-created instance of the form to mimic an explicitly-created one. In which case, create it explicitly, as shown above.

Resources