MikeAuz1979
Programmer
Hi,
Using Excel 2000 I have a function that simplifies the vlookups in cell formulas (Constructed from various posts in this forum.)
This works great when the file 'Margin Report - Data.xls' is open but not when it's closed, any ideas on how to put in a file location so it can still find the file when it's closed (The error I'm getting when running this with the file closed is 'Subscript out of range')
I tried simply pasting in the folder location but this didn't work. [i.e set ws = Workbooks("C:\FolderName\Margin Report - Data.xls") etc.]
I also tried opening the file at the start of this code but excel appears not to let you open other excel files from a function (Works ok from a sub)
Thanks heaps for any help
Mike
----Code Start---
Function LoadVLookup(SiteNumber As Object, ColumnNo As Long) As Variant
On Error GoTo ErrHandler
Dim ws As Worksheet, rng As Range, Result As Variant
Set ws = Workbooks("Margin Report - Data.xls").Sheets("Margin Report - Forecast Export")
Set rng = ws.Range("B:AC")
Result = Application.VLookup(SiteNumber, rng, ColumnNo, 0)
If Not IsError(Result) Then
LoadVLookup = Result
Else
LoadVLookup = 0
End If
Exit Function
ErrHandler:
MsgBox Err.Description
End Function
----Code End---
Using Excel 2000 I have a function that simplifies the vlookups in cell formulas (Constructed from various posts in this forum.)
This works great when the file 'Margin Report - Data.xls' is open but not when it's closed, any ideas on how to put in a file location so it can still find the file when it's closed (The error I'm getting when running this with the file closed is 'Subscript out of range')
I tried simply pasting in the folder location but this didn't work. [i.e set ws = Workbooks("C:\FolderName\Margin Report - Data.xls") etc.]
I also tried opening the file at the start of this code but excel appears not to let you open other excel files from a function (Works ok from a sub)
Thanks heaps for any help
Mike
----Code Start---
Function LoadVLookup(SiteNumber As Object, ColumnNo As Long) As Variant
On Error GoTo ErrHandler
Dim ws As Worksheet, rng As Range, Result As Variant
Set ws = Workbooks("Margin Report - Data.xls").Sheets("Margin Report - Forecast Export")
Set rng = ws.Range("B:AC")
Result = Application.VLookup(SiteNumber, rng, ColumnNo, 0)
If Not IsError(Result) Then
LoadVLookup = Result
Else
LoadVLookup = 0
End If
Exit Function
ErrHandler:
MsgBox Err.Description
End Function
----Code End---