Microsoft VB: Run-time error '424': Object required - vba

I'm currently trying to implement some code into my excel so when a user selects one check box the other check boxes will be unselected. This is the following code I have for it:
Sub CheckBox77_Click()
If CheckBox77.Value = True Then
CheckBox78.Value = False
End If
End Sub
Sub CheckBox78_Click()
If CheckBox78.Value = True Then
CheckBox77.Value = False
End If
End Sub
When I test this, I get an error that says:
Run-time error '424': Object Required.
How can I fix this? Thanks!

Ok, you want to place those methods in "Module1", you should use as follow:
If Sheets("sheetname").CheckBox78.Value = True Then
Sheets("sheetname").CheckBox77.Value = False
End If

Related

VBA Access Pivot Chart set the y-axis min/max scale

Came into another snag formating my Pivot Chart via VBA in Access 2010. I get the "Run-time error '438': Object doesn't support this property or method" error.
Again, I have tried many different iterations of the code, but can't get the right one. As always, thank you in advance for your assistance.
Private Sub Form_Load()
'Call LineMarkerFormat
RONMax = DMax("[Total A/C]", "tblRONChartComparison")
RONMin = DMin("[Total A/C]", "tblRONChartComparison")
With Me.ChartSpace.Charts(0).Axes(1).Scaling
.MinimumScale = RONMin
.MaximumScale = RONMax
End With
End Sub
and the very next site I check had the very simple solution. you no longer need the scaling on the end of the .minimum/.maximum. Here is my working code.
Private Sub Form_Load()
'Call LineMarkerFormat
RONMax = DMax("[Total A/C]", "tblRONChartComparison")
RONMin = DMin("[Total A/C]", "tblRONChartComparison")
With Me.ChartSpace.Charts(0).Axes(1).Scaling
.Minimum = RONMin
.Maximum = RONMax
End With
End Sub

vba Checkbox if else condition in Subform, access

I have a main form with several tab controls with subforms in them. On the first tab or subform I have a "checkbox 1", which shall be:
checked if "textbox 1" is not empty
unchecked if "textbox 1" is empty
The code is directly put into the Class Object of "subform 1", that's why I thought I could use Me.
Here is my code, but I always get error messages :(
Private Sub Ctltextbox_1_AfterUpdate()
If Len(Ctltextbox_1.Value) = 0 Then
Me.checkbox_1.Value = 0
Else
Me.checkbox_1.Value = -1
End If
End Sub
That way I am getting the
Run-time error '2448': You can't assign a value to this object.
On the line that attempts to assign -1 to Me.checkbox_1.Value.
Try this. It is working for me. I moved it to the Ctltextbox_1_Change action. This will call the function whenever it is changed and will catch when you have nothing in the box as well. I also changed your values to True and False.
Please try this and let me know.
Private Sub Ctltextbox_1_Change()
If Len(frmSub.Ctltextbox_1.Value) = 0 Then
frmSub.CheckBox_1.Value = False
Else
frmSub.CheckBox_1.Value = True
End If
End Sub
you can also solve this in one line as mentioned below.
Private Sub Ctltextbox_1_Change()
frmSub.CheckBox_1.Value = IIf((Len(frmSub.Ctltextbox_1.Value) = 0), False, True)
End Sub
Ok, the simple answer is: you can not use the expression Len()= 0 for a not numeric field! So my working code for the subform is now:
Private Sub Textbox1_AfterUpdate()
If IsNull(Textbox1.Value) = False Then
Me.checkbox1.Value = True
Else
Me.checkbox1.Value = False
End If
End Sub
Thank you all for your help! :)

Reference a check box in another open workbook

I am trying to refer to the value of an ActiveX checkbox control in another worksheet. My goal is to make the value of the check box in my current workbook the same as the one in another open workbook.
I can do this between two checkboxes in the same workbook on different sheets:
Private Sub CommandButton1_Click()
If Sheets("Sheet2").Box2.Value = True Then
Box1.Value = True
Else: Box1.Value = False
End If
End Sub
But I'm receiving a Run-time error '9' "Subscript out of range" error when I run the following code:
Private Sub CommandButton2_Click()
If Worksheets("Book2").OLEObjects("Box3").Value = True Then
Box1.Value = True
Else: Box1.Value = False
End If
End Sub
The "If Worksheets" line is highlighted when I try to debug the code. I'm sure I'm referring to the other checkbox incorrectly, but I've searched high and low for the proper way to refer to this without any luck. Thank you in advance for your help!
If Worksheets("Book2").OLEObjects("Box3").Value = True Then
"Book2" is not the name of a Worksheet, presumably it is the name of the other book. In which case:
If WorkBooks("Book2").Worksheets(1).OLEObjects("Box3").Value = True Then
..or use whatever the name of the worksheet in the other book is called.

VBA errors when opening xl document

