现在我正在将此代码用于SumIf公式:
Public Sub CommandButton1_Click()
Dim lastrow As Long
lastrow = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row + 1
i = 4
calc = "=SUM(SUMIF($A$2:$A$22,N3,$B$2:$B$22),"
Do Until i = lastrow + 1
calc = calc & "SUMIF($A$2:$A$22,N" & i & ",$B$2:$B$22),"
i = i + 1
Loop
ActiveSheet.Range("N" & lastrow + 1 & ":BI" & lastrow + 1).Value = calc & ")"
End Sub
我现在的问题是,有没有一种方法可以更改calc = calc和SUMIF($ A $ 2:$ A $ 22, N“ &i&”,$ B $ 2:$ B $ 22)”,它从N列跳到M列直到BI列?
事实上,我的代码工作正常,但是如果我在表中移动一些单元格,它也会移动公式。如果我加上$符号,它只会使用N栏。
我试图添加它,但是它不起作用
i = 4
lCol = 14
calc = "=SUM(SUMIF($A$2:$A$22,N3,$B$2:$B$22),"
Do Until i = lastrow + 1
calc = calc & "SUMIF($A$2:$A$22," & Columns(i, lCol) & ",$B$2:$B$22),"
i = i + 1
lCol = lCol + 1
Loop
我希望我的意思很清楚,有人可以帮助我。
答案 0 :(得分:2)
尝试创建一个单元并使用其地址。
calc = calc & "SUMIF($A$2:$A$22," & Cells(i, lCol).address(0, 0) & ",$B$2:$B$22),"
答案 1 :(得分:0)