vba 将excel sumifs公式转换为vba代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16856740/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 15:44:18  来源:igfitidea点击:

converting excel sumifs formula to vba code

vbaexcel-vbaexcel-2007excel-formulaexcel

提问by user2440270

I'm trying to do a SUMIFS calculation in VBA. It works fine when I enter it on the spreadsheet, but when I try to convert it to VBA, it doesn't seem to work.

我正在尝试在 VBA 中进行 SUMIFS 计算。当我在电子表格中输入它时它工作正常,但是当我尝试将其转换为 VBA 时,它似乎不起作用。

Sheets("Master").Range("B2:B" & Range("A" & Rows.Count).End(xlUp).Row).Formula = _
    "=SUMIFS(Input!C32,Input!C37,Master!C1,Input!C31,Master!R1C)"

This is the snippet of code (originally in a comment):

这是代码片段(最初在评论中):

Dim LastRow As Long 
Dim rw As Long 
LastRow = Range("A" & Rows.Count).End(xlUp).Row 
For rw = 2 To LastRow 
  Sheets("Master").Cells(rw, 2).Value = Application.WorksheetFunction.SumIfs(Sheets("Input").Range("AF:AF"), Sheets("Input").Range("AK:AK"), Sheets("Master").Range("A:A"), Sheets("Input").Range("AE:AE").Sheets("Master").Range("B2"))  
Next 

回答by Jon Crowell

You aren't specifying the values you want to lookup in your criteria1. You have to specify a value, not a range.

您没有指定要在标准中查找的值1。您必须指定一个值,而不是一个范围。

Your sum range is fine.

你的总和范围很好。

Sheets("Input").Range("AF:AF")

Your criteria range1 is fine.

您的标准 range1 很好。

Sheets("Input").Range("AK:AK")

Your criteria1 needs to be a value, not a range.

您的标准 1 需要是一个值,而不是一个范围。

Use this Sheets("Master").Range("A2").valueinstead of Sheets("Master").Range("A:A")

用这个Sheets("Master").Range("A2").value代替Sheets("Master").Range("A:A")

Obviously you can replace the 2 in the criteria1 with a variable if you need to to get your loop to work.

显然,如果您需要让循环正常工作,您可以用变量替换标准 1 中的 2。