I have the following code (credit Tim Williams, here Check box to select and deselect all other check boxes in spreadsheet)
Dim CB As checkbox
For Each CB In ActiveSheet.CheckBoxes
If CB.Name <> "Expand All Checkbox" Then
CB.Value = ActiveSheet.CheckBoxes("Expand All Checkbox").Value
End If
Next CB
It works perfectly using a checkbox for selecting / deselecting all other checkboxes, but each checkbox is assigned to another macro (the same macro, simply called "checkbox"). I would like to call the "checkbox" macro as each of the other checkboxes is selected / deselected - is that possible? If so, how?!
Dim CB As checkbox
For Each CB In ActiveSheet.CheckBoxes
If CB.Name <> "Expand All Checkbox" Then
CB.Value = ActiveSheet.CheckBoxes("Expand All Checkbox").Value
Call Checkbox
End If
Next CB
Try this, I believe this is what you are trying to do.
Related
I can't for the life of me find how to set a form control checkbox background to transparent
Dim cb As CheckBox
For Each cb In ActiveSheet.CheckBoxes
cb.Value = -4146
cb.Interior.Color = ???
Next cb
To be clear I'm talking about a Forms checkbox on a worksheet, not on a form and not an ActiveX checkbox (Developer menu > Insert > Form Controls).
I recorded a macro and got this but I'm wondering if there's a way to do it via the Checkbox object rather than use the Shape object. Even a link to the MSDN Dev page for a Form Controls checkbox would help as I can't find it!
ActiveSheet.Shapes("cb1_1").Fill.Visible = msoFalse
cb.Interior.ColorIndex = xlNone
I am creating a VBA macro to select all checkboxes in a sheet and it works fine but now i want to adjust my code to select and unselect just the checkboxes in a particular range.
Here is my code.
Sub Select_all()
Dim Cbox As CheckBox
Dim Rng As Range
Set Rng = ActiveWorkbook.Sheets("Sheet4").Range("B7, B104")
For Each Cbox In ActiveSheet.CheckBoxes
If Not Intersect(Cbox.TopLeftCell, Rng) Is Nothing Then
If Cbox.name <> ActiveSheet.CheckBoxes("Check Box 104").name Then
Cbox.Value = ActiveSheet.CheckBoxes("Check Box 104").Value
End If
End If
Next Cbox
End Sub
I added
If Not Intersect(Cbox.TopLeftCell, Rng) Is Nothing
but it doesn't change anything, and when i remove this part then it selects all the checkboxes in the sheet and i need only for the Range("B7:B104")
Any suggestions please ? Thank you very much.
This works for me. But I changed the cb# to "1" because I don't have 104 of them on my test sheet. Any cb's in range B7:B104 will be changed to the value of cb1, which you can change to cb104 in your script.
Sub Link_Checkboxes_To_Cells()
Dim cBox As CheckBox
For Each cBox In Sheet1.CheckBoxes
If Not Intersect(cBox.TopLeftCell, Range("B7:B104")) Is Nothing Then
If cBox.Name <> ActiveSheet.CheckBoxes("Check Box 1").Name Then
cBox.Value = ActiveSheet.CheckBoxes("Check Box 1").Value
End If
End If
Next cBox
End Sub
#JuniorDev
There are a few reasons why the above code doesn't work for you. I'll try to list them.
The above code works for "Form Control" checkboxes - Not ActiveX type checkboxes. You would need a different code for ActiveX checkboxes. Or you may not have a form checkbox named "Check Box 1". But I think that would give you an error. Or your other checkboxes may not be in the Range("B7:B104"). You can try changing that to Range("A1:Z10000") to insure your checkboxes are in the range.
If you are not sure what type checkboxes you have then you can run the following code to find their names and what type of control they are, either form or activex. It will print the info in your immediate window of the VB editor.
Sub CheckboxLoop()
'PARTIAL SOURCE: www.TheSpreadsheetGuru.com/the-code-vault
Dim cb As Shape
'Loop through Form Checkboxes
For Each cb In ActiveSheet.Shapes
If cb.Type = msoFormControl Then
If cb.FormControlType = xlCheckBox Then
Debug.Print "Form Control: " & cb.Name
End If
End If
Next cb
'Loop through ActiveX Checkboxes
Dim ChkBx As OLEObject
For Each ChkBx In ActiveSheet.OLEObjects
If TypeName(ChkBx.Object) = "CheckBox" Then
Debug.Print "ActiveX Control: " & ChkBx.Name
End If
Next ChkBx
End Sub
Sorry to resurrect an old post, but I hate it when a solution has not been provided and I know the answer, which is seldom. Thank you, this helped me on a project I am currently working on.
If you're like me, I change all my Sheet Names for sake of clarity.
For Each cBox In Sheet1.CheckBoxes
This line in John Muggins code limits you to the Sheet Name, "Sheet1".
I was able to change the line to...
For Each cBox In ActiveSheet.CheckBoxes
...and it worked perfectly.
I have a series of ComboBoxes that are populated based on the previous ComboBox selection. So for example, ComboBox3 is populated based on the value selected in ComboBox2. The "trigger" for populating ComboBox3 is the DropButtonClick action. I have a message box pop up when there is no value in ComboBox2. This is working successfully - code below.
If Me.ComboBox2.ListIndex = -1 Then
MsgBox "Please select all preceding comboboxes"
ComboBox3.Value = ""
Exit Sub
Else
sh.Range("B2") = Me.ComboBox2.Value
End If
My issues is once the Message Box appears (as a result of there being no value in ComboBox2) ComboBox3 still displays drops down values. Is there a way to undo the ComboBox3 DropButtonClick Event when there is no value in ComboBox2, so that ComboBox3 never drops down?
You can immediately close the drop-down window of the combo-box by simulating the "ESC" key:
MsgBox "Please select all preceding comboboxes"
ComboBox3.Value = ""
' close immediately the combo's dropdown window
SendKeys "{ESC}{ESC}"
Is this something for you?
If Me.ComboBox2.Value = "" Then
ComboBox2.SetFocus
Else
sh.Range("B2") = Me.ComboBox2.Value
End If
When you want to click on the dropbutton of combobox3 it automaticly goes back to combobox2.
I'm making an add records form for a spreadsheet of mine, and let's say that I want one of the controls to be a dropdown that is populated by unique entries under a certain column "type". However, I want to also make it such that the dropbox always has a initial option to "add new type" and upon such selection, it becomes a regular text box. How would I do this in VBA?
You cannot change a control type at run time. The easiest thing to do is create a combo box and a text box. Set the text box visibility to false. Then in the onchange event of the combo box your code will unhide the text box and hide the combo box. You will also need a save button so that when it is clicked it will add the option to the drop down, clear the text box, hide the text box, hide the button and unhide the drop down.
Okay, so here's my idea of how to tackle this.
Create 2 hidden elements (Visibility = False), one a TextBox and one a CommandButton.
Populate your ComboBox with the values from the sheet under column "type"
Add one more entry AddItem with wording such as "Add new item..."
When the user selects "Add new item...", change the Visibility of the TextBox & CommandButtons to True
When the user clicks the CommandButton, add the phrase to the column and add a new element to the ComboBox
I have created a mockup UserForm and code that does a little more than just this; it also styles the user entry to sentence case (consistency purposes) and checks to make sure the value isn't already in the column.
Excel Sheet with "type" column
UserForm with name labels
UserForm Code
Private Sub bAdd_Click()
Dim str As String
Dim rng As Range
Dim ro As Integer
'Makes sure there is an entry, adds it to the Sheet and then updates the dropdown
If Len(Me.tbNew) > 0 Then
'Converts user entry to "Sentance Case" for better readability
str = StrConv(Me.tbNew, vbProperCase)
'Finds out if the entry already exists
Set rng = Sheets(1).Range(Sheets(1).Cells(2, 1), Sheets(1).Cells(Sheets(1).Cells(Sheets(1).Rows.Count, 1).End(xlUp).Row, 1))
On Error Resume Next
Err.Number = 0
'Searches for duplicate; if found, then ListIndex of cbColor is modified without inserting new value (prevents duplicates)
ro = rng.Find(str, LookIn:=xlValues, LookAt:=xlWhole).Row
Debug.Print Err.Number
'Ensures a user doesn't add the same value twice
If Err.Number > 0 Then
Sheets(1).Cells(Sheets(1).Cells(Sheets(1).Rows.Count, 1).End(xlUp).Row + 1, 1) = str
Me.cbColor.AddItem StrConv(Me.tbNew, vbProperCase), Me.cbColor.ListCount - 1
Me.cbColor.ListIndex = Me.cbColor.ListCount - 2
Else
Me.cbColor.ListIndex = ro - 2
End If
'Resets and hides user form entries
Me.tbNew = vbNullString
Me.tbNew.Visible = False
Me.bAdd.Visible = False
End If
End Sub
Private Sub bClose_Click()
Unload Me
End Sub
Private Sub cbColor_Change()
'Visibility is toggled based on if the user selected the last element in the dropdown
Me.bAdd.Visible = Me.cbColor.ListIndex = Me.cbColor.ListCount - 1
Me.tbNew.Visible = Me.cbColor.ListIndex = Me.cbColor.ListCount - 1
End Sub
Private Sub UserForm_Initialize()
'Populate from the sheet
For a = 2 To Sheets(1).Cells(Cells(Sheets(1).Rows.Count, 1).End(xlUp).Row, 1).Row
Me.cbColor.AddItem Sheets(1).Cells(a, 1)
Next
'Add option for new type
Me.cbColor.AddItem "Add new type..."
End Sub
Currently I have 5 group boxes all filled with checkboxes, when I want to unselect all of them (for a 'clear selection' button), I use this code that I found on a forum:
For Each CheckBox In grpbox_Hiragana
CheckBox.checked = "false"
Firstly, I'm sure if this is the correct way to unselect the checkboxes, secondly the "grpbox_Hiragana" groupbox returns the following error:
Expression is of type 'System.Windows.Forms.GroupBox', which is not a collection type
If anyone could confirm this is the correct way of doing this/ help fix the error by telling me why the groupbox won't be accepted that would be great.
if you have all check box on one group box use this code :
Dim ChkBox As CheckBox = Nothing
' to unchecked all
For Each xObject As Object In Me.GroupBox1.Controls
If TypeOf xObject Is CheckBox Then
ChkBox = xObject
ChkBox.Checked = False
End If
Next
' to checked all
For Each xObject As Object In Me.GroupBox1.Controls
If TypeOf xObject Is CheckBox Then
ChkBox = xObject
ChkBox.Checked = True
End If
Next
Or you can use CheckedListBox Control.
A alternative with less lines of code is:
For Each ChkBox As CheckBox In GroupBox1.Controls
ChkBox.Checked = False
Next
Incidentally your code would have worked if you added .controls, the As CheckBox just enables the intellisense (and also ensures it is only Checkbox's that are processed) .