Reading cell value from a closed Excel file error

Pablo Espressobar

New Member
Joined
Nov 15, 2017
Messages
6
Hello Everyone,

I have a workbook where i want to collect numeric data from another excel files (from only 1 cell) without opening them manually.
I found a neat macro for this, but something is still not okay and i would like to ask for some help.

In the main excel i have a unique string in "C2" i.e. "S2NKL561102" which is the start of my file's name, but after that there is always a space character and an unknown string, that's why i wanted to use a Wildcard.
The template of the searched excel file is always the same, i always want to get the value from cell "E1" from Sheet1.

Using the code above doesn't give a pop-up error, but the value in "F2" cell is always #REF . (it should be a number like 20)

I think it has to do something with the Wildcard in my filename, but i don't know what's the problem with it.

Here is the code:

Sub TestGetValue()
p = "C:\Test\Budget"
f =
Range("C2").Value & " *.xlsx"
s = "Sheet1"
a = "E1"

Range("F2").Value =
GetValue(p, f, s, a)

End Sub


Private Function GetValue(path, file, sheet, ref)
' Retrieves a value from a closed workbook
Dim arg As String
' Make sure the file exists
If Right(path, 1) <> "" Then path = path & ""
If Dir(path & file) = "" Then
GetValue = "File Not Found"
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

Thank you for your help
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Tu support wildcard try this modification of the GetValue function:
Rich (BB code):
Private Function GetValue(path, ByVal file, sheet, ref)
  ' Retrieves a value from a closed workbook
  Dim arg As String
  ' Make sure the file exists
  If Right(path, 1) <> "" Then path = path & "\"
  file = Dir(path & file)
  If file = "" Then
    GetValue = "File Not Found"
    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
 
Upvote 0

Forum statistics

Threads
1,223,908
Messages
6,175,305
Members
452,633
Latest member
DougMo

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