Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Excel VBA SUMIFS Multiple Criteria: Step-by-Step

· 5 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

To sum values in Excel with multiple conditions from VBA, use the WorksheetFunction.SumIfs method. The syntax is Application.WorksheetFunction.SumIfs(sum_range, criteria_range1, criteria1, criteria_range2, criteria2, ...). All ranges must be the same size, and criteria can be numbers, text, cell references, or expressions like ">100". This guide shows you how to write it correctly, avoid common errors, and troubleshoot when the result is wrong or the macro fails.

What You'll Need

  • Excel with macros enabled (the Developer tab visible). If it is not visible, right-click the ribbon and choose Customize the Ribbon, then tick Developer.
  • A worksheet with data laid out in columns, for example a Region column, a Product column, and an Amount column.
  • Basic familiarity with the VBA editor (Alt+F11) and how to insert a module.

Step-by-Step

  1. Open the VBA editor. Press Alt+F11 (on Mac, Fn+Alt+F11 or use the Developer tab). In the Project Explorer, right-click your workbook, choose Insert, then Module. A blank code window appears.
  2. Write a Sub procedure. Type Sub SumWithCriteria() and press Enter; VBA adds End Sub. All code goes between these lines.
  3. Declare variables. Add Dim total As Double and Dim ws As Worksheet. Set Set ws = ThisWorkbook.Worksheets("Sheet1"), replacing Sheet1 with your sheet name. Using a worksheet variable avoids errors when the active sheet changes.
  4. Call SumIfs with two criteria. For example: total = Application.WorksheetFunction.SumIfs(ws.Range("C2:C100"), ws.Range("A2:A100"), "North", ws.Range("B2:B100"), "Widget"). Here C is the sum range, A holds regions, B holds products. The first pair sums only rows where column A equals North and column B equals Widget.
  5. Use cell references for criteria. Instead of hard-coding, point to cells: ws.Range("A2:A100"), ws.Range("E1"). This lets users change the criteria without editing code. Make sure E1 contains the exact text or number.
  6. Add more criteria pairs. SumIfs accepts up to 127 pairs. Append , ws.Range("D2:D100"), ">=100" to add a numeric condition. Keep the order: sum range first, then each criteria range followed by its criteria.
  7. Handle text with wildcards. Use "*Widget*" to match any text containing Widget, or "?" for a single character. Wildcards work in criteria strings, not in cell references unless the referenced cell contains them.
  8. Output the result. Write ws.Range("G1").Value = total to place the sum in a cell, or MsgBox total to display it. Run the macro with F5. If the result is 0, check range sizes and criteria spelling.
  9. Wrap in error handling (optional but recommended). Add On Error GoTo ErrHandler before the SumIfs line and a label at the end: ErrHandler: MsgBox "Error: " & Err.Description. This catches mismatched ranges or missing sheets.

Labels in the VBA editor and ribbon can differ slightly by Excel version and platform, but the SumIfs method and argument order are the same in Excel 2010 and later on Windows and Mac.

Common Mistakes

  • Mismatched range sizes. SumIfs requires every range to have the same number of rows and columns. If C2:C100 is paired with A2:A99, you get a run-time error 1004 or a wrong total. Use identical dimensions.
  • Forgetting quotes around text criteria. "North" works; North without quotes is treated as a variable and causes a compile error.
  • Using the wrong comparison operator. For greater than or equal to, write ">=100" as a string. Writing >=100 outside quotes is invalid syntax.
  • Assuming SumIfs is case-sensitive. It is not. North and north match. If you need case sensitivity, use SUMPRODUCT with EXACT instead.
  • Pointing at the wrong sheet. If ranges are unqualified, VBA uses the active sheet, which may not be the one you expect. Always qualify with a worksheet variable.
  • Criteria with leading or trailing spaces. "North " will not match "North". Trim cell values or use wildcards carefully.

Tips

  • Use named ranges in the worksheet and refer to them in VBA for readability, for example ws.Range("Amounts").
  • Store criteria in cells and reference them, so the macro stays flexible without code changes.
  • For dates, use ">=" & DateSerial(2024,1,1) or reference a date cell; do not hard-code locale-specific date strings.
  • Test with a small range first, then expand. A quick Debug.Print total in the Immediate window shows the result without writing to a cell.
  • If you need to sum with OR logic (North or South), SumIfs cannot do it directly; sum two SumIfs calls or use SUMPRODUCT.

Troubleshooting

  • Run-time error 1004: Usually mismatched range sizes or an invalid sheet name. Check every range has the same dimensions and the sheet exists.
  • Result is 0 when it should not be: Verify criteria spelling, check for hidden spaces, confirm the sum range contains numbers (not text stored as text), and make sure the criteria range actually contains the value.
  • Compile error: Expected: end of statement: A missing quote or parenthesis. Count the commas and quotes in the SumIfs line.
  • Wrong total: Print each range's address with Debug.Print ws.Range("A2:A100").Address to confirm you are referencing the intended columns.
  • Macro does nothing: Ensure macros are enabled and the code is in a standard module, not a sheet module, unless you intend it to run on events.

Frequently Asked Questions

Can SumIfs use more than two criteria?

Yes. Add additional criteria range and criteria pairs, up to 127 pairs. Each pair must have matching range sizes.

Does VBA SumIfs work with wildcards?

Yes, in criteria strings. Use * for any number of characters and ? for a single character. Wildcards do not work with numeric comparisons.

Why does SumIfs return an error when my ranges are on another sheet?

Ranges on different sheets are fine, but each range must be fully qualified with its worksheet, for example Worksheets("Data").Range("A2:A100"). Unqualified ranges default to the active sheet.

Is SumIfs case-sensitive in VBA?

No. It ignores case. For case-sensitive summing, use SUMPRODUCT with the EXACT function.

Can I use a cell reference as a criterion?

Yes. Pass the range object, for example ws.Range("E1"), as the criteria argument. The cell's value is used.

What is the maximum number of criteria pairs?

127 pairs. Beyond that, restructure your data or use a helper column.