Sum values if less than a specific month
To sum the values if less than a specific month we can apply multiple Excel and VBA methods
Example: Sum values if less than a specific month
=SUMPRODUCT((MONTH(B8:B14)
|
The formula uses a combination of the Excel SUMPRODUCT and MONTH functions to sum the values that are associated with dates that are less than a specified month. With this formula you only have to select the data (dates and associated values) and the specific month for which you want to sum value for if the dates are less then the specified month.
|
METHOD 2. Sum values if less than a specific month using an Array formula with SUM, IF and MONTH functions
EXCEL
This is an array formula that uses a combination of the Excel SUM, IF and MONTH functions to sum the values that are associated with dates that are less than a specified month. Given this is an array formula, after entering the formula into a cell you need to click Ctrl + Shift + Enter to make it an array formula.
|
Dim ws As Worksheet
Set smonth = ws.Range("C5")
For i = 8 To 14
End If
End Sub
Worksheets: The Worksheets object represents all of the worksheets in a workbook, excluding chart sheets.
Range: The Range object is a representation of a single cell or a range of cells in a worksheet.
Worksheet Name: Have a worksheet named Analysis.
Dates: If using the exact VBA code you will need to capture the dates that you are testing against in range ("B8:B14"). This is especially important for the For Loop to loop through specified rows and columns. In this example we have specified for the For Loop to loop through rows 8 to 14 in column 2 to find all of the dates that are less than the third month (March).
Values: If using the exact VBA code you will need to capture the values that you want to sum, in range ("C8:C14"), that are associated with a date that is less that the specified month. This is especially important for the For Loop to loop through specified rows and columns. In this example we have specified for the For Loop to loop through rows 8 to 14 in column 3 to find all of the values associated with the dates that are less than the third month (March).
ADJUSTABLE PARAMETERS
Output Range: Select the output range by changing the cell reference ("F7") in the VBA code to any cell in the worksheet that doesn't conflict with the formula.
Date: Select a range that captures the dates that you want to test against by changing the i values that represent the row range and the value of 2 in the Cells reference that represents the column reference. In this example the dates are captured in rows 8 to 14 and column 2, therefore, if you are capturing the dates in different rows you will need to change the From and To i values in the VBA code. If you are capturing the dates in a different column you will also need to change the value of 2 in the Cells reference, to the column that captures the dates.
Values: Select a range of values that you want to sum by changing the i values that represent the row range and the value of 3 in the Cells reference that represents the column reference. In this example the dates are captured in rows 8 to 14 (this example assumes that the dates and values are captured in the same row range, therefore, the row range for both dates and values need to match) and column 3, therefore, if you are capturing the values in different rows you will need to change the From and To i values in the VBA code (this will be the same as for dates, therefore, if you have already changed the i values for the dates no further action is required for the values row range). If you are capturing the values in a different column you will also need to change the value of 3 in the Cells reference, to the column that captures the values.
METHOD 2. Sum values if less than a specific month using VBA with a formula for SUMPRODUCT and MONTH functions
VBA
Dim ws As Worksheet
ws.Range("F7").Formula = "=SUMPRODUCT((MONTH(B8:B14)
End Sub
Worksheets: The Worksheets object represents all of the worksheets in a workbook, excluding chart sheets.
Range: The Range object is a representation of a single cell or a range of cells in a worksheet.
Worksheet Name: Have a worksheet named Analysis.
Dates: If using the exact VBA code you will need to capture the dates that you are testing against in range (B8:B14).
Values: If using the exact VBA code you will need to capture the values that you want to sum that are associated with dates that are less than the specified month in range (C8:C14), which is the column next to the dates.
ADJUSTABLE PARAMETERS
Output Range: Select the output range by changing the cell reference ("F7") in the VBA code to any cell in the worksheet that doesn't conflict with the formula.
Date: Select a range that captures the dates that you want to test against by changing the range reference (B8:B14) in the VBA code that doesn't conflict with the formula.
Values: Select a range of values that you want to sum that are associated with dates that are less than the specified month by changing the range reference (C8:C14) in the VBA code that doesn't conflict with the formula.
METHOD 3. Sum values if less than a specific month using VBA with a array formula
VBA
Dim ws As Worksheet
ws.Range("F7").FormulaArray = "=SUM(IF(MONTH(B8:B14)
End Sub
Worksheets: The Worksheets object represents all of the worksheets in a workbook, excluding chart sheets.
Range: The Range object is a representation of a single cell or a range of cells in a worksheet.
Worksheet Name: Have a worksheet named Analysis.
Dates: If using the exact VBA code you will need to capture the dates that you are testing against in range (B8:B14).
Values: If using the exact VBA code you will need to capture the values that you want to sum that are associated with dates that are less than the specified month in range (C8:C14), which is the column next to the dates.
ADJUSTABLE PARAMETERS
Output Range: Select the output range by changing the cell reference ("F7") in the VBA code to any cell in the worksheet that doesn't conflict with the formula.
Date: Select a range that captures the dates that you want to test against by changing the range reference (B8:B14) in the VBA code that doesn't conflict with the formula.
Values: Select a range of values that you want to sum that are associated with dates that are less than the specified month by changing the range reference (C8:C14) in the VBA code that doesn't conflict with the formula.
To sum the values if less than a specific month we can apply multiple Excel and VBA methods.
=SUMPRODUCT((MONTH(dates)
{=SUM(IF(MONTH(dates)
ARGUMENTS
dates: A range of dates to be tested if they are less that a specified month.
values: A range of values associated with dates that are to be summed.
month: The month the dates are tested against to return the sum of associated values if the dates are less than the specified month.