I have a ActiveX CheckBox control on worksheet "A" and another on worksheet "B". When I check the CheckBox at "A", I want my macro to check the CheckBox at "B".
What I have tried so far:
This: Sheets("B").Shapes("CheckBox1").ControlFormat.Value = xlOn
And this: ThisWorkbook.Worksheets(1).Shapes("Check Box 1").OLEFormat.Object.Value = 1
Both codes give me an error saying that the the object doesn't accept this property or method.
So it's not possible to check a CheckBox from another worksheet?
I find it useful to use a With ... End With statement to reference a worksheet as it allows multiple operations.
With Worksheets("B")
' for Form Control Checkbox
.Shapes("Check Box 2").ControlFormat.Value = xlOn
' for ActiveX Control Checkbox
.Shapes("CheckBox1").OLEFormat.Object.Object.Value = xlOn
End With
The prefix period (aka . or full stop) applies the parent worksheet.
Checkbox1 in sheet 1 to change checkbox1 in sheet 2
Private Sub CheckBox1_Click()
If Me.CheckBox1 = True Then
Sheets("Sheet2").CheckBox1.Value = True
Else
Sheets("Sheet2").CheckBox1.Value = False
End If
End Sub
For me, replacing the numbers 1 and 0 as for, respectively, the
boolean "True" and "False" did not work. Some say that ActivexControl
has "False" equal to -4146 and FormControl has "False" as equal to 0. This did not work either. The snipped below is now working.
Dim i as Integer
For i = 1 to 502
If Sheets("Sheet1").Shapes("Alpha-" & i).ControlFormat.Value = 1 Then '<-- FormControl
Sheets("Sheet2").Shapes("beta" & i).OLEFormat.Object.Object.Value = 1 '<--ActivexControl
Else
Sheets("Sheet2").Shapes("beta" & i).OLEFormat.Object.Object.Value = 0 '<--ActivexControl
End If
next i
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.
Me again,
In my userform I just added 2 optionbutton click (1 and 2).
What I need is when you choose one of those buttons to get text in cell ( Option button 1 text I need in cell is Daily check and optionbutton 2 is weekly check).
I tried with this code but it gives me only false or true as return information.
Private Sub OptionButton1_Click()
If OptionButton1.value = True Then
Range("I2").value = "MONTHLY CHECK"
Else
Range("I2").value = "OPERATIONAL DAY"
End If End sub
Then after choosing option button this I have Commmand button that would enter this data (daily check or weekly check) into cells in column I, every entry new row which works but as I said only with true or false information
Private Sub CommandButton2_Click()
Dim sh As Worksheet, lastRow As Long
Set sh = Sheets("Test")
lastRow = sh.Range("A" & Rows.Count).End(xlUp).row + 1
sh.Range("I" & lastRow).value = OptionButton1.value
sh.Range("I" & lastRow).value = OptionButton2.value
Unload Me
End Sub
Thanks in advance on your help, as always.
Per my comment, the issue is you are assigning your sh.Range values with the OptionButton.Value.
Per the OptionButton.Value property; Determines or specifies whether the specified option button is selected...
The .Value property returns True/False if the OptionButton is Selected/Unselected respectively.
Note: You can also change the state of the OptionButton to select/unselect it by setting the .Value property in your code.
The OptionButton.Caption property (see here for label.caption which outlines the same principal for an optionbutton caption) is the visible text to the user generally describing what the optionbutton is for or does.
So your code was returning True/False because you were using the .Value property - instead use the .Caption property to return the descriptive text assigned to that optionbutton.
Example:
This is a new UserForm with one OptionButton, both with default names.
OptionButton1.Value is True
AND
OptionButton1.Caption is This is OptionButton1.
If we were to unselect the optionbutton (say by executing some 'Reset' code) the OptionButton1.Value would then be False but the OptionButton1.Caption would remain This is OptionButton1.
I am making an Excel form with some activeX controls and am having a problem incorporating the following functionallity:
I wish for the users to select a number in ComboBox11. If the number be 0, the form changes so, that comboboxes 9 and 10 become disabled (using VBA code) and the table below 'fades out' (using conditional formatting), informing the user not to fill it out.
On the other hand if the user selects a number larger than 0, the form stays as it is. Under the table is a checkbox (checkbox1) used to expand the table (unhiding previously hidden rows) if data needeed to be put in the form is larger than the table size.
The VBA code behind combobox 11 is:
Private Sub ComboBox11_change()
Dim ws As Worksheet
Set ws = Sheets("Form")
If Not Me.ComboBox11.Text = "" Then ws.Range("J24") = CInt(Me.ComboBox11.Text) 'to write integer instead of text into linked cell
If Me.ComboBox11.Value = 0 Then
Me.ComboBox9.Enabled = False
Me.ComboBox10.Enabled = False
If Me.CheckBox1.Value = True Then Me.CheckBox1.Value = False 'if combobox11 is 0 than the table doesn't need to be expanded
Me.CheckBox1.Enabled = False
Else
Me.ComboBox9.Enabled = True
Me.ComboBox10.Enabled = True
Me.CheckBox1.Enabled = True
End If
End Sub
And the code behind CheckBox1 is:
Private Sub CheckBox1_Change()
Dim ws As Worksheet
Set ws = Sheets("Form")
If CheckBox1.Value = False Then ws.Rows("46:71").Hidden = True
If CheckBox1.Value = True Then ws.Rows("46:71").Hidden = False
End Sub
So, if I select 0 in combobox11 the result is:
So far so good. But if I select something larger than 0, say 1 in combobox11 and then try to expand the table by clicking on the checkbox1, the table expands, but I get an error message:
Run-time error '1004': Not possible to set properties: enabled class:
OLEObject
(not sure about the exact error text, since I'm not using English MSOffice)
When pressing the Debug button, the following line in Sub ComboBox11_Change() lights up:
Me.ComboBox9.Enabled = True
The Strange thing is, that this error does not appear when the combobox11 is left blank ( a value is not selected).
I have no idea why the checkbox would interact with the other comboboxes. I am bewildered and any help would be much appreciated.
To reproduce this:
Have a sheet like this:
A1:A6 is the ListFillRange of the ComboBox.
Then hiding any row between 1:6 using VBA code in CheckBox1_Change() will throw the error.
Private Sub CheckBox1_Change()
If CheckBox1.Value = False Then Me.Rows("7:13").Hidden = True
If CheckBox1.Value = True Then Me.Rows("7:13").Hidden = False
'If CheckBox1.Value = False Then Me.Rows("6:13").Hidden = True 'ERROR
'If CheckBox1.Value = True Then Me.Rows("6:13").Hidden = False 'ERROR
End Sub
Private Sub ComboBox1_Change()
If Me.ComboBox1.Value = 0 Then
If Me.CheckBox1.Value = True Then Me.CheckBox1.Value = False
Me.CheckBox1.Enabled = False
Else
Me.CheckBox1.Enabled = True
End If
End Sub
If we create a name named "list" which is related to a whole column like this:
and use this "list" as the ListFillRange of the ComboBox, then the error occurs ever if rows in this sheet were hidden from code. This is independent on whether the hidden rows are within the real active "list" rows 1-6 or not.
If we not reference a whole column in the name but for example only rows 1-10 like:
=INDEX(Sheet1!$A$1:$A$10;1):INDEX(Sheet1!$A$1:$A$10;6)
then the error only occurs if the code hides rows from 1 to 10 but not if it hides rows from 11 upwards.
Edit:
To continue my example:
Moved the numbers from Sheet1!A:A to Sheet2!A:A and created a named range "list" related to
=Sheet2!$A$1:INDEX(Sheet2!$A$1:$A$40;COUNTIF(Sheet2!$A$1:$A$40;"<>"))
Then the following code to set Sheet1.ComboBox1.ListFillRange to "list" will work if it is within the Sheet2 class module:
Private Sub Worksheet_Change(ByVal Target As Range)
ThisWorkbook.Worksheets(1).ComboBox1.ListFillRange = "list"
End Sub
It will produce the error if setting the ListFillRange is tried from within the Sheet1 class module.
Because comments are too short to explain whad I did to solve the issue, I am posting this answer.
First of all thanks to Axel Richter, who took his time to elaborate on my problem.
The Combobox11 was filled with a generated ListFillRange in the name manager:
The described error stopped appearing, as soon as I changed the ListFillRange. At first I tried a simple alternative: "=list!AU1:AU40" Although I don't understand the problem it is now solved!
Afterwards I used the following code to create a dynamic list for combobox11.
Dim zadnji As Integer
zadnji = Sheets("Form").Range("T9").Value + 1
Me.OLEObjects("combobox11").ListFillRange = "=lists!AU1:AU" & zadnji
I assigned a macro to my checkbox, so that when you tick it, it sets a certain value into a cell. I however get a invalid qualifier error on my first Checkbox1.value.
Here's the code:
Option Explicit
Public CheckBox1 As Boolean
Sub CheckBox1_Click()
If CheckBox1.Value = True Then Range("Q10").Value = 1
If CheckBox1.Value = False Then Range("Q10").Value = 0
End Sub
Thanks!
I'm assuming you have used an ActiveX control with the name CheckBox1, if so you are having the problem because you've also defined the public boolean CheckBox1.
You're code is unhappy since the public declaration of the CheckBox1 boolean is overwriting the control variable CheckBox1 and a boolean doesn't have a .Value property. If you want to store the use of the checkbox value in a boolean then change your public variable name from Checkbox1, otherwise delete this declaration entirely.
Edit:
Based on your comment, you are not linking the checkbox from your worksheet. The code has to know what CheckBox1. Since you've deleted the boolean it now does not know what Checkbox1 is. Consequently you need to tell it where the checkbox is.
You can do it a number of ways. If it is a form checkbox (not ActiveX like I first thought) then the following code will work:
Sub Checkbox1()
Dim cb As Object, myCheckBoxValue As Boolean
'Get the object from the worksheet
Set cb = ActiveSheet.Shapes("Check Box 1").OLEFormat.Object
'Assign it's value to the boolean (1 is checked, -4146 is unchecked, 2 is mixed (grey box))
If cb.Value = 1 Then myCheckBoxValue = True Else myCheckBoxValue = False
'Output to worksheet
If myCheckBoxValue Then
ActiveSheet.Range("Q10").Value = 1
Else
ActiveSheet.Range("Q10").Value = 0
End If
Set cb = Nothing
End Sub
You can also cut this down significantly and the following will work:
Sub Checkbox1()
If ActiveSheet.Shapes("Check Box 1").OLEFormat.Object.Value = 1 Then
ActiveSheet.Range("Q10").Value = 1
Else
ActiveSheet.Range("Q10").Value = 0
End If
End Sub
I'm trying to use an IF-clause to determine whether my checkbox, named "Check Box 1", is checked.
My current code:
Sub Button167_Click()
If ActiveSheet.Shapes("Check Box 1") = True Then
Range("Y12").Value = 1
Else
Range("Y12").Value = 0
End If
End Sub
This doesn't work. The debugger is telling me there is a problem with the
ActiveSheet.Shapes("Check Box 1")
However, I know this code works (even though it serves a different purpose):
ActiveSheet.Shapes("Check Box 1").Select
With Selection
.Value = xlOn
My checkboxes (there are 200 on my page), are located in sheet1, by the name of "Demande". Each Checkbox is has the same formatted name of "Check Box ...".
Sub Button167_Click()
If ThisWorkbook.Worksheets(1).Shapes("Check Box 1").OLEFormat.Object.Value = 1 Then
Range("Y12").Value = 1
Else
Range("Y12").Value = 0
End If
End Sub
1 is checked, -4146 is unchecked, 2 is mixed (grey box)
Is this what you are trying?
Sub Sample()
Dim cb As Shape
Set cb = ActiveSheet.Shapes("Check Box 1")
If cb.OLEFormat.Object.Value = 1 Then
MsgBox "Checkbox is Checked"
Else
MsgBox "Checkbox is not Checked"
End If
End Sub
Replace Activesheet with the relevant sheetname. Also replace Check Box 1 with the relevant checkbox name.
Building on the previous answers, you can leverage the fact that True is -1 and False is 0 and shorten your code like this:
Sub Button167_Click()
Range("Y12").Value = _
Abs(Worksheets(1).Shapes("Check Box 1").OLEFormat.Object.Value > 0)
End Sub
If the checkbox is checked, .Value = 1.
Worksheets(1).Shapes("Check Box 1").OLEFormat.Object.Value > 0 returns True.
Applying the Abs function converts True to 1.
If the checkbox is unchecked, .Value = -4146.
Worksheets(1).Shapes("Check Box 1").OLEFormat.Object.Value > 0 returns False.
Applying the Abs function converts False to 0.
It seems that in VBA macro code for an ActiveX checkbox control, you use
If (ActiveSheet.OLEObjects("CheckBox1").Object.Value = True)
and for a Form checkbox control, you use
If (ActiveSheet.Shapes("CheckBox1").OLEFormat.Object.Value = 1)
Try: Controls("Check Box 1") = True