Copy a formula into a cell from a Variable

Dalua

New Member
Joined
Aug 9, 2016
Messages
5
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

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
 
The for cycle had a mistake an "&" was missing

Code:
For Each Nfile In ProjCol
    Formula = "," & Formula & "IFERROR(VLOOKUP($D7,'" & .Path & "\[" & .Name & "]" & Nfile & "'!$D$1:$NP$40,  COLUMN()-3,  FALSE  ),"""")"
Next Nfile

Still does not works
 
Upvote 0

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top