vegetamaker
New Member
- Joined
- Jun 17, 2015
- Messages
- 18
Hello again! And thanks for your time!
Well, I found a very usefull code to add many checkboxes in the Sheet, but I was trying to do something more. I was trying to check if the cell has a checkbox already, if not, then it should add it.
The code is the next: https://stackoverflow.com/questions...d-size-with-cells-for-checkbox-created-in-vba (Isn't the original one, I can't find it again. But it's basically the same). I converted it to this:
Well, after it the next step is learn how to check if a cell has a checkbox, and I find this another code for it: https://stackoverflow.com/questions/35921758/excel-vba-ckeck-if-cell-contains-a-checkbox . I converted it to this:
But the Function requires a Range and my actual code is using a single Address. So I was trying two things: Or change the Function to ask for an Address or try to convert my Address to Range. And I can't find a solution for it.
I tryed something like this:
The next step is add an "If" inside the "For Each" in the first code that call the 'HasCheckbox' function.
But for now that is my problem. I guess I could find a way to fix it with something of the next (But I can't fix it by myself atm):
- convert my Address to a Range
- Change the HasCheckbox function to need a Address instead a Range
- OR another way to check if a cell has a checkbox already
Btw, I am using Form Controls checkboxes.
Thanks so much for your help!
Well, I found a very usefull code to add many checkboxes in the Sheet, but I was trying to do something more. I was trying to check if the cell has a checkbox already, if not, then it should add it.
The code is the next: https://stackoverflow.com/questions...d-size-with-cells-for-checkbox-created-in-vba (Isn't the original one, I can't find it again. But it's basically the same). I converted it to this:
Code:
Sub CellCheckbox()
Dim myCell As Range
Dim CBX As CheckBox
With ActiveSheet
.CheckBoxes.Delete 'nice for testing!
'#################
Set myRng1 = .Range("B2:B2, C2:C5") 'change to the range you want
'#################
End With
For Each myCell In myRng1.Cells
With myCell
Set CBX = .Parent.CheckBoxes.add _
(Top:=.Top, _
Left:=.Left + 20, _
Width:=1, _
Height:=.Height)
CBX.Name = "CheckBox_" & .Address(0, 0)
CBX.Caption = "" 'Text with Checkbox
CBX.Value = xlOff
CBX.Placement = xlFreeFloating 'To set it automatically to NOMOVE
CBX.LinkedCell = .Address(RowAbsolute:=False, _
ColumnAbsolute:=False, _
external:=False)
.NumberFormat = ";;;"
End With
Next myCell
End Sub
Well, after it the next step is learn how to check if a cell has a checkbox, and I find this another code for it: https://stackoverflow.com/questions/35921758/excel-vba-ckeck-if-cell-contains-a-checkbox . I converted it to this:
Code:
Public Function HasCheckbox(rng As Range) As Boolean
If Not Application.Intersect(rng, ActiveSheet.CheckBoxes.TopLeftCell) Is Nothing Then
HasCheckbox = True
Else
HasCheckbox = False
End If
End Function
But the Function requires a Range and my actual code is using a single Address. So I was trying two things: Or change the Function to ask for an Address or try to convert my Address to Range. And I can't find a solution for it.
I tryed something like this:
Code:
Dim myRange$
myRange = Range(Cells(myCell.Row, myCell.Column), Cells(myCell.Row, myCell.Column)).Address(False, False)
'But this didn't work :D
The next step is add an "If" inside the "For Each" in the first code that call the 'HasCheckbox' function.
But for now that is my problem. I guess I could find a way to fix it with something of the next (But I can't fix it by myself atm):
- convert my Address to a Range
- Change the HasCheckbox function to need a Address instead a Range
- OR another way to check if a cell has a checkbox already
Btw, I am using Form Controls checkboxes.
Thanks so much for your help!
Last edited: