我想知道是否有人愿意与我分享在A栏中查找特定名称后如何处理B列的总和?比方说,我想将test1与test4相加。然后将总数放入“B1”中,其中“In”旁边是“In”,“In”不一定是“B1”。我添加了一张图片以缓解混乱。
我尝试过编写代码:
Sub Macro1()
Dim lcell As Range, lrow As Long, rowToSum As Long
lrow = 1
Do
set lcell = Sheets("Sheet1").cells(lrow,1)
If lcell = "Test1" or "Test2" Or "Test3" Or "Test4" Then
'I'm not sure how to i sum different columns, my guess was to do a rowToSum = lcell
lrow = lrow + 1
Exit Do
End If
Loop
End Sub
如果你能解决我的问题,我将不胜感激!非常感谢你!
答案 0 :(得分:2)
简单的方法是B1中的公式
=SUM(COUNTIF(A:A, {"Test1","Test2","Test3","Test4"}))
可以通过
在VBA中完成Sub Test()
Sheets(1).[b1] = Evaluate("SUM(COUNTIF(A:A, {""Test1"",""Test2"",""Test3"",""Test4""}))")
End Sub
已更新以澄清意图
Sub Test()
Sheets(1).[b1].Value = Evaluate("SUM(SUMIF(A2:A10, {""Test1"",""Test2"",""Test3"",""Test4""},B2:B10))")
End Sub
答案 1 :(得分:1)
嗨,我想你误会了我的问题。我想将test1 - test4的值加在一起,这意味着,我想在Col B处添加值,我应该在B1处添加10。您提供的代码实际上总结了Col A中的test1-test4,而不是B列中的值。 - user1204868
这让我们两个人大声笑。
试试这个
=SUMPRODUCT((A1:A10={"Test1","Test2","Test3","Test4"})*(B1:B10))
关注
将此公式放在C1
中如果要在B1中使用它,请将其更改为
=SUMPRODUCT((A2:A10={"Test1","Test2","Test3","Test4"})*(B2:B10))
VBA代码
Sub Sample()
Sheets(1).[b1] = Evaluate("SUMPRODUCT((A2:A10={""Test1"",""Test2"",""Test3"",""Test4""})*(B2:B10))")
End Sub