An implementation of Visual Basic that is built into Microsoft products.
Try using VBA to build the external FILTER/BYROW/LAMBDA formula dynamically, rather than hard-coding the source folder, workbook name, or sheet name. Since the source workbook and sheet names change, VBA can determine them at runtime.
Note though that Excel dynamic-array functions such as FILTER generally require the referenced source workbook to be open. VBA can open the source workbook before installing the formula.
For example, if your structure is:
...\1_DB_Collectives\DB Collective_1.xlsm
and the preprocessing workbook is:
...\2_Preprocessing_DB_Data\Preprocessing_DB Collective_1.xlsm
than the following macro can derive the source workbook from the preprocessing workbook name. You only need to specify the source worksheet.
Sub UpdateDBCollectiveFormula()
Dim wbDest As Workbook
Dim wbSource As Workbook
Dim wsDest As Worksheet
Dim wsSource As Worksheet
Dim destFolder As String
Dim sourceFolder As String
Dim sourceFile As String
Dim sourceSheet As String
Dim collectiveName As String
Set wbDest = ThisWorkbook
Set wsDest = ActiveSheet
'Get the name of the current preprocessing workbook
collectiveName = Replace(wbDest.Name, "Preprocessing_", "")
'Remove the extension
collectiveName = Left(collectiveName, InStrRev(collectiveName, ".") - 1)
'Source folder is the sibling folder 1_DB_Collectives
destFolder = wbDest.Path
sourceFolder = Replace(destFolder, _
"2_Preprocessing_DB_Data", _
"1_DB_Collectives")
'Source workbook name
sourceFile = collectiveName & ".xlsm"
'Change this to the required source worksheet
sourceSheet = "S1"
'Open source workbook if it is not already open
On Error Resume Next
Set wbSource = Workbooks(sourceFile)
On Error GoTo 0
If wbSource Is Nothing Then
If Dir(sourceFolder & "\" & sourceFile) = "" Then
MsgBox "Source workbook not found:" & vbCrLf & _
sourceFolder & "\" & sourceFile, vbCritical
Exit Sub
End If
Set wbSource = Workbooks.Open(sourceFolder & "\" & sourceFile)
End If
'Check that the source worksheet exists
On Error Resume Next
Set wsSource = wbSource.Worksheets(sourceSheet)
On Error GoTo 0
If wsSource Is Nothing Then
MsgBox "Source worksheet '" & sourceSheet & _
"' was not found in " & sourceFile, vbCritical
Exit Sub
End If
'Create the FILTER/BYROW/LAMBDA formula
wsDest.Range("A1").Formula2 = _
"=FILTER('" & wbSource.FullName & "[" & wbSource.Name & "]" & _
wsSource.Name & "'!$C$6:$DB$105," & _
"BYROW('" & wbSource.FullName & "[" & wbSource.Name & "]" & _
wsSource.Name & "'!$G$6:$DB$105," & _
"LAMBDA(r,SUM(r)>0)),""N"")"
MsgBox "Formula updated successfully.", vbInformation
End Sub
The resulting formula will be equivalent to your current formula, but VBA constructs the workbook path, workbook name, and worksheet name automatically. For example, it would generate a reference conceptually like this:
=FILTER('Q:\...\1_DB_Collectives\[DB Collective_1.xlsm]S1'!$C$6:$DB$105,BYROW('Q:\...\1_DB_Collectives\[DB Collective_1.xlsm]S1'!$G$6:$DB$105,LAMBDA(r,SUM(r)>0)),"N")
You would need to change sourceSheet = "S1" if the source worksheet itself changes. If the worksheet name follows a predictable relationship to the workbook—for example, S1, S2, S3 corresponding to DB Collective_1, DB Collective_2, DB Collective_3—that can also be calculated automatically.
Also, A1 in the example is the cell where the spilled FILTER formula is placed. If your existing formula is in another cell, replace wsDest.Range("A1") with that cell, such as wsDest.Range("C6").
If your source workbook is sometimes .xlsx, .xlsm, or another extension, the macro can also be changed so it searches the 1_DB_Collectives folder and automatically finds the matching DB Collective_1, DB Collective_2, etc., without assuming the extension.
If the above response helps answer your question, remember to "Accept Answer" so that others in the community facing similar issues can easily find the solution. Your contribution is highly appreciated.
hth
Marcin