+ Reply to Thread
Results 1 to 6 of 6

VBA Run-time error 1004 caused by blank vlookup value

  1. #1
    Registered User
    Join Date
    09-27-2012
    Location
    Perth
    MS-Off Ver
    Excel 2003
    Posts
    20

    VBA Run-time error 1004 caused by blank vlookup value

    Hi

    I’m looking for some help.

    I have a user form which contains two vlookup's. The purpose of the form is lookup a post code entered in a text box and confirm, if the post code is available in the list and if so, what products are available in this postal region and who can provide it.

    When I enter a post code the form either provides me with the relevant details regarding that region or a message box appears to advise the post code is not listed. This is fine and exactly what I am looking for. I have a button to clear the user form and once this has been selected and I click in the empty text box where I want to enter a new post code, I get the following error message:

    Run-time error '1004'

    Unable to get the VLookup property of the WorksheetFunction class

    From looking at similar posts online, it appears that the issues possibly lies with the text box attempting to lookup a blank value which I want to avoid; however I am a total novice in regards to VBA and can’t take the next step to amend the code.

    Can anyone help adjust my code to stop the ProductBox and PartnerBox attempting to look up a value when I click on the PostCodeBox after clearing the form?

    Private Sub PostCodeBox_AfterUpdate()
    'Check to see if value exists
    If WorksheetFunction.CountIf(Sheet21.Range("A:A"), Me.PostCodeBox.Value) = 0 Then
    MsgBox "No Products Available In This Post Code District"
    Me.PostCodeBox.Value = ""
    Exit Sub
    End If
    'Lookup values based on first control
    With Me
    .ProductBox = Application.WorksheetFunction.VLookup((Me.PostCodeBox), Sheet21.Range("Lookup"), 2, 0)
    .PartnerBox = Application.WorksheetFunction.VLookup((Me.PostCodeBox), Sheet21.Range("Lookup"), 3, 0)
    End With
    End Sub

  2. #2
    Forum Expert Logit's Avatar
    Join Date
    12-23-2012
    Location
    North Carolina
    MS-Off Ver
    Excel 2019 Professional Plus - 2007 Enterprise
    Posts
    7,021

    Re: VBA Run-time error 1004 caused by blank vlookup value

    Try adding "On Error Resume Next" at the top of your macro :

    Please Login or Register  to view this content.
    If that doesn't work, please post your project for review.

  3. #3
    Forum Expert
    Join Date
    06-25-2009
    Location
    Sofia, Bulgaria, EU
    MS-Off Ver
    Excel 2003-2013
    Posts
    1,290

    Re: VBA Run-time error 1004 caused by blank vlookup value

    On Error Resume Next SHOULD NOT be in the beginning of the sub. This will obfuscate any errors in the sub. Put it as close the the Vlookup line as possible.
    Please Login or Register  to view this content.
    If you are pleased with a member's answer then use the Star icon to rate it.

  4. #4
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,914

    Re: VBA Run-time error 1004 caused by blank vlookup value

    On Error Resume next is just a way to avoid writing extensive error handling and should be used with caution.
    Worksheetfunction.formula always go into debug in case of error.
    Application.formula stores the error in the resulting variable and can be trapped using IsError(variable) or Not IsError(variable).

    Please Login or Register  to view this content.
    Avoid using Select, Selection and Activate in your code. Use With ... End With instead.
    You can show your appreciation for those that have helped you by clicking the * at the bottom left of any of their posts.

  5. #5
    Registered User
    Join Date
    09-27-2012
    Location
    Perth
    MS-Off Ver
    Excel 2003
    Posts
    20

    Re: VBA Run-time error 1004 caused by blank vlookup value

    Thanks for all the help, I went with bakerman2's code and it works perfectly. Really appreciate it, thanks again

  6. #6
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,914

    Re: VBA Run-time error 1004 caused by blank vlookup value

    You're welcome and thanks for rep+.

+ 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. VBA Vlookup Problem - run time error '1004'
    By rsami in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 11-11-2013, 11:13 AM
  2. [SOLVED] Vlookup assistance - getting Run-Time error '1004'
    By dank_01 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 07-30-2012, 06:51 PM
  3. [SOLVED] VLookup() Run-Time Error '1004'
    By lloydgodin in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 04-12-2012, 12:13 PM
  4. Run-time error '1004': Unable to get the VLookup property of the WorksheetFunction c
    By BlondOIverBlue in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-02-2010, 12:37 AM
  5. worksheetfunction.vlookup returning run-time error (1004)
    By Mervil in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-19-2010, 01:41 AM
  6. Problem with Vlookup macro - Run Time Error '1004'
    By obc1126 in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 12-09-2008, 09:56 PM
  7. [SOLVED] Mysterious Error 1004 caused by AutoFilter?
    By Ken Johnson in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 03-05-2006, 04:10 PM
  8. [SOLVED] VBA VLookup Problem: Run-Time error '1004'
    By hurlbut777 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 02-04-2005, 09:06 PM

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