Creating UserForm on VBA

723 Views Asked by At

I am following the exact steps on creating userform on this website http://www.excel-easy.com/vba/userform.html. I followed all the steps but I can't make it work.

Please see the image for the error. Excel Error

When I pasted this code.

Private Sub UserForm_Initialize()

'Empty NameTextBox
 NameTextBox.Value = ""

 'Empty PhoneTextBox
 PhoneTextBox.Value = ""

'Empty CityListBox
CityListBox.Clear

'Fill CityListBox
With CityListBox
.AddItem "San Francisco"
.AddItem "Oakland"
.AddItem "Richmond"
End With

'Empty DinnerComboBox
DinnerComboBox.Clear

'Fill DinnerComboBox
With DinnerComboBox
.AddItem "Italian"
.AddItem "Chinese"
.AddItem "Frites and Meat"
End With

 'Uncheck DataCheckBoxes
DateCheckBox1.Value = False
DateCheckBox2.Value = False
DateCheckBox3.Value = False

'Set no car as default
CarOptionButton2.Value = True

'Empty MoneyTextBox
MoneyTextBox.Value = ""

'Set Focus on NameTextBox
 NameTextBox.SetFocus
 End Sub

this code will have error

Private Sub CommandButton1_Click()

DinnerPlannerUserForm.Show

End Sub

Please see the website first.

2

There are 2 best solutions below

4
On BEST ANSWER

One of your controls is not named correctly. Adding Option Explicit to the top of your code modules will usually allow you to catch this type of error early.

enter image description here

4
On

It seems that either:

  • you did not rename the UserForm1 to DinnerPlannerUserForm, or
  • you did not rename one of the controls that is mentioned in your Initialize code, or
  • you have never made a call like
    Set MyDinnerPlanner = New DinnerPlannerUserForm
    (variables that refer to objects start as Null; they need to be Set to a New instance at some point before you can use them.)

If you click on the "Object required" error dialog's "Debug" command button, the VBA IDE will take you to a line of code. That line of code will probably have a statement of the form
<ObjectInstanceName>.<MethodName>(<parameters>) or
<ObjectInstanceName>.<PropertyName> = <Value>.
The "Object required" error is telling you that either VBA does not recognize the <ObjectInstanceName> or <ObjectInstanceName> Is Null.