First, is what I'm doing here logical? Second, I keep getting an error on my single quote that begins my filepath.
stuff = Workbooks('\\public\Documents\Amazon Retail\Analysis\[US Retail Quick Reference.xlsx]').Sheets("Quick Reference").Range("A1")
First, is what I'm doing here logical? Second, I keep getting an error on my single quote that begins my filepath.
stuff = Workbooks('\\public\Documents\Amazon Retail\Analysis\[US Retail Quick Reference.xlsx]').Sheets("Quick Reference").Range("A1")
Assuming you have a single instance of Excel, and are not using multiple instances of Excel:
If that file is already open, you have to reference it by its name only, not the full path. If the file isn't yet open, you need to open it first (and then refer to it by its name only).
Change this:
stuff = Workbooks('\\public\Documents\Amazon Retail\Analysis\[US Retail Quick Reference.xlsx]').Sheets("Quick Reference").Range("A1")
To This:
stuff = Workbooks("US Retail Quick Reference.xlsx").Sheets("Quick Reference").Range("A1")
Ensure stuff
is declared as a String
or possibly as a Variant
type (in case A1 might contain non-text or error values).
If you don't know at runtime whether the file is or may be open, then you can fancify your code like so:
Function IsWorkbookOpen(path as String, name as String) As Boolean
Dim wb as Workbook
On Error Resume Next
Set wb = Workbooks(name)
If wb.FullName = path & name Then
IsWorkbookOpen = True
End If
End Function
And then do like:
Dim path as String, fileName as String
path = "\\public\Documents\Amazon Retail\Analysis\"
fileName = "US Retail Quick Reference.xlsx"
If (IsWorkbookOpen(path & fileName)) Then
stuff = Workbooks(fileName).Sheets("Quick Reference").Range("A1").Value
Else
' Do Something Else // UNTESTED:
stuff = ExecuteExcel4Macro("'" & path & "[" & fileName & "]" & _
"Quick Reference'!" & Range("A1").Address(True, True, -4150))
' or:
' Dim wb as Workbook
' Set wb = Workbooks.Open(path + fileName)
' stuff = wb.Sheets("Quick Reference").Range("A1").Value
' wb.Close
End If
For the "Something Else", I'd recommend using the ExecuteExcel4Macro
method for obtaining value from a closed workbook.