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.
Related
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
Hey, I have an UserForm into which an user inputs varius profile
fields.
Once it's filled in, there is verification - if anything goes astray,
the CommandButton, named save_button is disabled
What I want to achieve is: If user clicks on the button, whilst it is in the disabled state, to display a MsgBox saying he needs to correct the incorrectly filled in field
For Demonstration purposes, I'm not gonna paste here the validation procedures, so let's just pretend that the save_button.Enabled = False is set from the getgo. Produces the same result.
save_button.Enabled = False ' already ran before, pretend this executes it
Private Sub save_button_Click()
If save_button.Enabled = False Then
MsgBox "Clicked disabled button"
End If
End Sub
Issue is, once a CommandButton is set to .Enabled = False then it can no longer be officially clicked (hence it can't even trigger the Click() procedure)
My next thought was to use the MouseUp as a substitute. Issue is,
this triggers on any miniscule movement over the button and I don't want to bombard the user with MsgBoxes
Can you think of any alternatives, as to how to detect if user clicked the disabled button?
When a control is disabled, the click event bubbles up the tree. In your case, I guess the user form will get the click instead. If you put your save button inside a frame, that will get the click if the button is disabled. It is fairly easy to make the frame invisible, by setting
Caption to ""
BorderStyle to fmBorderStyleNone
SpecialEffect to fmSpecialEffectFlat
And then size the frame so that is the same size as the button.
The code is easy:
Private Sub YourNewFrame_Click()
MsgBox "Save button disabled!"
End Sub
Tip: If you draw the frame, cut your button and paste it inside your new frame, it will be placed properly. Properly, as in in the right part of the hierarchy. Visually, you will have to do manually.
I want to offer another approach based on my comment to your question.
In the below gif, you will see that the 'Create Form' does not become enabled until all the necessary information is provided. You don't see it in the gif so much, but each entry that requires validation also has it behind the scenes in code.
The essential code behind that action is this:
Sub checkFields()
Select Case True
Case Len(Me.formTitleLine1) = 0, Len(Me.formPrefix) = 0, Len(Me.formNumber) = 0, Len(Me.formProduct) = 0, Len(Me.formEditionMonth) = 0, Len(Me.formEditionYear) = 0
Me.createForm.Enabled = False
Case Else
Me.createForm.Enabled = True
End Select
End Sub
And it's called on the afterUpdate event of each relevant box:
Private Sub formEditionYear_AfterUpdate()
checkFields
End Sub
With one small change to the checkFields sub, you can only show the save button once everything has been filled in correctly. That change would be:
Me.createForm.Visible
instead of
Me.createForm.Enabled
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?
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.
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 ...