I have a list in excel with an autofilter and where every row has a checkbox.
I have made a button with a macro to select all the checkboxes. This works fine. But when I filter my rows my 'select all' has to select only the visible checkboxes.
With my code it still selects all the checkboxes. Has anybody an idea to solve this?
My code:
Sub SelectAll()
Dim chk As CheckBox
If Worksheets("Summary").FilterMode = True Then
MsgBox "Filter mode is on"
Else
MsgBox "Filter mode is off"
For Each chk In Worksheets("Summary").CheckBoxes
chk.Value = Checked
Next
End If
End Sub
Thanks in advance
I'm taking it that your checkboxes are being hidden by the filtering.
This might help, inside your loop:
Dim chkRng As Range
Set chkRng = chk.TopLeftCell
Let addr = chkRng.Address ' for debugging to verify the cell the checkbox is associated with
Dim visr As Range
Set visr = chkRng.SpecialCells(xlCellTypeVisible)
Set ans = Intersect(visr, chkRng)
If Not ans Is Nothing Then
MsgBox ("visible")
End If
Related
I have this code but it only work for my first row.
It is suppose to look if the checkbox on B, C or D is checked, and if so, a date + username will automaticaly fill in F and G.
here is a picture of my table:
This is what my code looks like:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Range("B2") Or Range("C2") Or Range("D2") = True Then
Range("G2").Value = Environ("Username")
Range("F2").Value = Date
Else
Range("F2:G2").ClearContents
End If
End Sub
Enter this code in a regular module, select all your checkboxes and right-click >> assign macro then choose ReviewRows.
This will run the check whenever a checkbox is clicked - a bit of overhead since all rows will be checked, but should not be a big deal.
Sub ReviewRows()
Dim n As Long
For n = 1 To 100 'for example
With Sheet1.Rows(n)
If Application.CountIf(.Cells(2).Resize(1, 3), "TRUE") > 0 Then
If Len(.Cells(6).Value) = 0 Then 'only enter if currently empty?
.Cells(6) = Date
.Cells(7) = Environ("Username")
End If
Else
.Cells(6).Resize(1, 2).ClearContents
End If
End With
Next n
End Sub
If you want to be more precise then Application.Caller will give you the name of the checkbox which was clicked, and you can use that to find the appropriate row to check via the linkedCell.
Sub ReviewRows()
Dim n As Long, shp As CheckBox, c As Range, ws As Worksheet
Set ws = ActiveSheet
On Error Resume Next 'ignore error in case calling object is not a checkbox
Set shp = ActiveSheet.CheckBoxes(Application.Caller) 'get the clicked checkbox
On Error GoTo 0 'stop ignoring errors
If Not shp Is Nothing Then 'got a checkbox ?
If shp.LinkedCell <> "" Then 'does it have a linked cell ?
With ws.Range(shp.LinkedCell).EntireRow
If Application.CountIf(.Cells(2).Resize(1, 3), "TRUE") > 0 Then
If Len(.Cells(6).Value) = 0 Then 'only enter if currently empty?
.Cells(6) = Date
.Cells(7) = Environ("Username")
End If
Else
.Cells(6).Resize(1, 2).ClearContents
End If
End With
End If 'has linked cell
End If 'was a checkbox
End Sub
However this appraoch is sensitive to the exact positioning of your checkbox
You have a long way to go!
Unfortunately, If Range("B2") Or Range("C2") Or Range("D2") = True Then is beyond repair. In fact, your entire concept is.
Start with the concept: Technically speaking, checkboxes aren't on the worksheet. They are on a layer that is superimposed over the worksheet. They don't cause a worksheet event, nor are they responding to worksheet events. The good thing is that they have their own.
If Range("B2") Or Range("C2") Or Range("D2") = True Then conflates Range with Range.Value. One is an object (the cell), the other one of the object's properties. So, to insert sense into your syntax it would have to read, like, If Range("B2").Value = True Or Range("C2").Value = True Or Range("D2").Value = True Then. However this won't work because the trigger is wrong. The Worksheet_Change event won't fire when when a checkbox changes a cell's value, and the SelectionChange event is far too common to let it run indiscriminately in the hope of sometimes being right (like the broken clock that shows the correct time twice a day).
The answer, therefore is to capture the checkbox's click event.
Private Sub CheckBox1_Click()
If CheckBox1.Value = vbTrue Then
MsgBox "Clicked"
End If
End Sub
Whatever you want to do when the checkbox is checked must be done where it now shows a MsgBox. You can also take action when it is being unchecked.
I have the following in my userform:
CheckBox11 CheckBox12 CheckBox13 CheckBox14 CheckBox15
CheckBox21 CheckBox22 CheckBox23 CheckBox24 CheckBox25
Also, in my excel I have following named ranges:
range11 range12 range13 range14 range15
range21 range22 range23 range24 range25
I want to loop through each of the checkboxes, and in case of True, do something with the range, Say copy-paste the corresponding ranges to another location.
I have a nested For loop, i = 1 to 2
and j = 1 to 5
and then inside another long variable nm = i*10+j
now I want to refer to the CheckBox and Range with nm.
Any other alternative is also welcome.
Thanks in Advance.
Maybe something like this ?
Sub Test()
For Each objControl In ActiveSheet.OLEObjects
If TypeName(objControl.Object) = "CheckBox" And objControl.Object = True Then
rngName = Replace(objControl.Name, "CheckBox", "range")
Range(rngName).Select
MsgBox rngName & " selected"
End If
Next
End Sub
The sub will select the named range at each selected CheckBox (True value).
But that's if the checkbox is an ActiveX and the named range is within the same sheet where the checkboxes are.
Anyway, you can just change the Range(rngName).Select .... MsgBox rngName & " selected" line to whatever process you want to do with the corresponding named range.
Below if the checkbox is in a Userform:
Private Sub CommandButton1_Click()
For Each ctrl In Me.Controls
If TypeName(ctrl) = "CheckBox" And ctrl = True Then
rngName = Replace(ctrl.Name, "CheckBox", "range")
Range(rngName).Select
MsgBox rngName & " selected"
End If
Next
End Sub
I have a form created in excel which has rows [10:48] hidden and I want to make so that when you click a checkbox rows [10:48] are unhidden. I assigned a macro to the checkbox and using this formula:
Private Sub CheckBox45_Click()
If CheckBox45 = True Then
[10:48].EntireRow.Hidden = False
Else: [10:48].EntireRow.Hidden = True
End If
End Sub
When I click the checkbox nothing happen, but when I unhide the rows and click the checkbox it hides the rows. Which makes me think that only one of the actions is working. Is there a way to fix this?
Thanks in advance for the help.
Don't know if this matters but the form checkbox is in column D row 6
This assumes you are hiding/unhiding rows on Sheet 1 and the checkbox belongs to sheet 1 of the workbook, then:
Private Sub CheckBox30_Click()
If ThisWorkbook.Sheets(1).CheckBoxes("Check Box 30").Value = 1 Then
ThisWorkbook.Sheets(1).Rows("10:48").Hidden = true
Else
ThisWorkbook.Sheets(1).Rows("10:48").Hidden = false
End If
End Sub
Here is another approach.
The statement ws.CheckBoxes("Check Box 30") = 1 will either return TRUE or FALSE which will either hide, or unhide, your target rows.
Private Sub CheckBox30_Click()
Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets("Sheet1")
ws.Range("A10:A48").EntireRow.Hidden = ws.CheckBoxes("Check Box 30") = 1
End Sub
I have some code that loops through the ActiveX controls on an Excel worksheet. This logs which checkboxes have been selected.
Dim obj AS OLEObject
For Each obj In ActiveSheet.OLEObjects
If TypeName(obj.Object) = "CheckBox" Then ' loop through all checkboxes to find selections
BooCheck = obj.Object
If BooCheck = True Then
MyArray(j) = obj.Name 'if checkbox selected then store the associated Name
j = j + 1
End If
End If
Next obj
This all works fine. However, as I have a number of checkboxes that I need to move around I thought I'd group them together by Shift/click in design mode, right click and select the "Group" option. However, if I do this the grouped checkboxes vanish from OLEObjects. Where do they go? Is there a way of altering my code to find them when they are grouped?
The way to reference the OLEObjects is like this:
Public Sub ReferenceTest(oSheet As Worksheet, sGroupName As String)
Dim i As Long
Dim oOle As OLEObject
With oSheet.Shapes.Range(sGroupName).GroupItems
For i = 1 To .Count
Set oOle = .Item(i).OLEFormat.Object
Debug.Print oOle.Name, oOle.Object.Value
Next i
End With
End Sub
Just specify the sheet and group name, e.g.
ReferenceTest ActiveSheet, "Group 1"
I have built a simple UserForm with a Combobox to delete rows by name.
I have populated the Combobox rowsource using a macro to build a
named range called "Combo_List". I statically inserted that into the
rowsource attribute (meaning, I did not do that with code).
Also on the UserForm is a Checkbox that I want to use for confirmation purposes.
Once they select the username from the list, they need to put a check in the Checkbox and THEN they can click the Delete button.
I have no clue how to write the code that validates the Checkbox and then deletes the selected row.
If it helps, here's the code to build the named range and then show the UserForm:
Sub RecordDelete()
Dim LastRow As Long
ActiveSheet.Select
LastRow = ActiveSheet.UsedRange.SpecialCells(xlCellTypeLastCell).Row
Range("B3:B" & LastRow).Name = "Combo_List"
frmDeleteData.Show
End Sub
The checkbox is just named 'checkbox1'. Any help?
One way to do this is
Set the default property for Enabled for your delete CommandButton to False in design mode
Add code to the CheckBox that enables the CommandButtonwhen the Checkbox is ticked
Add code to the delete CommandButton to delete the entire row
code
Private Sub CheckBox1_Click()
CommandButton1.Enabled = CheckBox1.Value
End Sub
Private Sub CommandButton1_Click()
Dim rng1 As Range
If Me.CheckBox1 Then
Set rng1 = Range("Combo_List").Find(Me.ComboBox1.Value, , xlValues, xlWhole)
If Not rng1 Is Nothing Then
rng1.EntireRow.Delete
Else
MsgBox Me.ComboBox1.Value & "not found"
End If
Else
`redundant if button is disabled
MsgBox "Checkbox not checked!", vbCritical
End If
End Sub