当列标题与某个值匹配时,我尝试将单元格值设置为公式。我收到了1004错误,我认为这是因为我的变量被设置为一个范围,我将其设置为字符串值。
如何更正该行" Cell.Formula = Formula_1"允许插入公式?
Public Function WorksheetFormula(ByVal target As Range, ColumnHeader As String, formula_1 As String, columnFormula As String, cell As Range)
columnFormula = target.Offset((14 - target.Row), 0).Value
If columnFormula = "% Discount" Then
formula_1 = "=IFERROR((GETPIVOTDATA(""Max of ""&INDIRECT((ADDRESS(14,COLUMN()))),'Database1 PivotTable'!$A$1," & _
"""KEY"",LEFT(INDIRECT(CONCATENATE(""B"",SUM(ROW()-1))),4)&CurrentHFMFamily&(OFFSET(INDIRECT(ADDRESS(ROW(),COLUMN())),-(SUM(ROW()-(6))),-(MOD(COLUMN()+1,4))))" & _
"&CurrentYear,""PRODUCT"",CurrentHFMFamily,""PROGRAM"",LEFT(INDIRECT(CONCATENATE(""B"",SUM(ROW()-1))),4)," & _
"""CUSTOMERSEG"",CurrentCustomerSegment,""MONTH"",(OFFSET(INDIRECT(ADDRESS(ROW(),COLUMN())),-(SUM(ROW()-(6))),-(MOD(COLUMN()+1,4)))),""YEAR"",CurrentYear)),"")"
ElseIf columnFormula = "% Take" Then
formula_1 = "=IFERROR((GETPIVOTDATA(""Max of ""&INDIRECT((ADDRESS(14,COLUMN()))),'Database1 PivotTable'!$A$1," & _
"""KEY"",LEFT(INDIRECT(CONCATENATE(""B"",SUM(ROW()-1))),4)&CurrentHFMFamily&(OFFSET(INDIRECT(ADDRESS(ROW(),COLUMN())),-(SUM(ROW()-(6))),-(MOD(COLUMN()+1,4))))" & _
"&CurrentYear,""PRODUCT"",CurrentHFMFamily,""PROGRAM"",LEFT(INDIRECT(CONCATENATE(""B"",SUM(ROW()-1))),4)," & _
"""CUSTOMERSEG"",CurrentCustomerSegment,""MONTH"",(OFFSET(INDIRECT(ADDRESS(ROW(),COLUMN())),-(SUM(ROW()-(6))),-(MOD(COLUMN()+1,4)))),""YEAR"",CurrentYear)),"")"
ElseIf columnFormula = "Dollars" Then
formula_1 = "=PRODUCT((OFFSET(INDIRECT(ADDRESS(ROW(),COLUMN())),-(SUM(ROW()-(8))),(MOD(COLUMN()+1,4)))),(OFFSET(INDIRECT(ADDRESS(ROW(),COLUMN())),0,1))," & _
"(OFFSET(INDIRECT(ADDRESS(ROW(),COLUMN())),0,2)))"
End If
Debug.Print columnFormula
Debug.Print formula_1
cell.Formula = formula_1
End Function
答案 0 :(得分:0)
由于您的功能名为WorksheetFormula
,请尝试将cell.Formula = formula_1
替换为WorksheetFormula = formula_1
Public Function WorksheetFormula(ByVal target As Range, ColumnHeader As String, formula_1 As String, columnFormula As String, cell As Range)
columnFormula = target.Offset((14 - target.Row), 0).Value
Select Case collumnFormula
Case "% Discount"
formula_1 = "=IFERROR((GETPIVOTDATA(""Max of ""&INDIRECT((ADDRESS(14,COLUMN()))),'Database1 PivotTable'!$A$1," & _
"""KEY"",LEFT(INDIRECT(CONCATENATE(""B"",SUM(ROW()-1))),4)&CurrentHFMFamily&(OFFSET(INDIRECT(ADDRESS(ROW(),COLUMN())),-(SUM(ROW()-(6))),-(MOD(COLUMN()+1,4))))" & _
"&CurrentYear,""PRODUCT"",CurrentHFMFamily,""PROGRAM"",LEFT(INDIRECT(CONCATENATE(""B"",SUM(ROW()-1))),4)," & _
"""CUSTOMERSEG"",CurrentCustomerSegment,""MONTH"",(OFFSET(INDIRECT(ADDRESS(ROW(),COLUMN())),-(SUM(ROW()-(6))),-(MOD(COLUMN()+1,4)))),""YEAR"",CurrentYear)),"")"
Case "% Take"
formula_1 = "=IFERROR((GETPIVOTDATA(""Max of ""&INDIRECT((ADDRESS(14,COLUMN()))),'Database1 PivotTable'!$A$1," & _
"""KEY"",LEFT(INDIRECT(CONCATENATE(""B"",SUM(ROW()-1))),4)&CurrentHFMFamily&(OFFSET(INDIRECT(ADDRESS(ROW(),COLUMN())),-(SUM(ROW()-(6))),-(MOD(COLUMN()+1,4))))" & _
"&CurrentYear,""PRODUCT"",CurrentHFMFamily,""PROGRAM"",LEFT(INDIRECT(CONCATENATE(""B"",SUM(ROW()-1))),4)," & _
"""CUSTOMERSEG"",CurrentCustomerSegment,""MONTH"",(OFFSET(INDIRECT(ADDRESS(ROW(),COLUMN())),-(SUM(ROW()-(6))),-(MOD(COLUMN()+1,4)))),""YEAR"",CurrentYear)),"")"
Case "Dollars"
formula_1 = "=PRODUCT((OFFSET(INDIRECT(ADDRESS(ROW(),COLUMN())),-(SUM(ROW()-(8))),(MOD(COLUMN()+1,4)))),(OFFSET(INDIRECT(ADDRESS(ROW(),COLUMN())),0,1))," & _
"(OFFSET(INDIRECT(ADDRESS(ROW(),COLUMN())),0,2)))"
Case Else
formula_1 = "Error"
End Select
Debug.Print columnFormula
Debug.Print formula_1
WorksheetFormula = formula_1
End Function