Excel VBA option button selected as default on start - excel

I am trying to make a preferred non activex radio button to be selected by default when the excel document is opened.
What I was trying to do:
Private Sub Workbook_Open()
ActiveSheet.Shapes("Option Button 1").Value = True
End Sub
But I get this error message:
Run-time error '438':
Object doesn't support this property of method

Try this:
ActiveSheet.Shapes("Option Button 1").ControlFormat.Value = xlOn

Related

DropDown a ComboBox in a sheet, NOT on a UserForm

In an Excel sheet, NOT on a UserForm - Trying to come up with a code to be able to drop down a combo box, i.e. to get it to open. Might use it to trigger with hover over a shape. But, get a "Compile error: Invalid use of Me keyword", and without "Me." get "Run-time error '424': Object required"
Sub ActivateCB1()
ActiveSheet.ComboBox1.Activate
Me.ComboBox1.DropDown
End Sub
Me only works in a code module attached to (eg) a worksheet or userform
Sub ActivateCB1()
With ActiveSheet.ComboBox1
.Activate
.DropDown
End With
End Sub

Add a dynamic placeholder in a textbox of a page inside of an Excel VBA Userform

I have a Userform with a multipage object. In one of pages there is a textbox object where I want to add a placeholder when Enter event is not detected. Problem is I'm not figure out how to make reference to textbox from Enter and Exit events due to I have a multipage structure.
I tried to adapt the following approach but it works only for a Userform without Multipage structure.
Got it here: Text box prompt text in Excel VBA
Private Sub TextBox1_Enter()
TB_enter ActiveControl.Name
End Sub
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
TB_exit ActiveControl.Name
End Sub
Sub TB_enter(TB_name)
If Len(Me.Controls(TB_name).Tag) = 0 Then
Me.Controls(TB_name).Tag = Me.Controls(TB_name).Value
Me.Controls(TB_name).Value = vbNullString
End If
End Sub
Sub TB_exit(TB_name)
If Len(Me.Controls(TB_name).Value) = 0 Then
Me.Controls(TB_name).Value = Me.Controls(TB_name).Tag
Me.Controls(TB_name).Tag = vbNullString
End If
End Sub
Here is a video that shows how to do it when the Userform don't have a Multipage structure: https://www.youtube.com/watch?v=yJ4fnw1zmGU
Setting up properties Tag and Text of textbox I get the placeholder if use a properly Forecolor (&H8000000) but I can't make it works dynamically, that means: delete placeholder text when Enter event occurs and if lenght (Len) of the string inside textbox is zero when Exit event occurs must show the placeholder string again.
Any suggestions? Thanks in advance.

How do I make a userform populate as soon as I press the button to open it?

I have a useform that uses a form, but it only populates when I click the 'clear' button.
Private sub userform1_Initialize()
'stuff populating a page
Then on the same page, I have:
Private Sub ClearButton_Click()
Call UserForm1_Initialize
End Sub
How do I go about using:
Call UserForm1_Initialize
somewhere that populates my form when the form pops up?
I tried putting it in the button to open the form (third line):
Private Sub CommandButton1_Click()
UserForm1.Show
UserForm1_Initialize
End Sub
But I get the error:
Compile error:
Sub or Function not defined.
I tried putting my initialize code in the module instead of in the form, but I get the error:
Run-time error '424'
Object Required
I can't put the entire code in the module, because it will not know where to look for cells, right? What am I doing wrong?
I also tried using public instead of private. Same error as above.
Thank you!
Here's an example of my code to activate a userform from my main macro:
UserForm1.Show
Then, the code for the userform itself (right-click UserForm1, then "View Code"):
Private Sub UserForm_Initialize()
ListBox1.List = ProgramScale
End Sub
This populates the listbox on the form automatically when the form appears.
I think Vincent G is right in the comments above in that the sub title for your userform should not have the form number in it, e.g. UserForm_Initialize not UserForm1_Initialize().

buttons property in worksheet class error on drop down cell with userform pop up excel

I'm trying to have a userform pop-up when I select a specific value from a drop-down menu. It's giving me a "Unable to get the Buttons property of the Worksheet Class" error. I can pull up the userform using a button. When I use the following code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("G9")) Is Nothing Then
Select Case Range("G9")
Case "Sporadic (<5)"
spor_pay.Show
'Cells(2, 2) = "what's wrong"
End Select
End If
End Sub`
I get the error above. However, if I delete the userform (spor_pay is the userform), and try to just enter a value in another cell, the error doesn't pop up. What am I doing wrong with this? Again, if I create a macro from a button, and have the form pop up, there isn't a problem at all.
Thanks for the help!

Excel userform checkboxes null

I have created a userform and added a chekcbox called 'DSSSContact'. I have written some code to check the contents of the userform before saving in the following sub:
Private Sub SaveandClose_Click()
When the form is initialized the checkbox is set greyed out but ticked - which I think means the value is null. The SavandClose sub code includes the following which I thought would make sure that checkbox is either set to true or false:
If DSSSContact = Null Then
MsgBox "DSSS Contact Test"
Exit Sub
End If
However when I save and close the form using the save and close command button without having made a selection on the checkbox the form saves the contents without triggering the message box. All the other checks that I have created to verify that textboxes and combo boxes are populated are working fine. I have also added the following code within the same sub:
Debug.Print Me.DSSSContact.Value
This prints null in the Immediate window
Any help much appreciated. Thanks
Try
If IsNull(DSSSContact.Value) Then
Check boxes in the forms are True = checked or False = Unchecked.
sub test()
MsgBox DSSSContact.Value
If DSSSContact.Value = "False" Then
MsgBox "DSSS Contact Test"
Exit Sub
End If
end sub

Resources