I am creating a Macros to Update a Formula everytime there is a new value on a defined table. The problem is that even if the Formula is generated correctly in VBA an error appears saying that "Application-defined or object-defined error"
This is my code
This is my code
Code:
Dim ProjCol As New Collection
Dim ProjName, Nfile, Path, Filename, Formula As String
Dim i, Length1, Length2 As Integer
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Application.EnableEvents = False
ActiveSheet.DisplayPageBreaks = False
Set RefBook = ActiveWorkbook
With RefBook
'Moves into a collection all the elements in the table to add into the formula
For i = 2 To Sheets("CFG").ListObjects("Projects").ListRows.Count
ProjName = Sheets("CFG").ListObjects("Projects").DataBodyRange(i, 1)
ProjCol.Add ProjName
Next i
'Generates the first part of the Formula with the first Element
Formula = "=CONCATENATE(IFERROR(VLOOKUP($D7,'" & .Path & "\[" & .Name & "]" & Sheets("CFG").ListObjects("Projects").DataBodyRange(1, 1) & _
"'!$D$1:$NP$40, COLUMN()-3, FALSE ),"""")"
'Add new elements to the Formula
For Each Nfile In ProjCol
Formula = ", " Formula & "IFERROR(VLOOKUP($D7,'" & .Path & "\[" & .Name & "]" & Nfile & "'!$D$1:$NP$40, COLUMN()-3, FALSE ),"""")"
Next Nfile
'Close the formula brakets
Formula = Formula & ")"
MsgBox Formula
'Place the formula in the required cell
Sheets("PLC").Range("K7").Formula = Formula
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
Application.EnableEvents = True
ActiveSheet.DisplayPageBreaks = True
Application.CutCopyMode = False
End With
End Sub