I am getting the
Run-time error '91': Object variable or With block variable not set
on this particular bit of code, I can't work out what is wrong with it ...
'Booking Number Validation
With Sheets("New Enquiries")
Dim r As Excel.Range
Set r = .Range("A:A").Find(What:=BookingNumberTextBox.Text, LookAt:=xlWhole, MatchCase:=False)
If r = BookingNumberTextBox.Text Then
MsgBox ("Booking Number already exists.")
Call UserForm_Initialize
Else
MsgBox ("Enquiry has been added.")
End If
End With
I am getting the error on line If r = BookingNumberTextBox.Text Then
The point of this is to look when adding data via the userform,
- if the booking number already exists, tell the user then initialize the userform,
- if it doesn't exist, add the data and confirm entry.
EDIT: Based on YowE3K's answer, I amended his code and came up with the following;
'Booking Number Validation
With Sheets("New Enquiries")
Dim r As Excel.Range
Set r = .Range("A:A").Find(What:=BookingNumberTextBox.Text, LookAt:=xlWhole, MatchCase:=False)
If r Is Nothing Then
MsgBox "Enquiry has been added."
Else
If r.Value = BookingNumberTextBox.Text Then
MsgBox "Booking Number already exists."
Call UserForm_Initialize
End If
End If
End With
If r = BookingNumberTextBox.Text Thendebug.print BookingNumberTextBox.Textprint out?