1

I'm very new to VBA so I apologise if the question seems silly: I have set up a UserForm with some controls in it, and created a function called ResetMyField as per below:

Function ResetMyField(MyField As Object)
    If MyField = ProjectReference Then
        'do something different and then
    End If
    MyField.Value = ""
End Function

When I call this function using ResetMyField(ProjectReference) VBA comes out with a 424 error (Object Required). Should I be declaring MyField as a different type of variable in the function?

Both the function and the point at which I call it are inside the Userform module.

Any help would be much appreciated.

GSerg
  • 76,472
  • 17
  • 159
  • 346
JaxHax
  • 11
  • 2
  • ProjectReference is the name of one of my Comboboxes on the Userform - sorry if that wasn't clear – JaxHax Nov 26 '18 at 12:51
  • Which line throws that error? Any why do you use a `function` if you don't return something you can use a procedure (`Sub`). Can you [edit] your question and add the code how exactly you call `ResetMyField`? If you are testing 2 objects for equality I recommend to test their name for equality `MyField.Name = ProjectReference.Name` – Pᴇʜ Nov 26 '18 at 12:59

1 Answers1

2

ResetMyField(ProjectReference), with the parentheses, tries to pass the default property of the ProjectReference combobox into ResetMyField. The default property of a combobox is Value, and that is not an Object, and ResetMyField expects an Object, hence the error ("Object Required").

Remove the parentheses:

ResetMyField ProjectReference

Also note that If MyField = ProjectReference Then, again, tries to compare default properties of MyField and ProjectReference, which in case of comboboxes will mean If MyField.Value = ProjectReference.Value Then.
If you want to know if MyField is ProjectReference, then it's

If MyField Is ProjectReference Then
GSerg
  • 76,472
  • 17
  • 159
  • 346