我正在尝试将变量插入到我将插入到表格中的公式中。该公式应该从另一个表执行 sumproduct。我通常只会调用表名,但问题是这将在不同的表上运行不同的时间,因此它实际上永远不会是相同的表名。我用变量“PrevSh”替换了表名
下面的这个公式是我的宏中的内容。它给了我问题。它给了我应用程序定义或对象定义的错误。
ActiveCell.FormulaR1C1 = "=IF(A7<>"",SUMPRODUCT(('[" & PrevSh & "] !$D$4:$D$485=A7])*('[" & PrevSh & "] !$E $4:$E$485])),"")"
Dim sh As Worksheet
Dim TableName As String
Dim theTable As ListObject
Dim PrevSh As String
Set sh = ActiveSheet
TableName = ActiveSheet.Previous.Name & "_Spider"
PrevSh = ActiveSheet.Previous.Name
Sheets.Add.Name = (TableName)
Range("A3").Select
ActiveCell.FormulaR1C1 = "MATERIAL"
Range("B3").Select
ActiveCell.FormulaR1C1 = "TOTAL"
Range("C3").Select
ActiveCell.FormulaR1C1 = "QTY OF LNG"
Range("D3").Select
ActiveCell.FormulaR1C1 = "MATERIAL DESCRIPTION"
Range("E3").Select
ActiveCell.FormulaR1C1 = "RAWMATERIAL LENGTH"
Range("F3").Select
ActiveCell.FormulaR1C1 = "PIECES"
Range("A3:f4").Select
ActiveSheet.ListObjects.Add(xlSrcRange, Range("$A$3:$F$4"), , xlYes).Name = _
TableName
Range(TableName & "[TOTAL]").Select
ActiveCell.FormulaR1C1 = "=IF(A7<>"",SUMPRODUCT(('[" & PrevSh & "] !$D$4:$D$485=A7])*('[" & PrevSh & "] !$E$4:$E$485])),"")"