Compile error: Argument not optional

Bandito1

Board Regular
Joined
Oct 18, 2018
Messages
239
Office Version
  1. 2016
Platform
  1. Windows
Hello all,

I have a textbox named txtCorrectby
I would like that the data that is filled in this textbox matches a list.
If not the user should be warned.

I got this code for it;
VBA Code:
Private Sub txtCorrectBy_AfterUpdate()
Dim rngCell As Range
Dim bMatch As Boolean
Dim vVal
    
    vVal = txtCorrectBy.Text
    bMatch = WorksheetFunction.CountIf(Range(Sheets("PersonSearch")("B2:B153"), vVal)) > 0
         
     'If a match was found then alert the user
    If bMatch Then MsgBox ("Name already exsists.")
     
End Sub

But it gives Compile error: Argument not optional

Somebody knows why?
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Try
Excel Formula:
bMatch = WorksheetFunction.CountIf(Sheets("PersonSearch").Range("B2:B153"), vVal)) > 0
 
Upvote 0
Since vba is being used, I would consider just using functions/methods etc native to that environment, rather than calling up a worksheet function.

VBA Code:
Private Sub txtCorrectBy_AfterUpdate()
  Dim rngCell As Range
  Dim vVal As String
    
  vVal = txtCorrectBy.Text
  Set rngCell = Sheets("PersonSearch").Range("B2:B153").Find(What:=vVal, LookAt:=xlWhole, MatchCase:=False)
  
  'If a match was found then alert the user
  If Not rngCell Is Nothing Then MsgBox "Name already exists."
End Sub
 
Upvote 0
Solution
Glad we could help & thanks for the feedback.
 
Upvote 1

Forum statistics

Threads
1,223,767
Messages
6,174,395
Members
452,561
Latest member
amir5104

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