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
Related
I have several Check Boxes (Form Controls) located in the same sheet. When I click on any Check Box, I need it to hide a specified range of rows and 2 specified scroll bars.
I had initially used an Active Control Check Box, and written this code which worked (This worked specifically for CheckBox1):
Private Sub CheckBox1_Click()
If CheckBox1.Value = True Then
[105:112].EntireRow.Hidden = True
ActiveSheet.Shapes("Scroll Bar 79").Visible = False
ActiveSheet.Shapes("Scroll Bar 82").Visible = False
Else:
[105:112].EntireRow.Hidden = False
ActiveSheet.Shapes("Scroll Bar 79").Visible = True
ActiveSheet.Shapes("Scroll Bar 82").Visible = True
End If
End Sub
However, I want to create a more generic code because I have 60 of these checkboxes(as described below).
This code has four variables: 1) Check Box Name 2) Row Range 3)Scroll Bar 1 4)Scroll Bar 2
For each check box in the sheet, I want to be able to alter the 4 variable names. I have tried using this format 'NameOfMacro "variable1", "variable2"' when assigning macros to each of the checkboxes but this hasn't worked!
Sub Hide ()
Dim chkBox As CheckBox
Dim RowRange As Range
Dim SB1 As Shape
Dim SB2 As Shape
Set chkBox = ActiveSheet.CheckBoxes(Application.Caller)
If chkBox.Value = True Then
[RowRange].EntireRow.Hidden = True
ActiveSheet.Shapes("SB1").Visible = False
ActiveSheet.Shapes("SB2").Visible = False
Else:
[RowRange].EntireRow.Hidden = False
ActiveSheet.Shapes("SB1").Visible = True
ActiveSheet.Shapes("SB2").Visible = True
End If
End Sub
I am having trouble on two fronts
1)Creating a generic macro, I get errors such as "unable to get the checkboxes property of the worksheet class". How can I make the check box name variable?
2) Assigning the same macro to multiple checkboxes while being able to alter the arguements using this method: 'NameOfMacro "variable1", "variable2"'
Any help would be greatly appreciated!
You need to make a parameter for your Hide routine. Then pass the value of the checkbox in to that parameter. This will give you the behavior you want. Just make one of these for each checkbox:
Private Sub CheckBox1_Click()
HideRow CheckBox1.Value, ActiveSheet.Range("A105:A112"), "Scroll Bar 79", "Scroll Bar 82"
End Sub
Public Sub HideRow(ByVal hiding As Boolean, ByVal rowRange As Range, ByVal ScrollBarOne As String, ByVal ScrollBarTwo As String)
If hiding Then
rowRange.EntireRow.hidden = True
ActiveSheet.Shapes(ScrollBarOne).Visible = False
ActiveSheet.Shapes(ScrollBarTwo).Visible = False
Else
rowRange.EntireRow.hidden = False
ActiveSheet.Shapes(ScrollBarOne).Visible = True
ActiveSheet.Shapes(ScrollBarTwo).Visible = True
End If
End Sub
first of all i want to say thanks for every person who respond me for my previous quesiton,
i have a combobox (list) and a textbox(key1), but i want to enable textbox IF combobox is not null, I tried to to this by this code
Private Sub TextBox1_Change()
If IsNullOrEmpty(ComboBox1.Text) Then
TextBox1.Visible = False
TextBox1.Enabled = False
Else
TextBox1.Visible = True
TextBox1.Enabled = True
End If
End Sub
but the result is always the textbox1 is disabled, even if i choose from the list or combobox<>null
This is simpler than you are making it.
Instead, test for the length of the ComboBox1's text property being greater than 0. That will give you a boolean value of True/False. You can then use that boolean result to set any other boolean value property (like the Enabled or Visible property) all in one line.
Also, you need to put it on the Change event of ComboBox1, not TextBox1.
Private Sub ComboBox1_Change()
TextBox1.Enabled = (Len(ComboBox1.Text)>0)
End Sub
Or
Private Sub ComboBox1_Change()
TextBox1.Visible = (Len(ComboBox1.Text)>0)
End Sub
Can someone tell me what the syntax is to determine the controlX checkbox name?
I have approximately 4 check boxes and this may potentially grow, so I'd like a method of passing through the checkbox name dynamically rather than writing the same execution 4-9 times.
My intention is to pass through the checkbox name as a variable so I do not have to repeat the below code for each checkbox. Also, does anyone know how to reference a named range to a specific checkbox? The code I have so far is:
Sub CheckBox1_Click()
Application.ScreenUpdating = False
Dim strCheck As String
strCheck = CheckBox1.Value
If strCheck = True Then
Range("RevAssp_CCV").Select
Selection.EntireRow.Hidden = False
Else
Range("RevAssp_CCV").Select
Selection.EntireRow.Hidden = True
End If
End Sub
Thanks in advance
The easiest way would be to create a custom wrapper class, create an array of objects of said class and then hook into the event there.
You can then (for example) check the Caption and set the "hidden" of the NamedRange.EntireRow equal to the value (e.g. checked is invisible, unchecked is visible)
The most basic implementation of this would be as follows:
CustomCheckBox Class module:
Private WithEvents p_chkBox As MSForms.checkbox
Public Property Let box(value As MSForms.checkbox)
Set p_chkBox = value
End Property
Public Property Get box() As MSForms.checkbox
Set box = p_chkBox
End Property
Private Sub p_chkBox_Click()
Range(p_chkBox.Caption).EntireRow.Hidden = p_chkBox.value
End Sub
And in a regular module:
Public cCheckBox() As CustomCheckBox
Sub Test()
Dim ws As Worksheet
Dim oleObj As OLEObject
Dim i As Integer
i = 0
For Each ws In ThisWorkbook.Worksheets
For Each oleObj In ws.OLEObjects
If TypeName(oleObj.Object) = "CheckBox" Then
ReDim Preserve cCheckBox(0 To i)
Set cCheckBox(i) = New CustomCheckBox
cCheckBox(i).box = oleObj.Object
i = i + 1
End If
Next oleObj
Next ws
End Sub
The regular module puts all checkboxes into 1 array, which is a public variable so it will be available even after the code has run. You could also place this code in the Workbook Module as Private Sub Workbook_Open to ensure that the checkboxes will be initialized properly in all cases.
Keep in mind that if the Named Range for the caption of the Checkbox doesn't exist, this will throw errors.
To get back to your example, you could now just add two checkboxes on your sheets and set the caption of the first one to "RevAssp_CCV" and the second one to whatever other named range you wish to toggle.
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
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