I added some VBA code for my xl sheets .
This code seems to compile when i open the WorkBook.
An exemple of the type of error i got when i open the document .
Private Sub ComboBox1_Change()
If ComboBox1.Value = "GGS" Then
Sheets("Index").CommandButton2.Visible = True
==> Sheets("Index").Label6.Visible = True
Sheets("MNO").CommandButton5.Visible = True
Sheets("ServiceProvider").CommandButton5.Visible = True
Sheets("ServiceDeployer").CommandButton5.Visible = True
Sheets("CardVendor").CommandButton5.Visible = True
Sheets("LoadFile").CommandButton5.Visible = True
Else
Sheets("Index").Label6.Visible = False
Sheets("Index").CommandButton2.Visible = False
Sheets("MNO").CommandButton5.Visible = False
Sheets("ServiceProvider").CommandButton5.Visible = False
Sheets("ServiceDeployer").CommandButton5.Visible = False
Sheets("CardVendor").CommandButton5.Visible = False
Sheets("LoadFile").CommandButton5.Visible = False
End If
I got a "Run-time errror '438' : Object doesn't support the property or method " on the marked line
Sometimes there is the same error for this line in another sheet
Me.ListBox1.Clear
So I end the debuging , then all the code works properly ,even those last lines .
Is it possible to disable the auto compilation at the opening of the document ?
It seems that the VBA editor tries to run the code before the View is created.
The debug just drop an "Object required" .I checked before stoping debuging and the object was not in the objects list .When i stoped the debuging then checked the list, the object was there
(I'm French so there might have some english mistakes )
Thank you for reading me
Spikeze
EDIT :
I found the problem.
The "ComboBox1.Style " is on fmStyleDropDownList to avoid the user to edit the options .
But this option makes the Combobox automatically choose the first option when the document opens.
So I guess it runs "ComboBox1_Change()" when the first option is choosed but some view elements are not loaded at this time and the VBA editor drops an "Objec required".
I set the style to fmStyleDropDownCombo and fmStyleDropDownList when the sheets is active but if the WorkBook is saved and reopened the style is on fmStyleDropDownList again and I got the error again .
To solve it I had those sub
Private Sub Worksheet_Activate()
ComboBox1.Style = fmStyleDropDownList
End Sub
In the Index sheet code and
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Sheets("Index").ComboBox1.Style = fmStyleDropDownCombo
End Sub
In the WorkBook code .
Spikeze
You can just use error handling. Not the best solution, but it should work. If you are sure these objects exist, when you need them, you can use
On Error Resume Next
Or handle the error properly, with checking code error and resuming only on 438

Cycling through some checkboxes on my excel page

I am VERY new to VBA (and know of it only within excel).
I am trying to cycle through some (but not all) checkboxes. They are currently named CheckBox1 through CheckBox15. Howe do I cycle through say for instance CheckBox5 through CheckBox10?
I guess I am hoping there is a 'method' similar to 'CheckType' for controls that will allow me to check name?
Here is what I have tried. Causes a Compile Error - Sub or Function not defined, and highlights Worksheet.
Private Sub BoxCheck()
atLeastOneChecked = False
For i = 2 To 4
If Worksheets("ActiveX").Controls("Checkbox" & i).Value = True Then
atLeastOneChecked = True
End If
Next i
End Sub
While the above doesnt work, what is below does:
Private Sub BoxCheck()
atLeastOneChecked = False
For i = 1 To 2
If Sheet2.CheckBox2.Value = True Then
atLeastOneChecked = True
End If
Next i
End Sub
Of course, the loop has no affect on the outcome, but it compiles and atLeastOneChecked turns from False to True when Checkbox2 is True. Note that Sheet2 has been named ActiveX. I clearly don't understand how Worksheet and Controls work. Could someone help?
After fixing the error described below, this still won't work. I simplified to the following:
Private Sub BoxCheck()
Dim ole As OLEObject
atLeastOneChecked = False
Set ole = Sheets("ActiveX").OLEObjects("Checkbox2")
If ole.Value = True Then
atLeastOneChecked = True
End If
End Sub
This doesn't work. It fails at:
If ole.Value = True Then
The error states: Object Doesn't support this property or method
This is true for OLEObjects, but not for Checkboxes. When I look at the properties of ole, I see that its Object property is set to Object/Checkbox and that this Object has a value. I guess that is what I should be referencing in my if statement, but I don't know how.
I think I solved the problem.
The value of the Checkbox is accessed by Referencing the Object property within the OLEObject I set...Like this:
If ole.Object.Value = True Then
Thanks for all your help. If someone has a more elegant solution, I would still like to see it.
Use CheckBox.Name
Example:
For Each cb In ActiveSheet.CheckBoxes
If cb.Name = "CheckBox5"
' Do stuff
End If
Next cb
To expand on #Parker's answer:
Private Sub BoxCheck()
atleastonechecked = False
Dim oles As OLEObject
For i = 2 To 4
'If you're using Shapes Controls:
If ThisWorkbook.Worksheets("ActiveX").Shapes("Check Box " & i).Value = True Then
atleastonechecked = True
End If
' If you're using ActiveX Controls
Set oles = ThisWorkbook.Worksheets("ActiveX").OLEObjects("CheckBox" & i)
If oles.Value = True Then
atleastonechecked = True
End If
Next i
End Sub
Sorry, just put together the previous answer without testing - that always fails. Just use the If loop based on what type of control you're using.