我有一个SUMIFS功能。我想将此功能转换为VBA代码,但无法使其正常工作。
我的Excel文件的两张图片显示了一个简化的示例。
我有一个输入选项卡,提供有关在不同日期买卖的几种产品的信息。产品名称显示在ISIN下。我想在一定条件下将数量从输入表汇总到输出表中。
我需要满足以下条件:
Dim Arg1 As Range 'the range i want to sum : so quantity
Dim Arg2 As Range 'criteria for range : Dates
Dim Arg3 As Range 'the criteria (range)
Dim Arg4 As Range 'criteria for range : ISIN
Dim Arg5 As Range 'the criteria (range)
Dim Arg6 As Range 'criteria for range : Type
Dim Arg7 As Range 'the criteria (range)
Set Arg1 = ThisWB.Sheets("INPUT").Range("A1:A13")
Set Arg2 = ThisWB.Sheets("INPUT").Range("B1:B13")
Set Arg3 = ThisWB.Sheets("OUTPUT").Range("A4:A8")
Set Arg4 = ThisWB.Sheets("INPUT").Range("C1:C13")
'these are rows (so ISIN codes vertically)
Set Arg5 = ThisWB.Sheets("OUTPUT").Range("B2:E2")
Set Arg6 = ThisWB.Sheets("INPUT").Range("D1:D13")
'This is the criteria that only values under Buy should be summed
Set Arg7 = ThisWB.Sheets("OUTPUT").Range("B2")
我想对输出文件中每个ISIN代码/产品的数量求和。
结果应显示在输出表中的红色轮廓框中。
如果日期和购买任务与输出文件中显示的日期和日期相对应,则会发生这种情况
我不知道应该如何调暗并正确设置变量。我也不知道代码将如何运行输出文件中显示的所有日期和ISIN代码。
这是我到目前为止用于真实Excel工作表的代码。不适用于我之前显示的简化版本。
Option Explicit
Sub InsertQ()
'Sum Quantities
'Declare variables
Dim lastRowData, lastRowInput, I, x, pasteRow As Integer
Dim shtInput As Worksheet
Dim shtData As Worksheet
Dim Arg1 As Range 'the range i want to sum : so quantity
Dim Arg2 As Range 'criteria for range : Dates
Dim Arg3 As Range 'the criteria (range)
Dim Arg4 As Range 'criteria for range : ISIN
Dim Arg5 As Range 'the criteria (range)
Dim Arg6 As Range 'criteria for range : Type
Dim Arg7 As Range 'the criteria (range)
'Set variables
Set shtData = Sheets("OUTPUT")
Set shtInput = Sheets("INPUT")
lastRowData = shtData.Range("B4").End(xlDown).Row
lastRowInput = shtInput.Range("A1").End(xlDown).Row
pasteRow = 5
Set Arg1 = shtInput.Range("G1:G1048576")
Set Arg2 = shtInput.Range("J1:J1048576")
Set Arg3 = shtData.Range("A4:A20")
Set Arg4 = shtInput.Range("AF1:AF1048576")
Set Arg5 = shtData.Range("B2:E2")
Set Arg6 = shtInput.Range("E1:E1048576")
Set Arg7 = shtData.Range("A2")
'Deactivate Screen for purpose of performance
Application.ScreenUpdating = False
'Code
For I = 2 To lastRowData
For x = 2 To lastRowInput
shtData.Cells(x, I) = _
Application.WorksheetFunction.SumIfs(Arg1, Arg2, Arg3, Arg4, Arg5, Arg6, Arg7)
Next x
pasteRow = pasteRow + 1
Next I
'Formatting
lastRowData = shtData.Range("B4").End(xlDown).Row
shtData.Range("B4:XFD" & lastRowData).NumberFormat = "0.00"
shtData.Range("E5:E" & lastRowData).NumberFormat = "0.00"
'Confirm to user
Application.ScreenUpdating = True
shtData.Range("A1").Select
End Sub
答案 0 :(得分:1)
Type
进行过滤ISIN
添加到列Dates
添加到行Quantity
添加到值Buy
作为单元格B2中的类型并完成。答案 1 :(得分:0)
这里不需要VBA。您可以使用一个简单的公式来完成此操作。
将以下内容复制到红色区域:
=SUMIFS(INPUT!$A:$A,INPUT!$B:$B,OUTPUT!$A:$A,INPUT!$C:$C,OUTPUT!$2:$2,INPUT!$D:$D,OUTPUT!$B$1)
或使用VBA自动编写:
Option Explicit
Sub WriteFormula()
With Worksheets("OUTPUT")
With .Range("C4", .Cells(.Cells(.Rows.Count, "A").End(xlUp).Row, .Cells(2, .Columns.Count).End(xlToLeft).Column))
.Formula = "=SUMIFS(INPUT!$A:$A,INPUT!$B:$B,OUTPUT!$A:$A,INPUT!$C:$C,OUTPUT!$2:$2,INPUT!$D:$D,OUTPUT!$B$1)"
'if needed as values not forumlas uncomment the following line
'.Value = .Value
End With
End With
End Sub
答案 2 :(得分:0)
因此,通过在构建函数Application.WorksheetFunction.SumIfs()
中使用VBA可以实现您想要的。我想这不是执行SUMIFS的最快方法,但是它是excel函数“ =SUMIFS()
”的副本。
根据我的理解,请注意,在每个条件放置位置,条件值只能是一个单一值(而不是范围)...因此,我们需要按需要遍历每个条件值(条件1和条件2) SUMIFS application中只有一个值)。这是在VBA中通过“ For each .. In ...
”循环完成的。
Application.WorksheetFunction.SumIfs(Sum_range, Criteria_range1, Criteria1, Criteria_range2, criteria2, etc..)
工作表“输入”:
Workhseet“输出”:
该代码复制上图中工作表“输出”中的右表。我们正在使用其他工作表“输入”中的某些范围,而在此工作表“输出”中需要我们结果的范围。 (请注意,在excel中,您可以为标准1使用范围,在VBA中是不可能的。)
颜色是我们在图纸上使用的= sumifs范围。这些将在代码中复制。
VBA代码:
Option Explicit
Sub Sumifs()
Dim InputSheet As Worksheet
Dim OutputSheet As Worksheet
Set InputSheet = ActiveWorkbook.Worksheets("Input")
Set OutputSheet = ActiveWorkbook.Worksheets("Output")
Dim Arg1 As Range 'the range i want to sum : so quantity
Dim Arg2 As Range 'criteria for range : Dates
Dim Arg3 As Variant 'the criteria (range)
Dim Arg4 As Range 'criteria for range : ISIN
Dim Arg5 As Variant 'the criteria (range)
Dim Arg6 As Range 'criteria for range : Type
Dim Arg7 As Variant 'the criteria (range)
Set Arg1 = InputSheet.Range("A2:A14") 'Sum_range
Set Arg2 = InputSheet.Range("B2:B14") 'Criteria_range1
Set Arg3 = OutputSheet.Range("A3:A7") 'Criteria1
Set Arg4 = InputSheet.Range("C2:C14") 'Criteria_range2
Set Arg5 = OutputSheet.Range("C2:F2") 'Criteria2 - these are rows (so ISIN codes vertically)
Set Arg6 = InputSheet.Range("D2:D14") 'Criteria_range3
Set Arg7 = OutputSheet.Range("B1") 'Criteria3 - This is the criteria that only values under Buy should be summed
Dim cell_date As Variant
Dim cell_ISIN As Variant
Dim cell_Type As Variant
Dim cell_ISIN_column As Long
Dim cell_date_row As Long
For Each cell_ISIN In Arg5 'Loop through all ISIN codes in range setin Arg 5
cell_ISIN_column = cell_ISIN.Column 'Get current column for ISIN
For Each cell_date In Arg3 'Loop through all Dates in range set in Arg3
cell_date_row = cell_date.Row 'Get current row for date
'My understanding is that the criteria values can only be only one single value at each criteria... therefore we need to loop through each criteria value (Arg3 and Arg5 needs to have only one value in SUMIFS application).
OutputSheet.Cells(cell_date_row, cell_ISIN_column) = Application.WorksheetFunction.Sumifs(Arg1, Arg2, cell_date, Arg4, cell_ISIN, Arg6, Arg7)
Next cell_date 'go to next date
Next cell_ISIN 'go to next ISIN
End Sub
Credit to D_Bester at SO and more inspiration/explantions can be found here in his thread :)