Solved

vlookup in Word

Posted on 2016-08-23
9
30 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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

I would like to show you some basics you can do with Mailings in MS Word. It´s quite handy feature you can use for creating envelopes, labels, personalized letters etc. First question could be what is this feature good for? Mailing can really he…
Shortcuts in Word Just the other day I had a training for Microsoft and they wanted me to show how well the new Windows and Office behaved on a touch device, which by the way is great, but it was only then that I realized that using keyboard shortc…
This video teaches the viewer how to align pictures around text while keeping the text properly aligned in the document.
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…

751 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