Updating checkbox value within CheckBox1_Click event runs code twice - excel

I want the checkbox stays the same and generates a message box.
Private Sub CheckBox1_Click()
Dim beast As String
beast = Range("k1").Value
If beast = "No" Then
CheckBox1 = False
MsgBox "You Are Missing Ingredients", vbCritical, "Missing Ingredients"
End If
End Sub

Disable application events. Application events for ActiveX controls are trapped by the code sheet belonging to the worksheet on which the control has been installed. Therefore the code below must be on that code sheet, not a standard code module where it will never run.
Private Sub CheckBox1_Click()
Dim beast As String
With CheckBox1
If .Value = True Then
beast = Range("K1").Value
If beast = "No" Then
MsgBox "You Are Missing Ingredients", vbCritical, "Missing Ingredients"
' prevent the upcoming change from triggering this event
Application.EnableEvents = False
.Value = False
Application.EnableEvents = True
End If
End If
End With
End Sub
Make sure that your check box is an ActiveX control. In fact, once you create such a control it will be listed in the controls drop-down on the top left of the sheet's code module and VBA will create or select the click event procedure for you.
I have tested the above code and it doesn't run more thna once on one click. I have added a check to let it do nothing if the checkmark was removed rather than set.

You can use a static boolean variable to avoid the double execution, like so:
Private Sub CheckBox1_Click()
Static isBusy As Boolean
Dim beast As String
If Not isBusy Then
isBusy = True
beast = Range("k1").Value
If beast = "No" Then
CheckBox1.Value = False
MsgBox "You Are Missing Ingredients", vbCritical, "Missing Ingredients"
End If
isBusy = False
End If
End Sub
Static variables maintain their values across invocations, so if you set it in the first CheckBox1_Click invocation, you can check it in the second one and know a first invocation is ongoing.

Related

Either of the checkbox can only be true

Situation
I have two form control check boxes. I am trying to write a code that will allow only either of them to be true.
my code is
Sub CheckBox2_Click()
If CheckBox1.Enabled = True Then
CheckBox2.Enabled = False
Else
If CheckBox2.Enabled = True Then
CheckBox1.Enabled = False
End If
End If
End Sub
I have this code in module and have assigned the same macro for both the checkboxes. I get run-time error 424. I beleive this is very basic problem but I unable to dela with it.
Thank you
Please, test the following way. Form check boxes do not have a click event, as ActiveX ones have. You should associate the next sub to both of them. The check boxes I tested, have their names as "Check Box 1" and "Check Box 2". You have to change yours according to the reality in your case, Please, copy the next code in a standard module and then associate it to both used check boxes:
Option Explicit
Sub FormCheckBoxChange()
If ActiveSheet.CheckBoxes(Application.Caller).value = 1 Then
Select Case Application.Caller
Case "Check Box 1": ActiveSheet.CheckBoxes("Check Box 2").value = -4146
Case "Check Box 2": ActiveSheet.CheckBoxes("Check Box 1").value = -4146
End Select
End If
End Sub
Use instead of the used check box names, the appropriate ones for your case.
In case of Form text boxes, their value is not True and False as in case of ActiveX ones. It is 1 and -4146...
Are you sure you want to enable/disable the checkboxes.
Following code makes sure that either one of both boxes is checked.
Public Sub checkbox2_onClick()
Dim oCb1 As Object
Set oCb1 = Table1.Shapes("Checkbox1").OLEFormat.Object
Dim oCb2 As Object
Set oCb2 = Table1.Shapes("Checkbox2").OLEFormat.Object
If oCb2.Value = 1 Then oCb1.Value = 0
End Sub
Public Sub checkbox1_onClick()
Dim oCb1 As Object
Set oCb1 = Table1.Shapes("Checkbox1").OLEFormat.Object
Dim oCb2 As Object
Set oCb2 = Table1.Shapes("Checkbox2").OLEFormat.Object
If oCb1.Value = 1 Then oCb2.Value = 0
End Sub

Autofill cells based on drop down list in excel

