Solved

vlookup in Word

Posted on 2016-08-23
9
29 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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

Suggested Solutions

It is often necessary in this forum and others to illustrate Word fields as text with the field delimiters replaced with the curly brackets that the delimiters resemble when field codes are being displayed on the document. This means that the text c…
The Selection object is designed for user interaction. It has a Range property, so it can be used in most places that a Range object can. Recorded macros must use the Selection because they are simply copying what the user is doing. A Range prope…
This video walks the viewer through the process of creating envelopes and labels, with multiple names and addresses. Navigate to the “Start Mail Merge” button in the Mailings tab: Follow the step-by-step process until asked to find the address doc…
In a previous video Micro Tutorial here at Experts Exchange (http://www.experts-exchange.com/videos/1358/How-to-get-a-free-trial-of-Office-365-with-the-Office-2016-desktop-applications.html), I explained how to get a free, one-month trial of Office …

749 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