尝试完成一个脚本,如果日期是所选月份并且在正确的团队中,则该脚本将在 C 中将值相加。 示例:
| Team| Date| Cost|
| 102|Mar-17| 13245|
| 103|Jan-17| 2050|
| 101|Feb-17| 1245|
| 104|Jan-17| 12400|
| 102|Mar-17| 5242|
| 104|Jan-17| 600|
| 102|Feb-17| 10240|
| 102|Jan-17| 450|
| 102|Mar-17| 12245|
| 101|Jan-17| 2300|
脚本的目标是确定我是否正在寻找一月:
- 101 = 2300
- 102 = 450
- 103 = 2050
- 104 = 13000
我一直在试图解决这个问题,但感觉陷入困境。在提供的公式中,cboMonth 是根据用户表单及其组合框确定为三个字母的月份。 cboYear 以类似的方式用 4 位数字确定。最初的计划是使用循环,但我不知道如何将求和函数组合在一起。这就是我到目前为止所拥有的。
Dim rng As Range
Dim lastrow As Long
Dim x As Integer
Dim txt101 As Long
Dim txt102 As Long
Dim txt103 As Long
Dim txt104 As Long
Dim txt105 As Long
lastrow = Cells(Rows.Count, "A").End(xlUp).Row
ReportWbk.Sheets(cboYear).Activate
Application.ScreenUpdating = False
For Each x In Range("D" & lastrow)
If x.Value = "FG-25" And x.Offset(, 1).Format(Month(mmm)) = cboMonth Then
txt101 =
txt102 =
txt103 =
txt104 =
txt105 =
End If
Next x
最佳答案
假设您的“团队”位于 A 列,“日期”位于 B 列,“成本”位于 C 列,并且您的变量 cboMonth
和 cboYear
属于 String
类型,您应该能够执行以下操作:
'Convert input to a date
Dim myDate As Date
myDate = DateValue("1 " & cboMonth & " " & cboYear)
'Perform the required SUMIFS
txt101 = Application.WorksheetFunction.SumIfs(Range("C:C"), _
Range("A:A"), "101", _
Range("B:B"), ">=" & myDate, _
Range("B:B"), "<=" & DateAdd("m", 1, myDate))
'txt102 =
'txt103 =
'txt104 =
'txt105 =
我不清楚您的数据是如何构造的,但这应该不难集成到您的代码中。
关于excel - 尝试完成 sumifs(?) 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43643043/