I am trying to creata a VBA that gives me automatic values based on drop down list in a form. The problem is that when I run the macro then it is causing an error and excel stops working. Any help in this case is most welcome.
Private Sub Worksheet_Change(ByVal Target As Range)
If Range("$G$11") = "UD Sheet" Then
Rows("20:25").EntireRow.Hidden = False
Else
Rows("21:25").EntireRow.Hidden = True
End If
If Range("G12").Value = "Flex Tape" Then
Range("B20").Value = "None"
Else
Range("B20").Value = ""
End If
exitHandler:
Application.EnableEvents = True
Exit Sub
End Sub
First thing first, in your code, no need to put an Exit Sub before the End Sub.
The code will end after that line so this is a redundancy.
The next thing that you need to understand is that the Change Event will keep triggering if you will not disable it explicitly. So it means that when you hide a row on that Sheet, the Change Event will keep on happening since there will be changes that will happen on the Sheet. i.e. (Hiding Rows).
To do that you need to disable the EventsListeners of the application using the Application.EnableEvents = False. So the application can do a single thing based on that first event.
The next thing that you need to keep in mind is to track where the Changes occur and fire your program. Target is a Range Object that will return the Range where the specific change occurs on the Sheet.
In order to do that, you need to validate if you need to trigger the routine based on the target using the Intersect function.
The whole code is as follows:
Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = False
If Not Intersect(Target, Range("G11")) Is Nothing Then
If Range("$G$11") = "UD Sheet" Then
Rows("20:25").EntireRow.Hidden = False
Else
Rows("21:25").EntireRow.Hidden = True
End If
End If
If Not Intersect(Target, Range("G12")) Is Nothing Then
If Range("G12").Value = "Flex Tape" Then
Range("B20").Value = "None"
Else
Range("B20").Value = ""
End If
End If
Application.EnableEvents = True
End Sub

Is it possible to hook CommandBarComboBox.Change event such that when a change has been made from Excel VBA code it fires event?

I have a ComboBox control on a custom CommandBar. I want to detect when a change has been made to the ComboBox and run some code.
This works fine when the user changes the value in the ComboxBox, but if some other VBA code changes it, the Event is not fired.
I have taken the code from here: https://learn.microsoft.com/en-us/office/vba/api/office.commandbarcombobox.change
and modified it slightly to delete a CommandBar with the same name so I can run it a number of times. I have then added another function to change the value of the ComboBox programmatically. When I change the value programmatically it can be seen to change on the Excel GUI, but the event code does not fire.
I have also tried the easier OnAction method, rather than hooking events, both seem to give the same result.
In a ClassModule called ComboBoxHandler I have the following code:
Private WithEvents ComboBoxEvent As Office.CommandBarComboBox
Public Sub SyncBox(box As Office.CommandBarComboBox)
Set ComboBoxEvent = box
If Not box Is Nothing Then
MsgBox "Synced " & box.Caption & " ComboBox events."
End If
End Sub
Private Sub Class_Terminate()
Set ComboBoxEvent = Nothing
End Sub
Private Sub ComboBoxEvent_Change(ByVal Ctrl As Office.CommandBarComboBox)
Dim stComboText As String
stComboText = Ctrl.Text
Select Case stComboText
Case "First Class"
FirstClass
Case "Business Class"
BusinessClass
Case "Coach Class"
CoachClass
Case "Standby"
Standby
End Select
End Sub
Private Sub FirstClass()
MsgBox "You selected First Class reservations"
End Sub
Private Sub BusinessClass()
MsgBox "You selected Business Class reservations"
End Sub
Private Sub CoachClass()
MsgBox "You selected Coach Class reservations"
End Sub
Private Sub Standby()
MsgBox "You chose to fly standby"
End Sub
In a module I have the following:
Private ctlComboBoxHandler As New ComboBoxHandler
Sub AddComboBox()
Set HostApp = Application
On Error Resume Next
CommandBars("Test CommandBar").Delete
Dim newBar As Office.CommandBar
Set newBar = HostApp.CommandBars.Add(Name:="Test CommandBar", Temporary:=True)
Dim newCombo As Office.CommandBarComboBox
Set newCombo = newBar.Controls.Add(msoControlComboBox)
With newCombo
.AddItem "First Class", 1
.AddItem "Business Class", 2
.AddItem "Coach Class", 3
.AddItem "Standby", 4
.DropDownLines = 5
.DropDownWidth = 75
.ListHeaderCount = 0
End With
ctlComboBoxHandler.SyncBox newCombo
newBar.Visible = True
End Sub
Sub test()
Dim newBar As Office.CommandBar
Set newBar = Application.CommandBars("Test CommandBar")
Dim cmbox As Office.CommandBarComboBox
Set cmbox = newBar.Controls(1)
cmbox.Text = "Business Class" ''<< I was hoping this would fire an event, but it does not! Same is true if I
End Sub
First I run AddComboBox and the events work fine if I manually change the ComboBox.
Then I run test() and the value displayed inside the ComboBox changes, but the event does not fire.

