1

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,

  1. if the booking number already exists, tell the user then initialize the userform,
  2. 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
3
  • At what line do you get the error? Commented May 5, 2017 at 9:22
  • If r = BookingNumberTextBox.Text Then Commented May 5, 2017 at 9:25
  • What does debug.print BookingNumberTextBox.Text print out? Commented May 5, 2017 at 9:30

1 Answer 1

3

You aren't checking to see whether the value already exists before you try to use the range:

'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
        'Find was not successful - do whatever you want in that situation
        '...
        '...
    Else
        'Find was successful
        If r.Value = BookingNumberTextBox.Text Then
            MsgBox "Booking Number already exists."
            Call UserForm_Initialize
        Else
            'You shouldn't ever reach this spot because you were searching
            'for BookingNumberTextBox.Text, so r.Value should be equal to it
            MsgBox "Enquiry has been added."
        End If
    End If

End With

Based on the OP's revised code, the final solution can be simplified to:

'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
        MsgBox "Booking Number already exists."
        Call UserForm_Initialize
    End If
End With
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you YowE3K, with a bit of playing around with your code, I got what I wanted which I have updated my original post with!
@Panacea06 - I did wonder whether that was what you were after, but I couldn't see what "Enquiry has been added" meant when nothing was being processed. (But that could be simply due to you having supplied the MCVE that we always like in questions! :) )
@Panacea06 - I updated the answer to show a simplification within the If - there is no need to test whether the value in r is equal to BookingNumberTextBox.Text - it has to be equal because r was set to be a cell that had a value that was equal.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.