I have a SUMIFS function. I want to translate this function into VBA code, but I can not make it work.
Two pictures of my Excel file to show a simplified example.
I have an input tab to provide information on several products which are bought and sold on different dates. The names of the products are shown under ISIN. I want to sum the quantities from the input sheet into the output sheet under certain criteria.
I have the following arguments that needs to be fulfilled:
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")
I want to sum the quantities, per ISIN code/product in the output file.
The results should be shown in the red outlined box in the output sheet.
This should happen if the dates and Buy task correspond to the ones displayed in the output file
I don't know how I should dim and set the variables correctly. I also don't know how the code will run all the dates and ISIN codes displayed in the output file.
This is the code I have so far for my real Excel sheet. Not for the SIMPLIFIED version I showed before.
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