Solved

vlookup in Word

Posted on 2016-08-23
9
23 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 17

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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

If you work with Word a lot, you probably use styles. If you use styles a lot, you've probably balled your fist more often than not when working with the ribbon. In Word 2007/2010, one of the things that I find missing when using styles is a quic…
A few years ago I was very much a beginner at VBA, and that very much remains the case today.  I'll do my best to explain things as I go in the hope that other beginners can follow.  If you just want to check out a tool that creates a Select Case fu…
This video teaches the viewer how to align pictures around text while keeping the text properly aligned in the document.
This video shows the viewer how to set up and create Footnotes in their document. Click on the References tab: Select "Insert Footnote": Type in desired text:

708 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

18 Experts available now in Live!

Get 1:1 Help Now