GetValue=ExecuteExcel4Macro

adamjones112182

New Member
Joined
Jan 30, 2020
Messages
24
Office Version
  1. 2016
Platform
  1. Windows
Hi all,

I was wondering if I could get some help with this

I keep on getting Run-Time Error'1004':



Private Function GetValue(Path, File, Sheet, Ref)
' Retrieve 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)
' Debug.Print arg
' Excute an XLM macro
GetValue = ExecuteExcel4Macro(arg) (Error occurs here)

End Function


I have run a debug, but I am still baffled
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Missing Apostrophe
arg = "'" & Path & "[" & File & "]" & Sheet & "'!" & Range(Ref).Range("A1").Address(, , xlR1C1)
 
Upvote 0
When I debug it I get this

C:\Users\Kami\Desktop\VBA BOOK\Excel 2016 VBA Book\Chapter 7\[Goku2.xlsm]sheet1'!R1C1
 
Upvote 0
You should have:
'C:\Users\Kami\Desktop\VBA BOOK\Excel 2016 VBA Book\Chapter 7\[Goku2.xlsm]sheet1'!R1C1

Check the code again.
Check that the path exists, the name of the file, the name of the sheet
 
Upvote 0
I'm glad to help you. Thanks for the feedback.
 
Upvote 0
Here is everything

Paths exist, and the names are correct. I tried another document just in case.


Private Function GetValue(Path, File, Sheet, Ref)
' Retrieve 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)

' Excute an XLM macro
GetValue = ExecuteExcel4Macro(arg)

End Function



Sub TestGetValue2()
Dim p As String, f As String
Dim s As String, a As String
Dim r As Long, c As Long

p = "C:\Users\Kami\Desktop\Excel Courses\Excel Reporting and Dashboards\dashboard (1)\files\Section 4"
f = "Table Sample with Dates.xlsx"
s = "SourceData"
a = "A1"
Application.ScreenUpdating = False
For r = 1 To 100
For c = 1 To 12
a = Cells(r, c) = GetValue(p, f, s, a)
Next c
Next r

MsgBox GetValue(p, f, s, a)
End Sub


When I run debug.print arg

I get this

'C:\Users\Kami\Desktop\Excel Courses\Excel Reporting and Dashboards\dashboard (1)\files\Section 4\[Table Sample with Dates.xlsx]SourceData'!R1C1

I have tried adding the apostrophe to the path code, but now I get an error related to this section of the function

If Dir(Path & File) = "" Then
 
Upvote 0
but now I get an error related to this section of the function
What the message says?


p = "C:\Users\Kami\Desktop\Excel Courses\Excel Reporting and Dashboards\dashboard (1)\files\Section 4"
f = "Table Sample with Dates.xlsx"
Check that you have a folder with that name and a file with that name.

a = Cells(r, c) = GetValue(p, f, s, a)
I don't think that works for you.
Suppose you want the result in a cell.
I guess you want to get several cells. Then it should be something like this:
Cells(r, c).value = GetValue(p, f, s, Cells(r, c).Address(0,0))
 
Upvote 0

Forum statistics

Threads
1,215,475
Messages
6,125,028
Members
449,205
Latest member
Eggy66

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