Solved

vlookup in Word

Posted on 2016-08-23
9
24 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
  • 4
  • 4
9 Comments
 
LVL 18

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 11

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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 11

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 11

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 11

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction Authors who set out to write any sort of lengthy piece for online submission—be it a long question or comment on a technical form, an article, or a substantial blog entry—often find it useful to work up a draft in an editor other t…
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…
Office 365 is currently available in five editions. Three of them are for business use: Office 365 Business Essentials, Office 365 Business, and Office 365 Business Premium. Two of them are for home/personal use: Office 365 Home and Office 365 Perso…
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 …

867 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now