I have a userform that contains two sets of comboboxes that depend on checkboxes each to be made visible. Upon the userform opening they are invisible. Upon checking the box the two fields become visible.
The comboboxes under the "CC MACH" checkbox work perfectly. They default to NOT visible upon loading the userform and only become visible upon checking the box...and become invisible if I uncheck the box.
But the comboboxes under neath "Label Printer", although they default to NOT visible upon loading, they do not become visible when I click the box...nor when I uncheck them.
The code that used for the CC MACH dependent boxes is the following:
Private Sub nd_ccmach_cx_Click()
If nd_ccmach_cx = True Then
nd_ccmach_cb.Visible = True
nd_ccip_lbl.Visible = True
nd_ccip_cb.Visible = True
Else
If nd_ccmach_cx = False Then
nd_ccmach_cb.Visible = False
nd_ccip_lbl.Visible = False
nd_ccip_cb.Visible = False
End If
End If
End Sub
The one for the Label Printer dependent boxes is as follows:
Private Sub nd_lblptr_cx_Click()
If nd_lblprt_cx = False Then
nd_lblptr_cb.Visible = False
nd_lblptr_usbip_lbl.Visible = False
nd_usbip_cb.Visible = False
Else
If nd_lblprt_cx = True Then
nd_lblptr_cb.Visible = True
nd_lblptr_usbip_lbl.Visible = True
nd_usbip_cb.Visible = True
End If
End If
End Sub
The Userform Initialize code is a follows:
nd_ccmach_cb.Visible = False
nd_ccip_lbl.Visible = False
nd_ccip_cb.Visible = False
nd_lblptr_cb.Visible = False
nd_lblptr_usbip_lbl.Visible = False
nd_usbip_cb.Visible = False
Can you please tell me why the "Label Printer" dependent boxes will not respond to the checkbox value?
Related
i have a toggle button that can hide me the chart but i want it to be more designed.
I would like that when the chart is hidden that the button text is 'Show' and backgroundcolor is Red and when he is show, the button is 'Hidden' and background color is Black.
This is my VBA code :
Option Explicit
Sub hidechart()
With ThisWorkbook.Worksheets("CA").Range("45:74,118:147, 192:220, 265:292")
.EntireRow.Hidden = Not .EntireRow.Hidden
End With
End Sub
If ToggleButton1.Value = True Then
Rows(4).EntireRow.Hidden = True
Rows(5).EntireRow.Hidden = True
Rows(6).EntireRow.Hidden = True
Else
Rows(4).EntireRow.Hidden = False
Rows(5).EntireRow.Hidden = False
Rows(6).EntireRow.Hidden = False
End If
So I have looked at various questions similar to this but they don't seem to work or are unanswered:
Run macro when slicer change
how to run a macro while clicking on a value in slicer in excel vba
My Question:
When i click one of the options in the slicer i want it to automatically run one of the macros but it doesn't work, when i run it manually it works, but clicking on the slicer's option doesn't
Sub updateFoodSlicer()
With ActiveWorkbook.SlicerCaches("Slicker_TABLE_StackChart")
.SlicerItems("food").Selected = True
.SlicerItems("fruit").Selected = False
.SlicerItems("drink").Selected = False
End With
With ActiveSheet.PivotTables(Foods_StackChart").PivotFields("FOODS")
.PivotItems(">0").Visible = False
.PivotItems(">2").Visible = False
.PivotItems(">4").Visible = False
.PivotItems(">6").Visible = True
.PivotItems(">8").Visible = True
End With
End Sub
Thats my macro, when i click run it works, but i want it so that when i click the option "food" from the slicer i want it to make columns >6, and >8 visible only
By checking out those above stackoverflow question I realized you need an event so I added that, but still it doesn't work:
Sub Worksheet_PivotTableUpdate(ByVal Target As PivotTable)
If ActiveWorkbook.SlicerCaches("Slicker_TABLE_StackChart").SlicerItems("food").Selected = True Then
Call updateFoodSlicer()
End If
End Sub
----------------------------
Sub updateFoodSlicer()
With ActiveWorkbook.SlicerCaches("Slicker_TABLE_StackChart")
.SlicerItems("food").Selected = True
.SlicerItems("fruit").Selected = False
.SlicerItems("drink").Selected = False
End With
With ActiveSheet.PivotTables(Foods_StackChart").PivotFields("FOODS")
.PivotItems(">0").Visible = False
.PivotItems(">2").Visible = False
.PivotItems(">4").Visible = False
.PivotItems(">6").Visible = True
.PivotItems(">8").Visible = True
End With
End Sub
EDIT:
Follow up, so I added into the Sheets tab so the macro would run, but now I get the error: method visible of object pivotitem failed
I have tried various solution from online, yet none seem to work
I have a userform with a combobox to select the training type for various employee classes.
When the user selects one of the options from the dropdown menu it runs the macro below.
Private Sub TrainingType_Selection_Change()
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Application.Calculation = xlCalculationManual
Dim TrainingType_Selection As String
TrainingType_Selection = Hiring_Validation_Form.TrainingType_Selection.Value
If TrainingType_Selection = "Sustain - 30" Or TrainingType_Selection = "Sustain - 60" Then
Hiring_Validation_Form.LinkingECPID_Selection.Visible = True
Hiring_Validation_Form.LinkingECP_Label.Visible = True
End If
If TrainingType_Selection <> "New Hire" Then
Hiring_Validation_Form.ReqReasonLv1_Selection.Visible = False
Hiring_Validation_Form.Label6.Visible = False
Hiring_Validation_Form.ReqReasonLv2_Selection.Visible = False
Hiring_Validation_Form.Label11.Visible = False
End If
End Sub
The problem I'm running into is that if someone makes a selection from the drop-down menu and then changes their mind and selects another value from the drop-down menu it isn't re-running the macro. For example they change it from the above "New Hire" to "Sustain - 30". I have a clear button on the userform, but that clears the entire form which would not be ideal in a situation where the user only wants to change one input, not completely start over.
How do I get the TrainingType_Selection_Change() macro to re-run again when the combobox selection is changed
This line:
Application.ScreenUpdating = False
disables screen updates even after the macro has finished running.
Add
Application.ScreenUpdating = True
as the last line before End Sub to re-enable them.
I was able to figure out a way to address this issue by using AfterUpdate()
I added the below macro and it now updates the entire form when selections are changed.
Private Sub TrainingType_Selection_AfterUpdate()
TrainingType_Selection = Hiring_Validation_Form.TrainingType_Selection.Value
Hiring_Validation_Form.LinkingECPID_Selection.Visible = False
Hiring_Validation_Form.LinkingECP_Label.Visible = False
Hiring_Validation_Form.ReqReasonLv1_Selection.Visible = True
Hiring_Validation_Form.Label6.Visible = True
Hiring_Validation_Form.ReqReasonLv2_Selection.Visible = True
Hiring_Validation_Form.Label11.Visible = True
Hiring_Validation_Form.Label21.Visible = True
Hiring_Validation_Form.NewHireSup_Selection.Visible = True
Call TrainingType_Selection_Change
End Sub
I have 5 checkboxes to set some options for a macro, one of them is a Select/Unselect All checkbox. I want to create something similar to what you have on web-based mailboxes when you select mails to delete or mark as read, etc.
When I check the Select/Unselect All checkbox, I turn the rest of the checkboxes's values to true and viceversa when I uncheck it. That's ok.
The problem comes when I also want to validate that if everything is unchecked and one by one I check the other checkboxes, if in the end I check all, then the Select/Unselect All checkbox turns to checked. And viceversa, meaning that if eveything is checked and then I uncheck one of the four others, then I turn the "All" checkbox to false (unchecked).
But it seems that even when I just set the value, for example Option1Checkbox.value = True, in the SelectAllCheckbox_Click event, it triggers both the Option1Checkbox_Click and Option1Checkbox_Change events.
Shouldn't it just trigger the Change event since I'm not actually clicking that checkbox?
What happens is that I check the SelectAll, so it turns Option1 to checked, but by doing this, Option1 triggers also the click event so it unchecks it which triggers the click event again and then unchecks again the All checkbox which in the end leaves everything unchecked, like at the beginning. Hope this part is clear enough.
How can I avoid this behavior? How can I make sure only the Change event is triggered and not the Click event?
Has anyone ever had such an arrangement of checkboxes and had a similar problem? Or how did you manage to do this without the behavior I'm getting?
The checkboxes are not on a form but simply on a worksheet. They are ActiveX controls. And what I have is nothing complicated:
Private Sub SelectAll_Click()
Option1Checkbox.Value = SelectAll.Value
Option2Checkbox.Value = SelectAll.Value
Option3Checkbox.Value = SelectAll.Value
Option4Checkbox.Value = SelectAll.Value
End Sub
Then the options checkboxes click events look like this:
Private Sub Option1Checkbox_Click()
If Option1Checkbox.Value = True And Option2Checkbox.Value = True And Option3Checkbox.Value = True And Option4Checkbox.Value = True Then
SelectAll.Value = True
Else
SelectAll.Value = False
End If
End Sub
It's quite simple, the biggest problem I see is the calls to the click events when the checkbox is not actually click.
Thanks for your help.
I would define a local variable (one defined outside the subs) and set/check that
Option Explicit
Dim ImChangingStuff As Boolean
Private Sub SelectAll_Click()
ImChangingStuff = True
Option1Checkbox.Value = SelectAll.Value
Option2Checkbox.Value = SelectAll.Value
Option3Checkbox.Value = SelectAll.Value
Option4Checkbox.Value = SelectAll.Value
ImChangingStuff = False
End Sub
then your click routines would look like this:
Private Sub Option1Checkbox_Click()
If ImChangingStuff Then Exit Sub
If Option1Checkbox.Value = True And Option2Checkbox.Value = True And Option3Checkbox.Value = True And Option4Checkbox.Value = True Then
SelectAll.Value = True
Else
SelectAll.Value = False
End If
End Sub
To solve this problem,
in your main userform (in this example it is called Mainform) add this code:
Private Sub Mainform_Initialize()
stateCheckbox = True
End Sub
Create a module to store your global variables (eg. globalVariables), and add the following, which will hold the state of the checkbox:
Public stateCheckbox as Boolean
In the _click event of your checkbox, wrap your code like this:
if stateCheckbox = false then
{ whatever your code is }
Else:
stateCheckbox = false
end if
I have had the same issue as described above. I recognized that each change of a Checkbox, no matter if by click or by changing Checkbox.Value, each time first fires the Checkbox_Change event and then the Checkbox_Click event , provided that any of these events are declared.
I would thus recommend to use only one event for each checkbox, p.e. the Checkbox_Change event, and to check if the checkbox has been changed by click or by declaration at the beginning of the Sub. This can be done by comparing the ActiveControl.Name with the Checkbox.Name:
Private Sub CheckBox1_Change()
On Error Goto Err:
If ActiveControl.Name = CheckBox1.Name Then
On Error Goto 0
'Commands for click
Exit Sub
Else
On Error Goto 0
'Commands for change from within the Userform
Exit Sub
Err:On Error Goto 0
'Commands for change from outside the Userform
End Sub
I would write a sub checking if Option1 to Option4 are all checked in an If statement. In a second if statemement, I'd check to see if they are all false:
Sub Are_O1_to_O4_All_Checked ()
If Option1Checkbox.Value = True And _
Option2Checkbox.Value = True And _
Option3Checkbox.Value = True And _
Option4Checkbox.Value = True Then
Option5Checkbox.Value = True ' select/unselect checkbox
End If
If Option1Checkbox.Value = False And _
Option2Checkbox.Value = False And _
Option3Checkbox.Value = False And _
Option4Checkbox.Value = False Then
Option5Checkbox.Value = False ' select/unselect checkbox
End If
End Sub
Then I would Call this sub at the end of each OnClick event for the check boxes.
I'm trying to change the value of my checkbox to true based on a another cell's value
if range("A1").value = "green" then
Checkbox1.value= true
end if
How to I change the value property to true for multiple checkbox at the same time
For some reason the code that i've tried doesn't do anything at all.
P.S. I'm using format checkboxes
This will change all Checkboxes
Sub Changeboxes()
Dim cb As CheckBox
If Sheet1.Range("a1").Value = "green" Then
For Each cb In Sheet1.CheckBoxes
cb.Value = True
Next cb
End If
End Sub
If you need to specify particular checkboxes, then
Sub ChangeSomeCbs()
If Sheet1.Range("a1").Value = "green" Then
Sheet1.CheckBoxes("Check Box 1").Value = True
Sheet1.CheckBoxes("Check Box 2").Value = False
Sheet1.CheckBoxes("Check Box 3").Value = True
End If
End Sub
Checkbox and Checkboxes are hidden properties. You won't get intellisense, but they work.
This works fine for me:
If range("O26").Value = "green" Then
CheckBox1.Value = True
CheckBox2.Value = True
End If
If you are in design mode this will not work.
This code is true for Office365:
If range("O26").Value = "green" Then
CheckBox1 = True
CheckBox2 = True
End If