[英]Multiple conditions using SumIFs
I am trying to automate sumIfs using multiple criteria for the below sample table.我正在尝试使用以下示例表的多个条件来自动化 sumIfs。
Column A to F is the data and the output table is displayed in column H through K. The output table sums the values in the data based on condition1 and condition2. A到F列是数据,output表显示在H到K列。output表根据condition1和condition2对数据中的值求和。 I am able to use excel built in formulas to do this task, however the values are 0 when I use a vba code to do this.我可以使用 excel 内置公式来执行此任务,但是当我使用 vba 代码执行此操作时,值为 0。
Can someone help?有人可以帮忙吗?
I am new to VBA and the code produced 0 when tried in a For loop for my actual data.我是 VBA 的新手,当在 For 循环中尝试我的实际数据时,代码生成 0。
Here is my code that I used for this sample, but this throws an error with the SumRange.这是我用于此示例的代码,但这会引发 SumRange 错误。
Sub Macro3()
Dim ws As Worksheet, Lr1 As Long, Lc1 As Long
Set ws = Worksheets("Sheet1")
Lr1 = ws.Range("A" & Rows.Count).End(xlUp).Row
Lc1 = ws.Range("A" & Columns.Count).End(xlToLeft).Column
For i = 3 To Lr1
ws.Range("I" & i).Formula = "=sumifs(range("B"&i&":"F"&i),B1:F1,I1,B2:F2,I2)"
Next i
End Sub
Also, tried using this:另外,尝试使用这个:
Sub Macro3()
Dim ws As Worksheet, Lr1 As Long, Lc1 As Long
Set ws = Worksheets("Sheet1")
Lr1 = ws.Range("A" & Rows.Count).End(xlUp).Row
Lc1 = ws.Range("A" & Columns.Count).End(xlToLeft).Column
For i = 3 To Lr1
Range("I" & i).Value = Application.WorksheetFunction.SumIfs(Range(Cells(i, 2), Cells(i, Lc1)), Range("B1:F1"), Range("I1"), Range("B2:F2"), Range("I2"))
Next i
End Sub
Error 1004: unable to get sumifs property of the worksheet function class错误1004:无法获取工作表的sumifs属性 function class
SUMIFS
使用 VBA 编写公式: SUMIFS
Option Explicit
Sub WriteSumIfsFormula()
Dim wb As Workbook: Set wb = ThisWorkbook ' workbook containing this code
Dim ws As Worksheet: Set ws = wb.Sheets("Sheet1")
Dim LastRow As Long: LastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
Dim rg As Range: Set rg = ws.Range("I3", ws.Cells(LastRow, "K"))
Dim Formula As String ' Pay attention to the dollar signs!
Formula = "=SUMIFS($B3:$F3,$B$1:$F$1,I$1,$B$2:$F$2,I$2)"
'Debug.Print Formula ' check until you got it right
rg.Formula = Formula
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.