GetValue from closed worksheet - simple question

tammiebrannon

New Member
Joined
Dec 28, 2011
Messages
2
I am using the function below. It works fine if I call it in the following manner;

BillableHours = GetValue(fldpath, ThisDateFileName, "IngenuitE Time Card", "D16")

with the "D16" hard coded.

I need to search the closed file and determine where the billable hours will be stored, so I have created a var string;

billableAddress As String

within the loop and when location is found ;

billableAddress = Cells(totalRow, c + 1).Address(False, False)

and then I call the GetValue as follows;

BillableHours = GetValue(fldpath, ThisDateFileName, "Time Card", BillableAddress)

T then recieve the error;

Run-time error "13':

Type mismatch


Private Function GetValue(path, file, sheet, ref)
' Retrieves a value from a closed workbook
' Note: You cannot use this function in a worksheet formula.
Dim arg As String
' Make sure the file exists
' On Error GoTo ErrHandler
If Right(path, 1) <> "\" Then path = path & "\"
If Dir(path & file) = "" Then
'GetValue = "File Not Found"
GetValue = 0
Exit Function
End If
' Create the argument
arg = "'" & path & "[" & file & "]" & sheet & "'!" & _
Range(ref).Range("A1").Address(, , xlR1C1)
' Execute an XLM macro
GetValue = ExecuteExcel4Macro(arg)
End Function



Please help ...new VBA as you can tell.... this is driving me crazy!!!
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
I'm guessing you need replace with the following?

Code:
Dim billableAddress As Range
.
.
.
Set billableAddress = Cells(totalRow, c + 1).Address(False, False)
 
Upvote 0
Welcome to the board.

Try this:

Code:
Function GetValue(ByVal sPath As String, sFile As String, _
                  sSht As String, sRng As String) As Variant
    ' Retrieves a value from a closed workbook
    ' VBA only
    Dim sArg        As String
 
    If Right(sPath, 1) <> "\" Then sPath = sPath & "\"
 
    If Len(Dir(sPath & sFile)) Then
        sArg = "'" & sPath & _
               "[" & sFile & "]" & _
               sSht & "'!" & _
               Application.ConvertFormula(sRng, xlA1, xlR1C1, True)
        GetValue = ExecuteExcel4Macro(sArg)
    Else
        GetValue = "File not found"
    End If
End Function

If that doesn't work, put Option Explicit as the first line at the top of the module and make sure you don't have a typo in a variable name.
 
Upvote 0

Forum statistics

Threads
1,223,896
Messages
6,175,264
Members
452,627
Latest member
KitkatToby

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