User form Error

johnohio

New Member
Joined
May 12, 2005
Messages
48
Office Version
  1. 2016
Platform
  1. Windows
I am trying to create a user form & I am stuck.

I get this error:
Run-time error '5'
invalid procedure call or argument

Here is the code
VBA Code:
rivate Sub CommandButton1_Click()
'submit button to add new customer

Dim xxx As Worksheet
Set xxx = Worksheets("cmast")
lastrow = xxx.Range("a" & Rows.Count).End(xlUp).Row

'Cust # and  Name Entered
If Me.Custnum = "" Or Me.custname = "" Then
    MsgBox ("You must enter a Customer Number and Customer Name")
    Exit Sub
    End If

' Check Cust # does not exist
If WorksheetFunction.Match(Me.Custnum, xxx.Cells("A:A"), 0) = "" Then ' <<<this line is the issue
    MsgBox ("it worked")
    End If
   
End Sub


Thanks for the help!

 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Cells("A:A") isn't valid syntax, you probably mean Range("A:A")
 
Upvote 0
Mark,
I tried doing what you suggest but now I have a different error message.......


err.JPG
 
Upvote 0
That error just means it isn't finding a match. If you are trying to trap the error when a result is not found then you are better off using Application.Match.
 
Upvote 0
I had to change some things and now I am getting another error.
I am getting a 424 run time error.

Thanks Again for any help....

VBA Code:
Private Sub CommandButton1_Click()
'submit button to add new customer

Dim xxx As Worksheet
Set xxx = Worksheets("cmast")
Dim zz As Worksheet
Set zz = Worksheets("z")

lastrow = xxx.Range("a" & Rows.Count).End(xlUp).Row


'CheckCust # and  Name Entered
If Me.Custnum = "" Or Me.custname = "" Then
    MsgBox ("You must enter a Customer Number and Customer Name")
    Exit Sub
    End If
 
' Check Cust # is not assigned
If Application.Match(Me.Custnum, xxx.Range("A:A"), 0) Is Error Then '<<<<<< problem
        q = 1
    Else
       MsgBox ("Customer number already exists")
       Exit Sub
    End If
   
   
End Sub
 
Upvote 0
Try...
VBA Code:
    If Not IsError(Application.Match(Me.Custnum, xxx.Range("A:A"), 0)) Then
        MsgBox ("Customer number already exists")
        Exit Sub
    Else
        q = 1
    End If
 
Upvote 0
Try this.
VBA Code:
Private Sub CommandButton1_Click()
'submit button to add new customer
Dim xxx As Worksheet
Dim zz As Worksheet
Dim Res As Variant

    Set zz = Worksheets("z")
    Set xxx = Worksheets("cmast")
    
    lastrow = xxx.Range("a" & Rows.Count).End(xlUp).Row

    
    'CheckCust # and  Name Entered
    If Me.Custnum = "" Or Me.custname = "" Then
        MsgBox ("You must enter a Customer Number and Customer Name")
        Exit Sub
    End If
     
    Res = Application.Match(Me.Custnum, xxx.Range("A:A"), 0)
    ' Check Cust # is not assigned
    If IsError(Res) Then
        q = 1
    Else
        MsgBox ("Customer number already exists")
        Exit Sub
    End If
      
End Sub

Note, it might be worth checking what type of data you have in the range you are searching in - if it's numeric then you aren't going to get a match for the text value from the controls of the userform.
 
Upvote 0
Solution
Got it working now. Thanks to both of you.
(and thanks for the reminder about data types!)
 
Upvote 0

Forum statistics

Threads
1,214,429
Messages
6,119,435
Members
448,898
Latest member
dukenia71

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top