UPDATE
see original question below.
Code is now:
Function Getvalue(CellRange As String) As Variant
Dim path As String
Dim file As String
Dim sheet As String
Dim str As String
set rng = Range(Cell Range)
yr = Format(CStr(Now), "yy")
monthn = Format(CStr(Now), "mmmm")
path = "C:\Folder\Subfolder\"
file = "File-" & monthn & ", FY''" & yr & ".xls"
sheet = "Sheet1"
rng = Range(CellRange)
str = "'" & path & "[" & file & "]" & sheet & "'!" & rng.Address( , ,xlR1C1)
GetValue = Application.ExecuteExcel4Macro(str)
End Function
Sub Routine()
Call GetValue("A1")
End Sub
When I use the subroutine in VBA and go through the function with F8, with "GetValue" being watched, The value of GetValue returns what I want. It pulls the data from the closed workbook. However when I put =GetValue("A1") into a cell on my worksheet, it returns #VALUE!
old:
I have a worksheet that references data from one workbook in multiple columns. My issue is that the workbook is updated and renamed every month (and every year). I could go through and change the link every month but I feel like there should be an automated way to do it.
Let's say the filename is (File-August, FY''20) so every month/year I would be changing the month/year in the filename.
I don't want to use the indirect function as I would like to be able to keep the reference workbook closed.
But basically I want an alternative to:
=INDIRECT("'C:\Folder\Subfolder\[File-" & TEXT(TODAY(),"mmmm") & ", FY''" & TEXT(TODAY(),"yy") & ".xls]Sheet1'!" & A1)
(Full path is 'C:\Folder\Subfolder[File-August, FY''20.xls]Sheet1'!A1)
Month, year, and cell are dynamic.
I am a beginner with VBA, so I've pieced together some code from research, but I know there are probably many things wrong with it. I tried to make it so that I could use a UDF and just call the cell, but I don't know if that is even possible.
Function Getvalue(CellRange As String) As Variant
Dim path As String
Dim file As String
Dim sheet As String
Dim str As String
Dim rng As Range
yr = Format(CStr(Now), "yy")
monthn = Format(CStr(Now), "mmmm")
path = "C:\Folder\Subfolder\"
file = "File-" & monthn & ", FY''" & yr & ".xls"
sheet = "Sheet1"
rng = Range(CellRange)
str = "'" & path & "[" & file & "]" & sheet & "'!" & rng
Result = ExecuteExcel4Macro(str)
End Function
This returns #VALUE
when entered.
thank you!