Greetings - I'm asking for help resolving the error(s) I get below or finding another solution to my goal.
Goal: VBA to get the values from cells 'A1' and 'A2' on 'Sheet2' of the XLFileName workbook (without opening) and assign them to the variables TotColumns and TotRows.
My code is following the examples from these websites (same creator):
As I step through the code and hover over 'xlR1C1 = -4150' and 'Range(ref) value <Object Variable or With block variable not set>'
Notes:
1) The code below is modified from my total code. 'InfoDataPath' and 'XlFileName' variables are assigned by user input which has been cut out to shorten the code. The values assigned here are what are returned from the other code.
2) I am writing VBA in a computer-aided drafting program called Microstation (I have, what I believe to be, the right references selected from the IDE --> Tools --> References. Pic of my selections are below the code)
3) My data (Excel file) is held in cloud storage (Google Drive) - not sure if that makes a difference or even pertains to the error.
Thanks in advance for helping.
Goal: VBA to get the values from cells 'A1' and 'A2' on 'Sheet2' of the XLFileName workbook (without opening) and assign them to the variables TotColumns and TotRows.
My code is following the examples from these websites (same creator):
When I run my code, I get 'Run-time error '1004': Method 'Range' of object '_Global' failed' on the Private Function line: 'arg = "'" & path &.....' As I step through the code and hover over 'xlR1C1 = -4150' and 'Range(ref) value <Object Variable or With block variable not set>'
Notes:
1) The code below is modified from my total code. 'InfoDataPath' and 'XlFileName' variables are assigned by user input which has been cut out to shorten the code. The values assigned here are what are returned from the other code.
2) I am writing VBA in a computer-aided drafting program called Microstation (I have, what I believe to be, the right references selected from the IDE --> Tools --> References. Pic of my selections are below the code)
3) My data (Excel file) is held in cloud storage (Google Drive) - not sure if that makes a difference or even pertains to the error.
Thanks in advance for helping.
VBA Code:
Option Explicit
Sub GetXlFileInfo()
Dim XlFileName As String, InfoDataPath As String
Dim TotColumns As Long, TotRows As Long
Dim p As String, f As String, c As String, r As String, s As String
InfoDataPath = "G:\My Drive\Test\TESTING\ProjLoc\Project Name"
XlFileName = "Data Needed (2021-12-18).xlsx"
p = InfoDataPath & "\"
f = XlFileName
c = "A1"
r = "A2"
s = "Sheet2"
TotColumns = ValueFromClosedWorkbook(p, f, s, c)
TotRows = ValueFromClosedWorkbook(p, f, s, r)
End Sub
VBA Code:
Private Function ValueFromClosedWorkbook(path, file, sheet, ref)
Dim arg As String
arg = "'" & path & "[" & file & "]" & sheet & "'!" & Range(ref).Range("A1").Address(, , xlR1C1) ' <--- This Line gives the error!!!
ValueFromClosedWorkbook = ExecuteExcel4Macro(arg)
End Function