我想在公式中插入2个变量。尽管他们出现在引号内。
fase1 = Cells(2, diffdate_ac + 4).Address(RowAbsolute:=False, ColumnAbsolute:=False)
prod1 = Cells(4, diffdate_ac + 4).Address(RowAbsolute:=False, ColumnAbsolute:=False)
Fase1指AG2和Prod1指AG4。
Range("B3").Formula = "=SUM((SUMIFS(ben_quant,ben_dia,R[-1]C,ben_fase, " & fase1 & ",ben_linha," & prod1 & "))
这是公式的一部分。 “& fase 1&”正在给我'AG2',而'& prod 1&'正在返回'AG4'。是的,它们都在公式内的引号内,如图所示:
我想删除那些单引号,因为公式不是指那些单元格。
此命令代码的完整部分
'Prints an array of dates
Dim startac2 As Long, finishac2 As Long
Dim datearray3 As Variant, y As Long
finishac2 = CLng(DateValue(lastac))
startac2 = CLng(DateValue(firstac))
ReDim datearray3(finishac2 - startac2 + 1)
For y = 0 To finishac2 - startac2
datearray3(y) = startac2 + y
Next
diffdate_ac = DateDiff("d", startac2, finishac2) + 1
wsPBen.Activate
Range(Cells(2, 2), Cells(2, diffdate_ac + 1)).Value = datearray3
Range(Cells(2, 2), Cells(2, diffdate_ac + 1)).NumberFormat = "d/m;@"
'
Range("A1").Value = "BENEFICIAMENTO: TINTURARIA LISO"
Range("A2").Value = "Data"
Range("A3").Value = "Produção"
Cells(2, diffdate_ac + 2).Value = "TOTAL"
Cells(2, diffdate_ac + 3).Value = "FASE"
Cells(4, diffdate_ac + 3).Value = "PRODUTO"
Cells(2, diffdate_ac + 4).Value = "TINGIR ALTA TEMP."
Cells(3, diffdate_ac + 4).Value = "TINGIR BAIXA TEMP."
Cells(4, diffdate_ac + 4).Value = "TINTO RAMADO"
Cells(5, diffdate_ac + 4).Value = "TINTO TUBULAR"
Dim fase1 As String
Dim fase2 As String
Dim prod1 As String
Dim prod2 As String
fase1 = Cells(2, diffdate_ac + 4).Address(RowAbsolute:=False, ColumnAbsolute:=False)
fase2 = Cells(3, diffdate_ac + 4).Address(RowAbsolute:=False, ColumnAbsolute:=False)
prod1 = Cells(4, diffdate_ac + 4).Address(RowAbsolute:=False, ColumnAbsolute:=False)
prod2 = Cells(5, diffdate_ac + 4).Address(RowAbsolute:=False, ColumnAbsolute:=False)
Range("B3").formula = "=SUM((SUMIFS(ben_quant,ben_dia,R[-1]C,ben_fase," & fase1 & ",ben_linha," & prod1 & ")),(SUMIFS(ben_quant,ben_dia,R[-1]C,ben_fase," & fase1 & ",ben_linha," & prod2 & ")),(SUMIFS(ben_quant,ben_dia,R[-1]C,ben_fase," & fase2 & ",ben_linha," & prod1 & ")),(SUMIFS(ben_quant,ben_dia,R[-1]C,ben_fase," & fase2 & ",ben_linha," & prod2 & ")))"
答案 0 :(得分:5)
如果我读了你的话,这个VBA行:
Range("B3").Formula = "=SUM((SUMIFS(ben_quant,ben_dia,R[-1]C,ben_fase, " & fase1 & ",ben_linha," & prod1 & "))"
正在尝试设置此公式表达式:
=SUM((SUMIFS(ben_quant,ben_dia,R[-1]C,ben_fase, AG2,ben_linha,AG4))
然而,这是在同一配方中混合R1C1和A1单元寻址。据我所知,这总是无效的语法。
答案 1 :(得分:1)
您的代码对我来说没问题...您是否可以启动新的Excel工作簿,转到VBA编辑器,并将以下代码粘贴到新模块中?
Sub x()
Dim cellAddress As String
cellAddress = Cells(1, 1).Address(RowAbsolute:=False, ColumnAbsolute:=False)
Dim formula As String
formula = "=SUMIFS(B1:B4,C1:C4," & cellAddress & ")"
Call MsgBox(formula)
Range("A2").formula = formula
End Sub
在此之后,运行它并告诉我们提示是否在公式中显示任何引号,并告诉我们之后在单元格A2中的内容。
答案 2 :(得分:1)
我只是简单地用引号替换引号:
Range("B3").Formula = "=SUM((SUMIFS(ben_quant,ben_dia,R[-1]C,ben_fase," & fase1 & ",ben_linha," & prod1 & ")),(SUMIFS(ben_quant,ben_dia,R[-1]C,ben_fase," & fase1 & ",ben_linha," & prod2 & ")),(SUMIFS(ben_quant,ben_dia,R[-1]C,ben_fase," & fase2 & ",ben_linha," & prod1 & ")),(SUMIFS(ben_quant,ben_dia,R[-1]C,ben_fase," & fase2 & ",ben_linha," & prod2 & ")))"
Range("B3").Formula = Replace(Range("B3").Formula, "'", "")