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

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
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
 
Upvote 0
I was hoping for something a bit more basic- is there a more simple way of doing this?
 
Upvote 0
Possibly - I suggested that as I wasn't sure how to get round the subscript out of range error

Bump :confused:
 
Upvote 0
<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>
 
Upvote 0
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:
 
Upvote 0
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.
 
Upvote 0

Forum statistics

Threads
1,213,497
Messages
6,113,998
Members
448,541
Latest member
iparraguirre89

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
Back
Top