An implementation of Visual Basic that is built into Microsoft products.
Hi @Chaturvedi, Santosh ,
Currently @Marcin Policht approach is a good option if you want to continue using the existing FILTER/BYROW/LAMBDA logic while allowing the workbook path, workbook name, and sheet name to change dynamically .
The macro should do the following:
- Determine the source workbook name from the destination workbook name.
- Open the source workbook if it is not already open.
- Confirm that the required source worksheet exists.
- Insert the FILTER, BYROW, and LAMBDA formula into the required destination cell.
Building on Marcin's example and the folder structure shown in your screenshots, please try the following version:
Option Explicit
Sub UpdateDBCollectiveFormula()
Dim wbDest As Workbook
Dim wbSource As Workbook
Dim wsDest As Worksheet
Dim wsSource As Worksheet
Dim sourceFolder As String
Dim sourceFile As String
Dim sourcePath As String
Dim sourceSheet As String
Dim destinationSheet As String
Dim destinationCell As String
Dim sourceReference As String
Set wbDest = ThisWorkbook
' Change these values to match the actual worksheet names and cell.
sourceSheet = "S1"
destinationSheet = "RSL31"
destinationCell = "J6"
' Check whether the destination worksheet exists.
On Error Resume Next
Set wsDest = wbDest.Worksheets(destinationSheet)
On Error GoTo 0
If wsDest Is Nothing Then
MsgBox "The destination worksheet '" & destinationSheet & _
"' was not found in:" & vbCrLf & _
wbDest.Name, _
vbCritical, _
"Destination worksheet not found"
Exit Sub
End If
' Determine the source workbook name from the destination workbook.
'
' Example:
' Preprocessing_DB Collective_1.xlsm
' becomes:
' DB Collective_1.xlsm
sourceFile = Replace( _
wbDest.Name, _
"Preprocessing_", _
"", _
1, _
1, _
vbTextCompare)
If StrComp(sourceFile, wbDest.Name, vbTextCompare) = 0 Then
MsgBox "The destination workbook name does not contain " & _
"'Preprocessing_'." & vbCrLf & vbCrLf & _
"Current workbook:" & vbCrLf & _
wbDest.Name, _
vbCritical, _
"Destination workbook not recognized"
Exit Sub
End If
' The source and destination workbooks are in sibling folders.
sourceFolder = Replace( _
wbDest.Path, _
"2_Preprocessing_DB_Data", _
"1_DB_Collectives", _
1, _
1, _
vbTextCompare)
If StrComp(sourceFolder, wbDest.Path, vbTextCompare) = 0 Then
MsgBox "The destination folder does not contain " & _
"'2_Preprocessing_DB_Data'." & vbCrLf & vbCrLf & _
"Current folder:" & vbCrLf & _
wbDest.Path, _
vbCritical, _
"Destination folder not recognized"
Exit Sub
End If
sourcePath = sourceFolder & Application.PathSeparator & sourceFile
' Check whether the source workbook is already open.
On Error Resume Next
Set wbSource = Workbooks(sourceFile)
On Error GoTo 0
' Open the source workbook when necessary.
If wbSource Is Nothing Then
If Dir(sourcePath) = "" Then
MsgBox "The source workbook was not found:" & _
vbCrLf & sourcePath & vbCrLf & vbCrLf & _
"Confirm that the source and destination " & _
"workbooks use the expected names and extensions.", _
vbCritical, _
"Source workbook not found"
Exit Sub
End If
On Error GoTo OpenWorkbookError
Set wbSource = Workbooks.Open(sourcePath)
On Error GoTo 0
End If
' Check whether the source worksheet exists.
On Error Resume Next
Set wsSource = wbSource.Worksheets(sourceSheet)
On Error GoTo 0
If wsSource Is Nothing Then
MsgBox "The source worksheet '" & sourceSheet & _
"' was not found in:" & vbCrLf & _
wbSource.Name & vbCrLf & vbCrLf & _
"Enter the exact worksheet-tab name in sourceSheet.", _
vbCritical, _
"Source worksheet not found"
Exit Sub
End If
' The source workbook is open, so the workbook name and
' worksheet name are sufficient for the external reference.
sourceReference = "'[" & _
Replace(wbSource.Name, "'", "''") & "]" & _
Replace(wsSource.Name, "'", "''") & "'!"
' Insert the dynamic-array formula.
wsDest.Range(destinationCell).Formula2 = _
"=FILTER(" & _
sourceReference & "$C$6:$DB$105," & _
"BYROW(" & _
sourceReference & "$G$6:$DB$105," & _
"LAMBDA(r,SUM(r)>0)),""N"")"
Application.Calculate
MsgBox "The source workbook was located and the formula " & _
"was updated successfully.", _
vbInformation, _
"Update completed"
Exit Sub
OpenWorkbookError:
MsgBox "Excel could not open the source workbook:" & _
vbCrLf & sourcePath & vbCrLf & vbCrLf & _
"Error " & Err.Number & ": " & Err.Description, _
vbCritical, _
"Unable to open source workbook"
End Sub
How to add the macro in Excel
- Open the Preprocessing_DB Collective_1 workbook.
- Save the workbook as an Excel Macro-Enabled Workbook, with the .xlsm extension.
- Press Alt + F11 to open the Visual Basic Editor.
- Select Insert > Module.
- Paste the code into the new module.
- Update these three values to match the actual source worksheet, destination worksheet, and destination cell:
sourceSheet = "S1"
destinationSheet = "RSL31"
destinationCell = "J6"
For sourceSheet, enter the exact name displayed on the worksheet tab inside the source workbook. The source workbook name and source worksheet name are not necessarily the same.
- Save the workbook.
- Return to Excel.
- Press Alt + F8.
- Select UpdateDBCollectiveFormula, and then select Run.
Based on the screenshot, I also noticed that the current formula is returning #REF!. This can occur if the source workbook is closed or if the generated workbook/worksheet reference does not match the actual source file.
If you found my response helpful or informative, I would greatly appreciate it if you could follow this guide for your confirmation.
Thank you.