【发布时间】:2014-10-02 21:09:34
【问题描述】:
我正在尝试在 VBA 中使用 Sumif 函数,但是,我遇到了运行时错误。当我尝试将变量 CritLastCol 添加到公式中时,错误开始了。如果我用实际的单元格引用替换它,它就可以工作。我希望能够自动填充下面的行,我的标准将根据行而改变。换句话说,我的总和范围和条件范围是固定的,我的条件是我的活动单元格左侧的单元格(但不是一个,可能是 20、30 列)。
Sub SumBydimcode()
Dim lastCol As Integer
Dim CritLastCol As String
lastCol = Cells(3 & Columns.Count).End(xlToLeft).Column
lastRow = Range("A" & Rows.Count).End(xlUp).Row
NewLastRow = Range("I" & Rows.Count).End(xlUp).Row
CritLastCol = ActiveCell.End(xlToLeft).Value
ActiveCell.FormulaR1C1 = "=SUMIF(R3C8:R" & lastRow & "C8," & CritLastCol & ",R3C10:R" & lastRow & "C" & lastCol & ")"
ActiveCell.AutoFill Destination:=Range(ActiveCell.Address & ":J" & NewLastRow)
End Sub
【问题讨论】:
-
你能在
CritLastCol中输入一个有效的值吗?使用CritLastCol = "WhatYouWant"进行测试