VBA: hide and unhide code not working

I'm new to VBA and have been trying to write a code that hides and unhides rows based on the input value of a certain cell address. However, it doesn't work and I don't why. I have posted my code below:
Sub Hide()
If Worksheets("IS").Range("B8").Value = "Show All" Then
Worksheets("IS").Rows("12:165").EntireRow.Hidden = False
End If
If Worksheets("IS").Range("B8").Value = "Just Revenue" Then
Worksheets("IS").Rows("12:165").EntireRow.Hidden = False
Worksheets("IS").Rows("28:165").EntireRow.Hidden = True
End If
If Worksheets("IS").Range("B8").Value = "Just Expenses" Then
Worksheets("IS").Rows("12:165").EntireRow.Hidden = False
Worksheets("IS").Rows("12:27").EntireRow.Hidden = True
Worksheets("IS").Rows("160:165").EntireRow.Hidden = True
End If
If Worksheets("IS").Range("B8").Value = "Just Cogs" Then
Worksheets("IS").Rows("12:165").EntireRow.Hidden = False
Worksheets("IS").Rows("12:27").EntireRow.Hidden = True
Worksheets("IS").Rows("64:165").EntireRow.Hidden = True
End If
If Worksheets("IS").Range("B8").Value = "Just Totals" Then
Worksheets("IS").Rows("12:165").EntireRow.Hidden = False
Worksheets("IS").Rows("12:25").EntireRow.Hidden = True
Worksheets("IS").Rows("28:61").EntireRow.Hidden = True
Worksheets("IS").Rows("64:91").EntireRow.Hidden = True
Worksheets("IS").Rows("93:155").EntireRow.Hidden = True
End If
End Sub
Any help with why my code doesn't work or any tips to improve it would be much appreciative.
Rewriting for Worksheet_Change:
In your VBE, paste this code into the code sheet for the "IS" worksheet (double click it in the Project - VBAProject pane. If the Project - VBAProject pane is not visible in your VBE, go to View>>Project Explorer):
Private Sub Worksheet_Change(ByVal Target As Range)
'Ensure that we don't trigger another change event while this code is running
Application.EnableEvents = False
'Check if cell B8 triggered this change:
If Not Intersect(Target, Range("B8")) Is Nothing Then
'B8 changed... which means B8 is "Target" variable
Select Case Target.Value
Case "Show All"
Worksheets("IS").Rows("12:165").EntireRow.Hidden = False
Case "Just Revenue"
Worksheets("IS").Rows("12:165").EntireRow.Hidden = False
Worksheets("IS").Rows("28:165").EntireRow.Hidden = True
Case "Just Expenses"
Worksheets("IS").Rows("12:165").EntireRow.Hidden = False
Worksheets("IS").Rows("12:27").EntireRow.Hidden = True
Worksheets("IS").Rows("160:165").EntireRow.Hidden = True
Case "Just Cogs"
Worksheets("IS").Rows("12:165").EntireRow.Hidden = False
Worksheets("IS").Rows("12:27").EntireRow.Hidden = True
Worksheets("IS").Rows("64:165").EntireRow.Hidden = True
Case "Just Totals"
Worksheets("IS").Rows("12:165").EntireRow.Hidden = False
Worksheets("IS").Rows("12:25").EntireRow.Hidden = True
Worksheets("IS").Rows("28:61").EntireRow.Hidden = True
Worksheets("IS").Rows("64:91").EntireRow.Hidden = True
Worksheets("IS").Rows("93:155").EntireRow.Hidden = True
End Select
End If
'Turn events back on so this code triggers again
Application.EnableEvents = True
End Sub
There are quite a few events that we can hook VBA to (SelectionChange, DoubleClick, Workbook_Close, etc). In this case we are hooking to Worksheet_Change().
This code gets triggered every time this worksheet experiences a change. The Target variable will hold the range that triggered the event. So we test to see if that Target intersects with Range("B8") which means B8 was changed. Then we perform the code inside the If block.
I switched your If/ElseIf over to a Select/Case just because it makes for cleaner code since we are testing a single condition (the value of B8) over and over again.
In this code we also toggle off the Excel Applications EnableEvents feature. This feature is what allowed this Worksheet_Change() to get triggered in the first place. Often times in the code we make more changes to the worksheet (hiding rows or columns, for instance) which will trigger the application to run Worksheet_Change() again... while it's running Worksheet_Change() already. This can cause code to run superfluously and, often, cause an endless loop that makes excel crash.
This code needs to be pasted on the sheet where you want are wanting to execute the code. You will not need to qualify your ranges with the sheets once the code is there as well.
You can just refer directly to your range without the Worksheets("IS"). as so:
Rows("so and so").EntireRow.Hidden = True
You can also just refer to your TargetRange by variable now like so:
If MyTarget = "Just Revenue" Then
I inserted one of your conditions in the code as an example
Option Explicit
Private Sub worksheet_change(ByVal target As Range)
Dim MyTarget As Range
Set MyTarget = Range("B8")
If Not Intersect(target, MyTarget) Is Nothing Then
Application.EnableEvents = False
'Your CODE HERE
If MyTarget = "Show All" Then
Rows("12:165").EntireRow.Hidden = False
End If
Application.EnableEvents = True
End If
End Sub

