I'm trying to add an Array formula into a Table column on a worksheet. I'm using Index Match and splitting the formula because it gets quite big with all the file names. I use it in other parts of the code by splitting it and it works (but using different references, etc).
I keep getting an error "Type mismatch" when I try and replace/insert the Match formula into the Index formula. When I do it manually (by copying the variable string values) it works...
ATPFile = Dir(DataPath & "\*ATP*COMBINED.xls*")
ATPFileF = DataPath & "\" & ATPFile
Set wb = Workbooks.Open(ATPFileF)
'Add ATP data from ATP raw data file
sheetname = Worksheets("ATP").Name
'Get last row with data in ATP file
Dim rowCountSource As Integer
Dim ATPRowRange As Range
Dim sFomrula2 As String
With wb.Worksheets("ATP")
rowCountSource = .Cells(.Rows.Count, 2).End(xlUp).Row
End With
ref = DataPath & "\[" & ATPFile & "]" & sheetname
sFormula = "=INDEX('" & ref & "'!$P$1:$P$" & rowCountSource & ",""X_X_X"",1)"
sFormula2 = "MATCH([@Article]&[@Site],('" & ref & "'!$B$1:$B$" & rowCountSource & ")&('" & ref & "'!$D$1:$D$" & rowCountSource & "),0)"
'Add ATP info
With ws.ListObjects("Table_DCvenSDCdata")
With .ListColumns("ATP").DataBodyRange
.NumberFormat = "General"
With .Cells(1, 1)
.FormulaArray = sFormula
.Replace """X_X_X""", sFormula2
End With
.FillDown
.Value = .Value
End With
End With
The error pops up at .Replace """X_X_X""", sFormula2
The file references and names are all correct. I'm not sure where in my match formula the issue is...
This is what the sFormula string is:
=INDEX('C:\_Store\10.05.2020\[T MA0 ATP 18.08.2020.xls COMBINED.xls]ATP'!$P$1:$P$126,"X_X_X",1)
And This is what the sFormula2 string is that replaces the "X_X_X" in the Index formula:
MATCH([@Article]&[@Site],('C:\_Store\10.05.2020\[T MA0 ATP 18.08.2020.xls COMBINED.xls]ATP'!$B$1:$B$126)&('C:\_Store\10.05.2020\[T MA0 ATP 18.08.2020.xls COMBINED.xls]ATP'!$D$1:$D$126),0)
Any advice will be greatly appreciated!
""X_X_X""
with0
, then replace that instead. – Scott CranerCells(1.1).Replace
would replace contents in value of Cells(1,1), it wont edit formula the way you are doing here. Try putting line ofsFormula2
before that ofsFormula
& then in place ofsFormula = "=INDEX('" & ref & "'!$P$1:$P$" & rowCountSource & ",""X_X_X"",1)"
trysFormula = "=INDEX('" & ref & "'!$P$1:$P$" & rowCountSource & ", sFormula2 ,1)"
– Puntal.Replace """X_X_X""", sFormula2
fromwith block
. – Puntal.FormulaArray
has a 255 character limit and as a work around it must be done in multiple steps to enter one that is longer. – Scott Craner