Solved

vlookup in Word

Posted on 2016-08-23
9
32 Views
Last Modified: 2016-09-07
I borrowed this code from another post but the vlookup doesn't appear to be working. The msgbox displays "#N/A." I've double-checked the spreadsheet names and references.

Sub GetResult()
    Dim res As Variant
    Dim wbXL As Excel.Workbook
    Dim wsXL As Excel.Worksheet

Set wbXL = GetObject("C:\Users\Ken\Documents\Custom Office Templates\adjusters.xlsx")
Set wbXL = ActiveWorkbook
'Set wsXL = wbXL.Worksheets("list")

On Error Resume Next
    res = wbXL.Parent.worksheetfunction.VLookup("Valencia", wsXL.Range("adjusterLookupTable"), 4, False)
    On Error GoTo 0
    If IsEmpty(res) Then
        MsgBox "#N/A"
    Else
        MsgBox res
    End If

End Sub
0
Comment
Question by:Ken Schleimer
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
9 Comments
 
LVL 19

Expert Comment

by:Serena Hsi
ID: 41767875
Why not just copy from Excel and Paste Special / Paste Link as Microsoft Excel Worksheet Object?

Then remove the columns you don't need in Word.
0
 
LVL 12

Expert Comment

by:zalazar
ID: 41767879
Can you please try to remove the comment character from
'Set wsXL = wbXL.Worksheets("list")
and change "list" to the correct sheet name.
Also please make sure that the named range "adjusterLookupTable" does exist.
Inside this range, it will look for Valencia in the first column and if found, report the value (via msgbox) in column 4.
1
 

Author Comment

by:Ken Schleimer
ID: 41767892
zalazar
I removed the comment and dimmed a couple more items. The file name, worksheet name, and range name are copied and pasted.

Sub GetResult()
    Dim res As Variant
    Dim wbXL As Excel.Workbook
    Dim wsXL As Excel.Worksheet
    Dim rngXL As Range
    Dim adjName As String

Set wbXL = GetObject("C:\Users\Ken\Documents\Custom Office Templates\adjusters.xlsx")
Set wbXL = ActiveWorkbook
Set wsXL = wbXL.Worksheets("list")
Set rngXL = Range("adjusterLookupTable")
adjName = "Valencia"

On Error Resume Next
    res = wbXL.Parent.WorksheetFunction.VLookup(adjName, rngXL, 4, False)
    On Error GoTo 0
    If IsEmpty(res) Then
        MsgBox "#N/A"
    Else
        MsgBox res
    End If

End Sub
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Accepted Solution

by:
Ken Schleimer earned 0 total points
ID: 41767999
I got it to work. I set the range for the lookup as text rather than as a range.

Sub adjLookup()

Dim objExcel As Object
Dim exWb As Excel.Workbook
Dim exWs As Excel.Worksheet
Dim res As String
Dim adjName As String
Dim rngXL As String
Set objExcel = CreateObject("Excel.Application")

adjName = "Buchele"

Set exWb = objExcel.Workbooks.Open("C:\Users\Ken\Documents\Custom Office Templates\adjusters.xlsx")
Set exWs = exWb.Worksheets("list")
rngXL = "adjusterLookupTable"

On Error Resume Next
    res = exWb.Parent.WorksheetFunction.VLookup(adjName, exWs.Range(rngXL), 4, False)
    On Error GoTo 0
    If IsEmpty(res) Then
        MsgBox "#N/A"
    Else
        MsgBox res
    End If

exWb.Close

Set exWb = Nothing

End Sub
1
 
LVL 12

Assisted Solution

by:zalazar
zalazar earned 500 total points
ID: 41768346
Good to hear that you got it working.
Instead of using CreateObject you could also do it like this.

Sub adjLookup()
  Dim objExcel As New Excel.Application
  Dim exWb As Workbook
  Dim exWs As Worksheet
  Dim res As String
  Dim adjName As String
  Dim rngXL As String, txtWorkbook As String

  adjName = "Buchele"
  rngXL = "adjusterLookupTable"
  txtWorkbook = "C:\Users\Ken\Documents\Custom Office Templates\adjusters.xlsx"

  Set exWb = objExcel.Workbooks.Open(txtWorkbook)
  Set exWs = exWb.Worksheets("list")
  
  On Error Resume Next
  res = exWb.Parent.WorksheetFunction.VLookup(adjName, exWs.Range(rngXL), 4, False)
  On Error GoTo 0
  If IsEmpty(res) Then
    MsgBox "#N/A"
  Else
    MsgBox res
  End If
  
  exWb.Close
  Set exWb = Nothing
End Sub

Open in new window

1
 
LVL 12

Expert Comment

by:zalazar
ID: 41775064
Would you be able to give some feedback ?
Thanks.
0
 

Author Comment

by:Ken Schleimer
ID: 41775170
Hi Zalazar,
I appreciate the quick response from you folks. You provided a tip that helped me get to a solution. Thanks very much.
Ken
0
 
LVL 12

Expert Comment

by:zalazar
ID: 41775306
You're welcome and thanks very much also.
0
 

Author Closing Comment

by:Ken Schleimer
ID: 41787482
Was able to get the macro to work before receiving another suggestion.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I'm writing to share my clumsy experience in using this elegant tool so you can avoid every stupid mistake I made. (I leave it to the authorities to decide if this deserves a place in the Knowledge archives.)  Now that I am on the other side of my l…
This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
In this video, we show how to convert an image-only PDF file into a PDF Searchable Image file, that is, a file with both the image (typically from scanning) and text, which is created in an automated fashion with Optical Character Recognition (OCR) …
This video walks the viewer through the process of creating an MLA formatted document, as well as a bibliography with citations.

615 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question