VBA - SUMIF with multiple criteria

aek919

New Member
Joined
Mar 31, 2014
Messages
3
I understand how SUMIFS works in Excel, but I cannot figure out how it works in VBA.


Ideally I want the code to do the following:


Input Box appears and asks user to enter in the year in which they would like to see the totals for. That year is stored as "strYear" and used throughout the VBA code.

The title of the document is Concatenated and will state (strYear + " Totals").

Then, a few cells down from the title, would be a SUM of a range on a different sheet, only added if it meets three different criteria (Year - as the user specified, Target Area - as the code specifies, and Type of Product - as the code specifies). I need every row in this database to be checked and the ones that meet the criteria to be totaled on the Totals page for the manager to see. Also the database adds rows to the bottom of the database, so it is always growing.


There are multiple totals that I need and I can't figure out how to put a code together to make this happen. I will also need to do the same concept but with COUNT, to see how many times a record of certain criteria has occurred.



This is my VBA code at the moment.
I have done the first two parts of what I would like the overall code to accomplish, and have specified the ranges of the criteria to be searched.




Sub Totals()



Dim strYear As String
Dim rStartPosition As Range
Dim Title As String



Sheets("Totals").Select


strYear = InputBox("Nutrients Applied and Active Ingredient Totals. Please enter year.", "Totals")


Title = strYear + " Totals"


Sheets("Totals").Select
Range("A3").Select
ActiveCell = Title



Dim AppDBSumRangeStart As Range
Dim AppDBSumRangeEnd



Sheets("Application Database").Select
Range("N4").Select

Set AppDBSumRangeStart = Application.ActiveCell


Do Until ActiveCell = ""
ActiveCell.Offset(1, 0).Select
Loop
ActiveCell.Offset(-1, 0).Select
Set AppDBSumRangeEnd = Application.ActiveCell




Dim AppDBYearRangeStart As Range
Dim AppDBYearRangeEnd As Range



Sheets("Application Database").Select
Range("AA4").Select
Set AppDBYearRangeStart = Application.ActiveCell


Do Until ActiveCell = ""
ActiveCell.Offset(1, 0).Select
Loop
ActiveCell.Offset(-1, 0).Select
Set AppDBYearRangeEnd = Application.ActiveCell




Dim AppDBTargetAreaRangeStart As Range
Dim AppDBTargetAreaRangeEnd As Range



Sheets("Application Database").Select
Range("D4").Select
Set AppDBTargetAreaRangeStart = Application.ActiveCell


Do Until ActiveCell = ""
ActiveCell.Offset(1, 0).Select
Loop
ActiveCell.Offset(-1, 0).Select
Set AppDBTargetAreaRangeEnd = Application.ActiveCell




Dim AppDBTypeRangeStart As Range
Dim AppDBTypeRangeEnd As Range



Sheets("Application Database").Select
Range("D4").Select
Set AppDBTypeRangeStart = Application.ActiveCell


Do Until ActiveCell = ""
ActiveCell.Offset(1, 0).Select
Loop
ActiveCell.Offset(-1, 0).Select
Set AppDBTypeRangeEnd = Application.ActiveCell




Sheets("Totals").Select
Range("C8").Select


'**This is the cell where I want the SUMIFs function to show the result.**




End Sub


***************************************

Any help is greatly appreciated!


Thanks in advanced!
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!

Forum statistics

Threads
1,214,782
Messages
6,121,532
Members
449,037
Latest member
tmmotairi

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top