+ Reply to Thread
Results 1 to 2 of 2

VBA Error Run-Time 91 - object variable or with block variable not set (Duplicate)

Hybrid View

  1. #1
    Registered User
    Join Date
    01-01-2023
    Location
    Sint Maarten
    MS-Off Ver
    2016
    Posts
    6

    VBA Error Run-Time 91 - object variable or with block variable not set (Duplicate)

    Hi, I'm having trouble with a line in my code, it compiles...
    but whenever I run it I get a #91 error
    The code is supposed to identify duplicates in a field.

    Function ValidateEntries() As Boolean
        
        ValidateEntries = True
        
        Dim iPVNr As Variant
        Dim sh As Worksheet
        iPVNr = frmForm.txtPVnr.Value
        
        With frmForm
            
            '~~> Default colors for mandatory fields
            .txtPVnr.BackColor = vbWhite
            .txtDatum.BackColor = vbWhite
            .cmbPost.BackColor = vbWhite
            .cmbLocatie.BackColor = vbWhite
            .cmbOvertreding.BackColor = vbWhite
            .cmbBevinding.BackColor = vbWhite
            .cmbOpslagplaats.BackColor = vbWhite
            .cmbVerbalisant1.BackColor = vbWhite
            .txtOpmerking.BackColor = vbWhite
            '----------------------------------------------------------------------
            
            If Trim(.txtPVnr.Value) = "" Then
            
                MsgBox "Please enter PV nr.!", vbOKOnly + vbInformation, "PVnr"
                ValidateEntries = False
                .txtPVnr.BackColor = vbRed
                .txtPVnr.SetFocus
                Exit Function
                
            End If
            
            '~~> Validating Duplicate entries
            If Not sh.Range("B:B").Find(what:=iPVNr, lookat:=xlWhole) Is Nothing Then
            
                MsgBox "Duplicate PV nr. found!", vbOKOnly + vbInformation, "PVnr"
                ValidateEntries = False
                .txtPVnr.BackColor = vbRed
                .txtPVnr.SetFocus
                Exit Function
                        
            End If

  2. #2
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    15,026

    Re: VBA Error Run-Time 91 - object variable or with block variable not set (Duplicate)

    Where are you setting reference to sh
    Good Luck...
    I don't presume to know what I am doing, however, just like you, I too started somewhere...
    One-day, One-problem at a time!!!
    If you feel I have helped, please click on the [★ Add Reputation] to left of post window...
    Also....Add a comment if you like!!!!
    And remember...Mark Thread as Solved...
    Excel Forum Rocks!!!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Adding attachments to outlook - "Run-time error 91: Object variable or With block variable
    By Deniouz in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-18-2021, 12:22 PM
  2. Replies: 12
    Last Post: 12-28-2019, 12:29 PM
  3. [SOLVED] Run time error '91' object variable/with block variable not set when using Find
    By lynnsong986 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 10-24-2019, 08:33 AM
  4. [SOLVED] Run-time error '91': Object variable or With block variable not set when closing userform
    By bishoposiris in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-10-2016, 08:59 AM
  5. Replies: 0
    Last Post: 04-16-2013, 07:15 AM
  6. Replies: 6
    Last Post: 12-21-2012, 08:03 AM
  7. Replies: 1
    Last Post: 09-25-2012, 08:03 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1