I am trying to unlock a combobox when a option button is enabled. I am using the following code in a module but when I run the code and select the option the combo box remains locked
If Custom = True Then
FromMonth.Locked = False
FromYear.Locked = False
ToMonth.Locked = False
ToYear.Locked = False
End If
I am hoping to find a way to simultaneously click the option button in the userform and have it unlock the combo boxes next to them
You can use the option button change event, and make sure to add the code into userform code module and not the normal module
Your code can be something like this
Private Sub Custom_Change()
FromMonth.Locked = Not Custom.Value
FromYear.Locked = Not Custom.Value
ToMonth.Locked = Not Custom.Value
ToYear.Locked = Not Custom.Value
End Sub
Related
i have an excel file like the following:
[enter image description here]enter image description here
[1]: https://i.stack.imgur.com/QVe6i.png
this checkbox is being controlled by a trigger and the code is as follows:
Sub CheckBox1_Click()
If Range("CheckBox1").Value = True Then
Range("ET").Rows.Hidden = True
Else:
Range("ET").Rows.Hidden = False
End If
If Range("CheckBox1").Value = True And Range("CheckBox1a").Value = True Then
Range("A50:A53").Rows.Hidden = False
Range("A61:A63").Rows.Hidden = False
Else:
Range("A50:A53").Rows.Hidden = True
Range("A61:A63").Rows.Hidden = True
End If
End sub
if the value of the checkbox is true, it hides rows "A50:A53" and"A61:A63" and unhides when the value is False. works perfect!
On the Same sheet, i have a dropdown that does the same stuff based on the selected option: following is the excel file:
[1]: https://i.stack.imgur.com/EvCt0.png
The same as the CheckBox, the dropdown option also, hides rows "A209:A210", if yes is selected. the triggers for the dropdown is as follows:
Private Sub Worksheet_Change(ByVal Target As Excel.Range)
' Section 4. Testing part 1
If Target.Column = 53 And Target.row = 208 Then
If Target.Value = "Yes" Then
Range("Addition").Rows.Hidden = False
Range("A112:A113").Rows.Hidden = True
ElseIf Target.Value = "No" Then
Range("A112:A113").Rows.Hidden = False
Range("Addition").Rows.Hidden = True
End If
End If
End sub
The issue now is. When i start to work with the CheckBoxes, they work fine however the Dropdown doesn't respond and the same thing happens if the start to work with the dropdown, the checkboxes stop to respond.
Any suggestion or help is highly appreciated!
Instead of CheckBox1_Click try the CheckBox1_Change event. This will ensure the checkbox is updating every time the value changes, even if if doesn't gain or lose focus.
Other than that, you will need to have something in Workbook_Open that checks the default status of those checkboxes and ranges, hiding and showing rows as necessary.
You will also need a separate event just for CheckBox1a. Each CheckBox_Change script only triggers based on the named object in the script's title. For example CheckBox1_Change will only trigger for changes to a matching CheckBox1 object. So if you want things to happen when the user clicks on CheckBox1a then you need a CheckBox1a_Change script.
Finally, you are retrieving and comparing checkbox values using Range("CheckBox1") which is not intended. There is a built in method for accessing these objects through VBA. Each CheckBox is added to the Worksheet object as a member. In the sheet's code module you can write Me.CheckBox1.Value. In an external code module you could write Sheets("Sheet1").CheckBox1.Value.
I'm trying to disable (or hide) specific right click options on the Excel sheet/tab menu. I don't want to disable the entire menu per this thread.
I've tried in both Excel 2010 & 2016, same result. The code runs fine but the options in the sheet right click menu are still enabled. I've tried using the control's name and ID to no avail.
Private Sub Worksheet_Activate()
Application.CommandBars("cell").Controls("cut").Enabled = False 'Works
With Application.CommandBars("Ply")
.Controls("Insert...").Enabled = False
.Controls("&Select All Sheets").Enabled = False
.Controls("Select All Sheets").Enabled = False
.FindControl(ID:=946).Enabled = False '&Select All Sheets ID:946
'.Enabled = False 'Works but don't want entire menu disabled
End With
End Sub
After code, options are still enabled:
Thanks in advance.
I have a userform with a multitab containing 8 tabs. I require a user to use checkboxes on tab 6. I'm try to disable the user from selecting the next tab (tab 7) if Checkbox 1 and Checkbox 2 are left unchecked. They should only be able to access the next tab if they select one of the two checkboxes.
Private Sub MultiPage1_Change()
If Failed1.Value = False Or Passed1.Value = False Then
Me.MultiPage1.Pages(7).Enabled = False
Exit Sub
ElseIf Failed1.Value = True Or Passed1.Value = True Then
Me.MultiPage1.Pages(7).Enabled = True
End If
End Sub
I can't quite get the code to work the way I want it to. Any help would be greatly appreciated.
FYI - I'm a VBA n00b.
Me.MultiPageName.Pages(index).enabled = false
Me = the form active where multipage is present
MulitipageName = named multipage
Index = place of the page to enable copied from:http://www.excelforum.com/excel-programming-vba-macros/534056-is-it-possible-to-disable-a-tab-page-on-a-multipage-userform.html
I want to disable a button with VBA code like this:
ActiveSheet.Shapes("Button 1").ControlFormat.Enabled = False
I tried:
Set b1 = ActiveSheet.Buttons("Button 1")
b1.Enabled = False
And:
Me.Shapes("Button 1").ControlFormat.Enabled = False
My button name is correct, because it doesn't give me an error message, so the code is completely run through.
After this script I can click on that button and the assigned macro runs. Nothing should happen when I click on it.
Disabling a Form button (not talking ActiveX here) does not prevent the assigned macro to run and does not gray out the button. The code below does exactly that based on the version got from Excel. If you did not assign a name to your Form button, you can also use (Buttons(1).
If Excel version = 16 or higher the button is "enabled" by making it black and assigning my macro, else the button is "disabled" by making it gray and assigning no action to it.
Code can e.g. reside in Private Sub Worksheet_Activate() within sheet "Test Sheet"
If Application.Version < 16 Then
Worksheets("Test Sheet").Buttons("button_name").Font.Color = 8421504
Worksheets("Test Sheet").Buttons("button_name").OnAction = ""
Else
Worksheets("Test Sheet").Buttons("button_name").Font.Color = 0
Worksheets("Test Sheet").Buttons("button_name").OnAction = "'Name of the workbook.xlsm'!my_macro_name"
End If
Probably you are using ActiveX Button. Try this:
Sheets("Sheet1").CommandButton1.Enabled = False '--->change sheet name as required
EDIT:
______________________________________________________________________________
For a Form control Button the following line
ActiveSheet.Shapes("Button 1").ControlFormat.Enabled = False
disables the button i.e. click event will no longer work but the appearance of the button does not change which gives an impression that the button is still active. So work around for that is to change the color of the text of the button as follows:
Sub disable_button_2()
Dim myshape As Shape: Set myshape = ThisWorkbook.Worksheets("Sheet1").Shapes("Button 2")
With myshape
.ControlFormat.Enabled = False '---> Disable the button
.TextFrame.Characters.Font.ColorIndex = 15 '---> Grey out button label
End With
End Sub
And to bring back button to its original state write:
Sub activate_button_2()
Dim myshape As Shape: Set myshape = ThisWorkbook.Worksheets("Sheet1").Shapes("Button 2")
With myshape
.ControlFormat.Enabled = True '---> Enable the button
.TextFrame.Characters.Font.ColorIndex = 1 '---> Highlight button label
End With
End Sub
I suggest to create a shadow button/shape with exactly same size/position, but different color (fill and/or text to your liking) and no macro/action attached. Then just change the .visible property of your primary shape. Visible = button is active; not visible button is e.g. grayed out and has no action/is passive.
Only tested on Excel 2016 x86
I continued to receive errors utilizing .ControlFormat. solutions.
After much searching I found another solution that worked great for my needs of disabling Shapes/Buttons.
To mimic the .Enabled property of a uf control, you might toggle the .OnAction property of a shape.
Function ShapeIsEnabled(aShape As Shape) As Boolean
ShapeIsEnabled = (aShape.OnAction <> "")
End Function
Sub EnableShapeMacro(aShape As Shape)
aShape.OnAction = aShape.AlternativeText
End Sub
Sub DisableShapeMacro(aShape As Shape)
aShape.AlternativeText = aShape.OnAction
aShape.OnAction = vbNullString
End Sub
Note the use of the .AlternativeText property to store the macro name.
source: mikerickson
https://www.excelforum.com/excel-programming-vba-macros/1267897-disable-action-of-macro-enabled-shape.html#post5080833
For my form I need to hide or disable a textbox with a on-click delete macro attached to it when the sheet is protected. I'm talking about excel's build-in protection system. I've looked at several tutorials but I can't seem to get it to work properly.
I tried multiple things including this:
If ActiveSheet.ProtectContents = True Then
TextBox1.Visible = False
Else
TextBox1.Visible = True
End If
Any idea how to do this?
Change
If ActiveSheet.ProtectContents = True Then
TextBox1.Visible = False
Else
TextBox1.Visible = True
End If
To
If ActiveSheet.ProtectContents = True Then
ActiveSheet.TextBox1.Visible = False
Else
ActiveSheet.TextBox1.Visible = True
End If
You are not declaring where the textbox is. This will fix it
It is not clear whether the textbox is embedded in the worksheet or in a userform.
If in the worksheet is it a text box based on a shape? These are created from the INSERT tab on the Ribbon.
Or is this an ActiveX textbox? These are created from the DEVELOPER tab on the Ribbon.
If on the other hand, you are referring to a textbox on a userform then you can use the following code (assuming the userform code name is UserForm1):
UserForm1.Controls("NameOfYourTextBox").Visible = Not ActiveSheet.ProtectContents