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
 

Some videos you may like

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.

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.
 

Watch MrExcel Video

Forum statistics

Threads
1,118,083
Messages
5,570,124
Members
412,305
Latest member
Mozz
Top