Only allowing one checkbox to be checked for a userform

So I'm making a userform and I have to use make a group of mutually exclusive checkboxes or only allow the user to pick one. "But just use option buttons!" you cry. Two problems with that:
I already have a separate set of option buttons in the userform (I believe you can somehow group them to allow multiple sets but I am unfamiliar with how to actually do this).
My professor specifically wants checkboxes
so I attempted to solve this problem like this
If CheckBoxBar.Value = True And CheckBoxatm.Value = True Then
GoTo Here:
End If
If CheckBoxatm.Value = True And CheckBoxmmHg.Value = True Then
GoTo Here:
End If
If CheckBoxatm.Value = True And CheckBoxpsia.Value = True Then
GoTo Here:
End If
If CheckBoxBar.Value = True And CheckBoxmmHg.Value = True Then
GoTo Here:
End If
If CheckBoxBar.Value = True And CheckBoxpsia.Value = True Then
GoTo Here:
End If
If CheckBoxmmHg.Value = True And CheckBoxpsia.Value = True Then
GoTo Here:
End If
The here leads to a message box that re initializes the userform after the msg box says "You are only allowed to select one" with code like this
Here: MsgBox "You are only allowed to select on pressure unit."
The code "works" but it always goes to the Here: statement despite only picking one of the checkboxes. Can you spot anything wrong?
Thanks for the help!
As stated by Doug Glancy in a comment, your current code is probably missing an Exit Sub before the label Here:, which is therefore allowing your code to fall into the section after the label.
Another way of achieving what you are after is just to have one If statement which checks if more than one CheckBox is checked, and then display the MsgBox if so, e.g. as follows:
If CheckBoxBar.Value + _
CheckBoxatm.Value + _
CheckBoxmmHg.Value + _
CheckBoxpsia.Value < -1 Then
MsgBox "You are only allowed to select one pressure unit."
Exit Sub
End If
Or you can rely on the .Value being the default property of a CheckBox and thus "reduce" that code to:
If CheckBoxBar + CheckBoxatm + CheckBoxmmHg + CheckBoxpsia < -1 Then
MsgBox "You are only allowed to select one pressure unit."
Exit Sub
End If
Note: This method won't work if the .TripleState property of the CheckBox is set to True. (My thanks to Comintern for pointing that out.)
You can make the checkboxes act like option buttons if you override the click function and clear the other boxes (although you can also have none selected).
Private Sub CheckBoxatm_Click()
If Me.Controls("CheckBoxatm").Value = True Then Call ClearOtherValues("CheckBoxatm")
End Sub
Private Sub CheckBoxBar_Click()
If Me.Controls("CheckBoxBar").Value = True Then Call ClearOtherValues("CheckBoxBar")
End Sub
Private Sub CheckBoxmmHg_Click()
If Me.Controls("CheckBoxmmHg").Value = True Then Call ClearOtherValues("CheckBoxmmHg")
End Sub
Private Sub CheckBoxpsia_Click()
If Me.Controls("CheckBoxpsia").Value = True Then Call ClearOtherValues("CheckBoxpsia")
End Sub
Private Function ClearOtherValues(cb As String)
Dim cbPressure() As String, i As Long
cbPressure = Split("CheckBoxBar,CheckBoxatm,CheckBoxmmHg,CheckBoxpsia", ",")
For i = 0 To UBound(cbPressure)
If cbPressure(i) <> cb Then Me.Controls(cbPressure(i)).Value = False
Next i
End Function

Resources