Results 1 to 8 of 8
  1. #1
    Euler271 is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Dec 2017
    Posts
    63

    VLOOKUP problem

    I'm in Access VBA trying to create and manipulate an Excel workbook. I fill two worksheets and need to use the Excel VLOOKUP formula in one worksheet to lookup values in the other worksheet. I need to fill a column of cells with the VLOOKUP function but when the code reaches the line:



    xlWs.Range("O2:O" & xlWs.Cells.SpecialCells(xlLastCell).Row).FormulaR1 C1 = "=VLOOKUP(P2,OK!$G$2:$G$" & LastStateRow & ",1,FALSE)"

    I get a 1004 error "Application-defined or object-defined error".

    The following previous line:

    xlWs.Range("O2:O" & xlWs.Cells.SpecialCells(xlLastCell).Row).FormulaR1 C1 = "=RC[1]&RC[-12]"

    works just fine.

    I read that VBA doesn't like #N/A and that might be what's going on here. My question is how can I insert the VLOOKUP formula in the column cells even if VBA encounters #N/A, if that's the problem.

    Thanks.

  2. #2
    JoeM is offline VIP
    Windows 7 32bit Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    The second one is using R1C1 range referencing. The first one it not.

    So change it to:
    Code:
    xlWs.Range("O2:O" & xlWs.Cells.SpecialCells(xlLastCell).Row).Formula = "=VLOOKUP(P2,OK!$G$2:$G$" & LastStateRow & ",1,FALSE)"

  3. #3
    Euler271 is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Dec 2017
    Posts
    63
    I should've mentioned that I tried it with R1C1 referencing:

    xlWs.Range("O2:O" & xlWs.Cells.SpecialCells(xlLastCell).Row).FormulaR1 C1 = "=VLOOKUP(RC[1],OK!$R$C[-8]:$R[" & LastStateRow & "]$C[-8],1,FALSE)"

    with the same result.

  4. #4
    JoeM is offline VIP
    Windows 7 32bit Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    Try incorporating the Excel IFERROR formula into your equation, which tells Excel what to return instead of an error.
    See: https://exceljet.net/formula/vlookup-without-na-error

  5. #5
    Euler271 is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Dec 2017
    Posts
    63
    That's great. I'll do just that. Thanks for responding.

  6. #6
    JoeM is offline VIP
    Windows 7 32bit Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    You are welcome.

  7. #7
    Missinglinq's Avatar
    Missinglinq is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,018
    Have you set a reference for the Excel library?

    This might help...for Access 2007 (which is also good for v2010, I believe)

    http://msdn.microsoft.com/en-us/libr...ice.12%29.aspx

    Linq ;0)>
    The problem with making anything foolproof...is that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

  8. #8
    Euler271 is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Dec 2017
    Posts
    63
    Thanks for replying.

    I haven't read the entire article but it appears to be about asking Excel to do a calculation and then returning the result to Access. That's not what I'm trying to do in this case.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Vlookup
    By abrahamgluck in forum Access
    Replies: 6
    Last Post: 12-28-2017, 03:45 AM
  2. Excel problem (vlookup)
    By sdc1234 in forum Misc
    Replies: 3
    Last Post: 02-20-2015, 10:53 AM
  3. Vlookup to DLookup Help
    By zoomzoomsd in forum Forms
    Replies: 2
    Last Post: 11-14-2013, 09:00 PM
  4. VLookup
    By chethan1333 in forum Access
    Replies: 1
    Last Post: 02-23-2013, 10:14 AM
  5. Vlookup Question?
    By jjm3066 in forum Access
    Replies: 3
    Last Post: 11-15-2011, 11:55 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums