Katrina
Katrina

Reputation: 51

Can anyone solve this error in my code to do with calling subs in userform? Error: array or user define expected

I want to call different sub depending on what button the user clicks.

Private Sub CommandButton1_Click()
If OptionButton1 = True Then
    Sort A, B, n
    Exit Sub
ElseIf OptionButton2 = True Then
    Reverse A, B, n
    Exit Sub
End If

End Sub

The error comes up as "array or user define expected" but i have checked that I have defined the arrays A, B the same throughout my code. Any ideas?

Upvotes: 0

Views: 33

Answers (1)

Sean
Sean

Reputation: 517

You should be using the property value of the your option buttons. Currently, you're trying to compare if the button itself (the actual physical control) is true.

For example, if you have a Car and want to check whether or not there is a driver, you would specifically check the driver seat and not just look at the entire car or any seat (hopefully this analogy doesn't confuse you).

So with a quick review of your code, it should look like so:

Private Sub CommandButton1_Click()
    If OptionButton1.Value = True Then
        Sort A, B, n            
    Else
        Reverse A, B, n
    End If
End Sub

Also just to help you become a better programmer, use VBA naming conventions for your controls. It helps a lot if someone else has to ever use your code, whether it's a week or a year from now!

Upvotes: 1

Related Questions