To use the SUMIF function in Excel VBA, call it with WorksheetFunction.SumIf and pass three arguments: the range to check, the criteria to match, and the range to sum. For example, WorksheetFunction.SumIf(Range("A2:A100"), "North", Range("B2:B100")) returns the total of column B where column A equals "North". This guide shows you how to write that line correctly, handle variable criteria, and fix the errors that trip people up.
What You'll Need
- Excel for Windows or Mac with the Developer tab enabled (File > Options > Customize Ribbon > check Developer, or Excel > Preferences > Ribbon & Toolbar on Mac).
- A workbook with at least one sheet containing data in a header row and values below it.
- Basic familiarity with the VBA editor (Alt+F11 on Windows, Option+F11 on Mac).
- Macro security set to allow macros, or the file saved as .xlsm.
Step-by-Step
- Open the VBA editor. Press Alt+F11 (Windows) or Option+F11 (Mac). In the Project pane, right-click your workbook and choose Insert > Module. A blank code window appears.
- Write a simple Sub procedure. Type
Sub SumByRegion()and press Enter; VBA addsEnd Sub. Everything goes between those lines. - Declare a variable for the result. Add
Dim total As Double. Use Double rather than Integer because sums can exceed 32,767. - Assign the SUMIF result. Add
total = WorksheetFunction.SumIf(Range("A2:A100"), "North", Range("B2:B100")). The first range is the criteria range, the second is the criteria itself, and the third is the sum range. If you omit the third argument, Excel sums the first range, which is rarely what you want. - Display or use the result. Add
MsgBox totalto see it, or write it to a cell withRange("E2").Value = total. Press F5 to run the macro. - Use a cell reference as the criteria. Instead of a literal string, point to a cell:
WorksheetFunction.SumIf(Range("A2:A100"), Range("D2").Value, Range("B2:B100")). Changing D2 and re-running updates the total without editing code. - Add wildcards for partial matches. Criteria like
"North*"match any text starting with North. Use"*" & Range("D2").Value & "*"to match a substring held in a cell. The asterisk stands for any number of characters; the question mark stands for exactly one. - Sum with numeric comparisons. Pass the operator inside quotes:
WorksheetFunction.SumIf(Range("B2:B100"), ">1000", Range("C2:C100")). To use a cell value, concatenate:">" & Range("D2").Value. Without the concatenation, Excel treats the cell as text and returns 0. - Reference the sheet explicitly. Qualify ranges with the sheet name to avoid summing the wrong tab:
Worksheets("Data").Range("A2:A100"). If the sheet name contains spaces, wrap it in quotes as shown. - Handle errors gracefully. Wrap the call in
On Error Resume Nextand checkErr.Number, or test that the ranges are the same size before calling. Mismatched sizes raise run-time error 1004. - Save and test. Save as .xlsm, close the editor, and run the macro from Developer > Macros. Compare the VBA result against a worksheet SUMIF formula on the same data to confirm they agree.
Common Mistakes
- Forgetting WorksheetFunction. Writing
SumIf(...)alone causes a "Sub or Function not defined" compile error. It must beWorksheetFunction.SumIf. - Mismatched range sizes. The criteria range and sum range must have the same number of rows and columns. A one-row difference returns error 1004 or a wrong total.
- Quoting numeric criteria incorrectly.
SumIf(rng, 1000, sumRng)works, butSumIf(rng, ">" & 1000, sumRng)needs the operator in quotes and the number concatenated. Mixing them up yields zero. - Using the wrong sheet. Unqualified
Rangecalls refer to the active sheet. If the user clicks another tab before running, the macro sums the wrong data. - Assuming SUMIF handles arrays. In VBA, WorksheetFunction.SumIf takes ranges, not arrays. For array logic, use Evaluate or loop through the data.
- Ignoring case and wildcards. SUMIF is not case-sensitive, and wildcards are on by default. If you need exact, case-sensitive matching, SUMIF cannot do it alone.
Tips
- Use
Application.WorksheetFunctionif you want the full object model; the shorterWorksheetFunctionworks inside a Sub. - Store criteria in a variable so you can loop over several values and build a summary table.
- For multiple conditions, use
WorksheetFunction.SumIfswith pairs of ranges and criteria. - Test with a small range first, then widen it. Debugging a 10-row sample is faster than a 10,000-row one.
- Keep the sum range to values only. Text in the sum range is ignored, which can hide data-entry errors.
Troubleshooting
Error 1004 "Unable to get the SumIf property". Usually a range-size mismatch or a sheet reference that does not exist. Check that both ranges cover the same rows and that the sheet name is spelled exactly as on the tab.
The macro returns 0. Confirm the criteria actually appears in the criteria range. Check for leading or trailing spaces, numbers stored as text, and whether you concatenated a cell reference into a comparison operator.
Compile error: Sub or Function not defined. You omitted WorksheetFunction. or misspelled SumIf. VBA is case-insensitive but spelling still matters.
Wrong total after switching sheets. Qualify every range with Worksheets("Name"). Do not rely on the active sheet.
Macro does nothing. Macros may be disabled. Check the yellow security bar, enable content, and confirm the file is saved as .xlsm.
Result differs from the worksheet formula. Compare the exact ranges. A common cause is one range ending at row 100 and the other at row 101.
Frequently Asked Questions
Can I use SUMIF without specifying a sum range?
Yes. If you omit the third argument, Excel sums the criteria range itself. That only makes sense when the criteria range holds the numbers you want to add.
How do I use a variable as the criteria?
Pass the variable directly for text: WorksheetFunction.SumIf(rng, myText, sumRng). For comparisons, concatenate: ">" & myNumber.
Does VBA SUMIF support wildcards?
Yes, asterisk and question mark work as in the worksheet function. Set the fourth argument to False only if you need wildcards treated as literal characters.
What is the difference between SUMIF and SUMIFS in VBA?
SUMIF takes one criteria pair; SUMIFS takes multiple pairs and requires the sum range first. Use SUMIFS when you need two or more conditions.
Why does my SUMIF return a date serial number?
The sum range likely contains dates formatted as numbers. Format the result cell as a date, or the value is correct but displayed as a serial number.
Can I sum across multiple sheets with one SUMIF?
Not directly. SUMIF works on a single range. Loop through the sheets, call SUMIF on each, and add the results, or consolidate the data onto one sheet first.