I'm trying to have a "select all" check box, however when all the check boxes are selected they do not get actioned.
The fields from Sheet1 do not get imported or set to Null. On their own checkboxes import data from Sheet1 into position, but do not work under master select all checkbox1 on / off commands.
This is my code so far:
Sub SelectAll_CHECK_BOX()
Dim CB As CheckBox
For Each CB In ActiveSheet.CheckBoxes
If CB.Name <> "Check Box 1" Then
CB.Value = ActiveSheet.CheckBoxes("Check Box 1").Value
End If
Next CB
End Sub
And other checkBoxes have the following code with different range values and Sub names for each checkbox.
Sub CHECK_BOX_PRODUCT_NAME()
Dim CB As CheckBox
For Each CB In ActiveSheet.CheckBoxes
If CB.Name <> ActiveSheet.CheckBoxes("Check Box 1").Name And CB.Value <> ActiveSheet.CheckBoxes("Check Box 1").Value And ActiveSheet.CheckBoxes("Check Box 1").Value <> 2 Then
ActiveSheet.CheckBoxes("Check Box 1").Value = 2
Exit For
Else
ActiveSheet.CheckBoxes("Check Box 1").Value = CB.Value
End If
Next CB
Dim xChk As CheckBox
Set xChk = ActiveSheet.CheckBoxes(Application.Caller)
With xChk.TopLeftCell.Offset(0, 3)
If xChk.Value = xlOff Then
.Value = Null
Else
.Value = Sheets("Sheet1").Range("B9")
End If
End With
End Sub
Here is a visualization of the problem:
You should try adding Call CHECK_BOX_PRODUCT_NAME to your first macro. just before End Sub.
As first macro is only checking the second box and not running the code for 2nd check box.
Related
As you can see in the below image, I have several checkboxes representing measures and the last two are supposed allow for additional measures. The textboxes on the front allow the user to specify the name of their measure.
I was able to successfully send the name (caption) of the ticked checkboxes to a Listbox using the following code:
Dim ctrl As Control
For Each ctrl In Me.Controls
If TypeName(ctrl) = "CheckBox" Then
If ctrl.Value = True Then
ListBox1.AddItem ctrl.Caption
End If
End If
Next ctrl
However, if the user selects the "Additional measures" checkbox I want what they inputted in the textboxes to be added to the list box, instead of the checkbox caption.
Is there any way I can do that?
To keep the code simple, you may want work with Tags instead of Captions
Private Sub CommandButton1_Click()
Dim ctl As Control
For Each ctl In Me.Controls
If TypeName(ctl) = "CheckBox" Then
If ctl.Value And ctl.Tag <> "" Then
ListBox1.AddItem ctl.Tag
End If
End If
Next ctl
End Sub
Private Sub TextBox1_Change()
Me.CheckBox4.Tag = Me.TextBox1.Text
End Sub
Private Sub TextBox2_Change()
Me.CheckBox5.Tag = Me.TextBox2.Text
End Sub
Edit: Added the condition And ctl.Tag <> ""
So I tried using more if statements as SJR suggested and it worked.
Dim ctrl As Control
For Each ctrl In Me.Controls
If TypeName(ctrl) = "CheckBox" Then
If ctrl.Value = True Then
If ctrl.Caption = "Additional Metric 1 (Specify Metric)" Then
ListBox1.AddItem TextBox3.Value
ElseIf TypeName(ctrl) = "CheckBox" Then
If ctrl.Value = True Then
If ctrl.Caption = "Additional Metric 2 (Specify Metric)" Then
ListBox1.AddItem TextBox4.Value
Else
ListBox1.AddItem ctrl.Caption
End If
End If
End If
End If
End If
Next ctrl
Is there any way to simplify this? :)
I'm using this macro to select a column range of checkboxes if a particular box is checked, and to uncheck all remaining checkboxes.
Sub SelectAll_Click_Formale_Denkstörungen()
Dim CB As CheckBox
Dim Nicht_Vorhanden As Range
Dim leichtgradige As Range
Dim mittelgradige As Range
Dim schwergradige As Range
Dim keineAussage As Range
Set Nicht_Vorhanden = Hoja2.Range("E26:E38")
Set leichtgradige = Hoja2.Range("F26:F38")
Set mittelgradige = Hoja2.Range("G26:G38")
Set schwergradige = Hoja2.Range("H26:H38")
Set keineAussage = Hoja2.Range("I26:I38")
For Each CB In Hoja2.CheckBoxes
If Not Intersect(CB.TopLeftCell, Nicht_Vorhanden) Is Nothing Then
CB.Value = Hoja2.CheckBoxes("Check Box 513").Value
End If
If Not Intersect(CB.TopLeftCell, leichtgradige) Is Nothing Then
CB.Value = -4146
End If
If Not Intersect(CB.TopLeftCell, mittelgradige) Is Nothing Then
CB.Value = -4146
End If
If Not Intersect(CB.TopLeftCell, schwergradige) Is Nothing Then
CB.Value = -4146
End If
If Not Intersect(CB.TopLeftCell, keineAussage) Is Nothing Then
CB.Value = -4146
End If
Next CB
End Sub
I'm using this very same macro in several instances of the worksheet and most of them work fine, but for a few, it leaves some of the checkboxes it's supposed to check unchecked, but it will work properly after the worksheet has been open for a few minutes. Does anyone know why this may be happening?
I am trying to create something like a "Mastercheckbox" that will automatically check all other checkboxes on my worksheet. So I'm trying to start the following VBA Code as soon as I click click on the "Mastercheckbox" I am using Form Tools rather than Active X to create my checkboxes in order to make sure my VBA is compatible with any machine (I read that Active X might cause some issues) and have got the following code:
Sub Mastercheckbox()
Dim Chk As CheckBox
With ActiveSheet.Shapes("Mastercheckbox")
If .ControlFormat.Value = False Or .ControlFormat.Value = True Then
If .ControlFormat.Value = False Then
For Each chk In ActiveSheet.CheckBoxes
If Not chk.Name = "Mastercheckbox" Then
chk.Value = False
End If
Next chk
End If
If Not .ControlFormat.Value = True Then
For Each chk In ActiveSheet.CheckBoxes
If Not chk.Name = "Mastercheckbox" Then
chk.Value = True
End If
Next chk
End If
Else: MsgBox ("couldn't check value of mastercheckbox")
End If
End With
End Sub
Everything in the For Loops works, but checking the value of the Mastercheckbox just isn't working for some reason and it jumps straight to the Else case. Can anyone help me out?
being not an ActiveX control you must check its value against xlOn(corresponding to 1) and xlOff `(corresponding to -4146)
you may also refactor a little your code as follows:
Option Explicit
Sub Mastercheckbox()
With ActiveSheet.Shapes("Mastercheckbox")
If .ControlFormat.Value = xlNone Then
MsgBox ("couldn't check value of mastercheckbox")
Else
HandleCheckBoxes .ControlFormat.Value
End If
End With
End Sub
Sub HandleCheckBoxes(val As Long)
Dim Chk As CheckBox
For Each Chk In ActiveSheet.CheckBoxes
If Not Chk.name = "Mastercheckbox" Then Chk.Value = val
Next Chk
End Sub
Since you have decided to use User_Form checkbox, you need to define your variables as Shape. And in order to check their value, you need to check if they are xlOn or xlOff.
In order to see that you read the value of "Mastercheckbox" once it's pressed, you can use the piece of code below:
Dim MasterCB As Shape
Set MasterCB = ActiveSheet.Shapes("Mastercheckbox") '<-- set the MasterCB variable
If MasterCB.ControlFormat.Value = xlOn Then
MsgBox "Is checked"
ElseIf MasterCB.ControlFormat.Value = xlOff Then
MsgBox "Not checked"
End If
Code for your post
Option Explicit
Sub Mastercheckbox()
Dim Chk As Shape
Dim MasterCB As Shape
' set the MasterCB variable
Set MasterCB = ActiveSheet.Shapes("Mastercheckbox")
' loop through all shapes in ActiveSheet
For Each Chk In ActiveSheet.Shapes
If Chk.Type = msoFormControl Then '<-- check your shape type is User_Form type
If Chk.FormControlType = xlCheckBox Then '<-- check if the shape is a checkbox
If Chk.Name <> "Mastercheckbox" Then
Chk.ControlFormat.Value = MasterCB.ControlFormat.Value '<-- modify all checkboxes to the value of "Mastercheckbox"
End If
End If
End If
Next Chk
End Sub
I use the following code to automatically generate a new checkbox and link a cell to it:
ActiveSheet.CheckBoxes.Add().LinkedCell = Selection.Address
I want to create another sub which should change the background color of the .LinkedCell cell of the checkbox when the checkbox is checked (blue) or unchecked (red). I will have about 200 checkboxes in the worksheet.
Is there a way to get/return the .LinkedCell address of the currently checked/unchecked checkbox so that the sub can change the background color of that cell?
For example let's presume that the .LinkedCell is the cell in which the checkbox was initially placed. This would be the initial status of each checkbox:
and this would be the result after the user checks/unchecks the checboxes:
until now I used this code to change the background color of the checkbox itself. But I don't want that, I want to change the color of the .LinkedCell.
Sub SetMacro()
Dim cb
For Each cb In ActiveSheet.CheckBoxes
If cb.OnAction = "" Then cb.OnAction = "CheckedUnchecked"
Next cb
End Sub
and
Sub CheckedUnchecked()
With ActiveSheet.Shapes(Application.Caller).DrawingObject
If .Value = 1 Then
.Interior.ColorIndex = 5
Else
.Interior.ColorIndex = 3
End If
End With
End Sub
You could replace your CheckedUnchecked code with the following:
Sub CheckedUnchecked()
With ActiveSheet.Range(ActiveSheet.CheckBoxes(Application.Caller).LinkedCell)
If .Value Then
.Interior.ColorIndex = 5
Else
.Interior.ColorIndex = 3
End If
End With
End Sub
I am working in Excel 2010.
I have set up 10 form control checkboxes in my worksheet, and I want to automatically select a certain set of the checkboxes. All previous posts that I have seen on this topic cover selecting all checkboxes or unselecting all checkboxes.
Here is the VBA code from a previous post for unselecting all checkboxes:
Sub clearcheck()
Dim sh As Worksheet For Each sh In Sheets
On Error Resume Next
sh.CheckBoxes.Value = False
On Error GoTo 0 Next sh
End Sub
Here is the updated code based on this chain, but it is still running into a syntactical issue as well as an unidentified sub:
Sub highengagedonline()
Dim cb As CheckBox, sht As Worksheet
Set sht = Worksheets("Graph")
For Each cb In sht.CheckBoxes
If cb.Name = "Check Box 35" or _
cb.Name = "Check Box 36" or _
cb.Name = "Check Box 37" or _
cb.Name = "Check Box 38" or _
cb.Name = "Check Box 39" Then
cb.Value = 1
Else: cb.Value = 0
End If
Next cb
End Sub
Try something along the lines of:
Sub ClearCheck()
Dim cb As CheckBox, sht As Worksheet
Set sht = Worksheets("Sheet1")
For Each cb In sht.CheckBoxes
If cb.Name = "Check Box 1" Then
cb.Value = 1
Else if cb.Name = "Check Box 2" Then
cb.Value = 0
End If
Next cb
End Sub