I have an Excel sheet "TestSheet", and a user form frmTestForm, with an option button named OptionButton1. I added the button manually from the editor, so I believe it is a Form Controls option button. I want to turn on (meaning show as selected) the option button based on the value of cell C2 in sheet "TestSheet".
Sub Test_Form()
Worksheets("TestSheet").Activate
Dim OptionButton1 As OptionButton
Dim myopt As OptionButton
Set myopt = OptionButton1
With frmTest_Form
If Range("C2").Value = 5 Then
OptionButton1.Value = True 'Errors here
End If
End With
frmTest_Form.Show
End Sub
The error message is "Object variable or With lock not set", which I believe indicates the option button is not properly defined, but I don't know how to fix it. Thanks in advance for your help.
I have edited the code to reflect both comments. I still have the sane error message, "Object variable or With lock not set" in the line OptionButton1.Value = True. Thanks again for your help.
Got it to work! Changed code as follows:
Sub Test_Form()
Dim OptionButton1 As Variant
Set OptionButton1 = frmTest_Form.OptionButton1
If Range("C2").Value = 5 Then
OptionButton1.Value = True
End If
frmTest_Form.Show
End Sub
Thanks for your help!
Related
i need to create check box to be checked only if another specific cell contains specific input and unchecked if this input is not existing, noting that i tried below code
Private Sub Worksheet_Ca()
Dim DestS As Worksheet
Set DestS = ThisWorkbook.Worksheets("rest")
Dim DestSh As Range
Set DestSh = DestS.Range("B2")
If DestSh.Value = "Device" Then
DestS.CheckBoxes("Reset").Value = xlOn
Else
DestS.CheckBoxes("Reset").Value = xlOff
End If
End Sub
Also the check box i used is part of the forms toolbar not activeX and i changed the name of the check box to 'reset'
The error i got using debug is
method of checkbox of object _worksheet failed
Please, copy the next event code in the worksheet "rest" code module:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("B2")) Is Nothing Then
If Target.value = "Device" Then
CheckBoxes("Reset").value = True
Else
CheckBoxes("Reset").value = False
End If
End If
End Sub
When you change the "B2" cell value in "Device", the check box will be checked. If you change its value in something else, the check box will be unchecked...
In order to copy the code in that sheet module, please activate sheet "rest" execute right click on the sheet name, choose View Code and copy the above code in the window which opens...
Hi,
I want to let the UserForm to be able to detect the text in cell B4 and choose the correct option button without any extra click in the userform. May I know how should I achieve that? Thank you.
Add this into your userform module:
Private Sub UserForm_Initialize()
If Sheets("Sheet1").Range("B4") = "Profit" Then Me.ProfitOption.Value = True
End Sub
Couple things to change:
-Change the Sheet1 to whatever yours is.
-Change the ProfitOption to whatever the name of your button is.
I recommend something like this
Private Sub UserForm_Initialize()
Select Case Sheet1.Range("B4").Value 'evaluate the value of the cell
Case "Profit"
Me.OptionButton1.Value = True
Case "Loss"
Me.OptionButton2.Value = True
Case Else 'if it is none of the above then go into undefined state
Me.OptionButton1.Value = Null
Me.OptionButton2.Value = Null
End Select
End Sub
Note that this does not change the cell value if you change the option in the userform. Therefore you would need write the changed state back using either the Private Sub OptionButton1_Change() event or a "save" button.
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.
Help!
Below is my code for a start/stop button in Excel and now I'm getting a Compile error that is highlighting btnStart on the first line.
Sub btnStart()
ActiveSheet.Unprotect
Cells(Rows.Count, 5).End(xlUp).Offset(1) = Date
Cells(Rows.Count, 6).End(xlUp).Offset(1) = Now
Cells(Rows.Count, 7).End(xlUp).NumberFormat = "hh:mm"
Cells(Rows.Count, 8).End(xlUp).Offset(1) = Environ("username")
Me.btnStart().Enabled = False
Me.btnStop.Enabled = True
End Sub
Sub btnStop()
ActiveSheet.Unprotect
Cells(Rows.Count, 7).End(xlUp).Offset(1) = Now
Cells(Rows.Count, 7).End(xlUp).NumberFormat = "hh:mm"
Me.btnStart.Enabled = True
Me.btnStop.Enabled = False
End Sub
Sub Worksheet_SelectionChange(ByVal Target As Range)
End Sub
If I understand correctly the buttons are on an excel sheet (not in a user form) so your issue because you are calling the buttons incorrectly. you can't simply call the named buttons you must look into the sheet.buttons property like this
ActiveSheet.Buttons("btnStart").
Also if you have named sheets I would use the sheetname rather than ActiveSheet
Sheets("sheetname").Buttons("btnStart").
One more note, that enabling/disabling a button works BUT it doesn't make the button appear enabled/disabled. To do this you also have to change the font color.
ActiveSheet.Buttons("btnStart").Font.ColorIndex = 15 '15 is grey, 1 is black
---- edit: code changed ---
REASON: After doing some more research it seems there are problems with my original solution. The most important is that the "enabled" property has no effect in excel 2010. Another route would be using activeX controls, BUT, a recent windows update (dec 2014) prevents activeX controls from running without deleting some system files (which would have to be done for each user, on every computer this code may run on -_- good job MS SOURCE)
This new solution should avoid all those problems. It uses two global variables start_btn_disabled and stop_btn_disabled I assume each button in your form (btnStart and btnStop) have a macro assigned to them? Simply check the global variable at the very beginning of the the sub if the button is disabled then quit the sub, so even though the click is still processed (it will always be processed in excel 2010 as stated before) the code doesn't run. So it behaves as though it was disabled. In my code I made a sub called btnStopClicked that would run when you click 'btnStop' In order to assign the macro to a button, right click the button, select "assign macro" and select the appropriate macro. I also created a similar sub for when you click the start button
'these are global variables and should be declared at the top of the module
'outside of any sub/function
'
'Note we use DISabled rather than enabled, because by default
'booleans = False. This means as soon as the form opens both these buttons
'will be enabled without any extra work.
'If you want to change this (make start button enabled and stop disabled,
'when the workbook opens simply change all the "stop_btn_disabled" to
'"stop_btn_enabled" and uncomment the following line) make sure you change the
'variable names so they make sense
'Dim stop_btn_enabled As Boolean 'initializes to false
Dim start_btn_disabled As Boolean 'intializes to false
Dim stop_btn_disabled As Boolean 'intializes to false
'Most of this code remains the same as before
Sub btnStart()
ActiveSheet.Unprotect
Cells(Rows.Count, 5).End(xlUp).Offset(1) = Date
Cells(Rows.Count, 6).End(xlUp).Offset(1) = Now
Cells(Rows.Count, 7).End(xlUp).NumberFormat = "hh:mm"
Cells(Rows.Count, 8).End(xlUp).Offset(1) = Environ("username")
'now we set the state of the global variable
start_btn_disabled = True
'makes the button appear greyed out
ActiveSheet.Buttons("btnStart").Font.ColorIndex = 15
'now we set the state of the global variable
stop_btn_disabled = False
'makes the button black like normal
ActiveSheet.Buttons("btnStop").Font.ColorIndex = 1
End Sub
Sub btnStop()
ActiveSheet.Unprotect
Cells(Rows.Count, 7).End(xlUp).Offset(1) = Now
Cells(Rows.Count, 7).End(xlUp).NumberFormat = "hh:mm"
'now we set the state of the global variable
stop_btn_disabled = True
'makes the button appear greyed out
ActiveSheet.Buttons("btnStop").Font.ColorIndex = 15
'now we set the state of the global variable
start_btn_disabled = False
'makes the button black like normal
ActiveSheet.Buttons("btnStart").Font.ColorIndex = 1
End Sub
'and now the real key is checking the globals before running the code
'when you click "btnStop" button this is the code that runs, you may have
'named the sub something different, I just named it this way so it's clear
'what the sub does
Sub StopBtnClicked()
'must be the first bit of code in the btn click sub
If (stop_btn_disabled) Then
Exit Sub
End If
'the rest of the code when you click stop button goes here
'the only way to get to this point is if the but is enabled
End Sub
Sub StartBtnClicked()
'must be the first bit of code in the btn click sub
If (start_btn_disabled) Then
Exit Sub
End If
'the rest of the code when you click start
End Sub
if this solves your problem, please mark it as the answer
Maybe try the following, I corrected one line
Sub btnStart()
ActiveSheet.Unprotect
Cells(Rows.Count, 5).End(xlUp).Offset(1) = Date
Cells(Rows.Count, 6).End(xlUp).Offset(1) = Now
Cells(Rows.Count, 7).End(xlUp).NumberFormat = "hh:mm"
Cells(Rows.Count, 8).End(xlUp).Offset(1) = Environ("username")
'Corrected line below
Me.btnStart.Enabled = False
Me.btnStop.Enabled = True
End Sub
Sub btnStop()
ActiveSheet.Unprotect
Cells(Rows.Count, 7).End(xlUp).Offset(1) = Now
Cells(Rows.Count, 7).End(xlUp).NumberFormat = "hh:mm"
Me.btnStart.Enabled = True
Me.btnStop.Enabled = False
End Sub
Sub Worksheet_SelectionChange(ByVal Target As Range)
End Sub
You need to use ActiveX buttons if you want to use the Enabled property:
Private Sub btnStart_Click()
Me.btnStart.Enabled = False
Me.btnStop.Enabled = True
End Sub
Private Sub btnStop_Click()
Me.btnStart.Enabled = True
Me.btnStop.Enabled = False
End Sub
As the title states: in Excel 2010 how can I reference the object that a macro has been assigned to? I've created a spreadsheet with a form checkbox and assigned this macro to it:
Sub Toggle()
If ActiveSheet.Shapes("Checkbox1").OLEFormat.Object.Value = 1 Then
ActiveSheet.Shapes("Picture1").Visible = True
Else
ActiveSheet.Shapes("Picture1").Visible = False
End If
End Sub
The checkbox toggles whether or not a picture is visible and that's working fine but I'd like to reuse the script for multiple checkboxes.
The above code is specifically targeting Checkbox1 but I'd like it to target "this", the object I've assigned the macro to.
I feel like this should be really easy but I spent all evening on MSDN, excelforums.com and just googling around.
Thanks for your help!
Application.Caller is what you want
Sub Toggle()
Dim cb As String, shps As Shapes
cb = Application.Caller
Set shps = ActiveSheet.Shapes
shps("Picture1").Visible = (shps(cb).OLEFormat.Object.Value = 1)
End Sub
As far as I know that is not possible using VBA. Sure you could put the toggle code into a separate sub and re-use it. That might help a bit but you still need to specify the name of the checkbox.
Private Sub CheckBox1_Click()
Call Toggle("Checkbox1", "Picture1")
End Sub
Sub Toggle(ByVal Nm As String, ByVal pic As String)
If ActiveSheet.Shapes(Nm).OLEFormat.Object.Value = 1 Then
ActiveSheet.Shapes(pic).Visible = True
Else
ActiveSheet.Shapes(pic).Visible = False
End If
End Sub
You need to put the toggle sub in the same sheet as the checkbox code or else put the toggle sub in a module.