VBA Vlookup

jam24

New Member
Joined
Nov 30, 2005
Messages
3
Hi - I'm hoping someone can help with this. I am trying to include a vlookup in my code which reads from a file that I want to remain closed. At the moment I am having to open the relevant file and then close it once the vlookup is done. I have something like this at the moment:

Workbooks.OpenText FileName:="C:\Test\Trial1.xls"

Output = Application.VLookup(Cells(3, 1), Workbooks("Trial.xls").Worksheets("Sheet1").Range("A4:k1000"), 9, False)

Workbooks("Trial1.xls").Close

Anyone know how I can get around the opening and closing? Thanks a lot in advance
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.

MichaelRo

Well-known Member
Joined
Jun 7, 2004
Messages
549
A slight twist on the GetValuesFromAClosedWorkbook theme:

Code:
'Values from a closed workbook
Sub ClosedWorkbookValues()
GVFCWrk "C:\Test", "Trial1.xls", "Sheet1", "A1", "A3", "A4:k1000", 9, 0
End Sub

Sub GVFCWrk(fPath As String, FName As String, Shname, OutputRge As String, _
LookupVal As String, TableArryRge As String, LookupCol As Integer, rangeLkup As Integer)

With ActiveSheet.Range(OutputRge)
.Formula = "=VLOOKUP(" & LookupVal & ",'" & fPath & "\[" & FName & "]" _
& Shname & "'!" & TableArryRge & "," & LookupCol & "," & rangeLkup & ")"
'.Value = .Value
End With
End Sub

Creates a formula in A1 of the activesheet
 

jam24

New Member
Joined
Nov 30, 2005
Messages
3
I was hoping for something a bit more basic- is there a more simple way of doing this?
 

MichaelRo

Well-known Member
Joined
Jun 7, 2004
Messages
549
Possibly - I suggested that as I wasn't sure how to get round the subscript out of range error

Bump :confused:
 

just_jon

Legend
Joined
Sep 3, 2002
Messages
10,473

ADVERTISEMENT

<font face=Courier New><SPAN style="color:#00007F">Sub</SPAN> test()
<SPAN style="color:#00007F">With</SPAN> [a1]
    .Formula = "=vlookup(a3, '[Book1.xls]Sheet1'!A4:k1000, 9, False)"
    .Value = .Value
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN>
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN>
</FONT>
 

MichaelRo

Well-known Member
Joined
Jun 7, 2004
Messages
549
Seems to work:

Code:
Sub test()
With [a1]
    .Formula = "=vlookup(a3,'C:\Test\[Trial1.xls]Sheet1'!A4:K1000,2,0)"
    .Value = .Value
End With
End Sub

Could have sworn I tried that... :confused:
 

jam24

New Member
Joined
Nov 30, 2005
Messages
3
Thanks for the answers- two questions.

1) Is there anyway that once the value has been found, the cell contents can read the value rather than the formula
2) anyway of speeding this up? It is within a loop and it takes way too long

thanks again for the help.
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,163,990
Messages
5,834,761
Members
430,318
Latest member
ZackChua95

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top