Search code examples
excelvba

How can I adjust the COUNTIF and SUMIF formula recorded in a Macro to reference entire column contents?


I am creating a small tool for my team to do rerates of line items. I have basically everything finished and figured it'd be nice to save a summary of the data for them as well to remove more of the manual work for them.

I'm still very new to VBA and I still using macros as a crutch to start my code, then edit from there. I found that creating a pivot table in VBA was beyond my abilities at this point in time and resigned to creating basic SUMIF and COUNTIF formulas in the macro. It works for the limited Row/column counts I have but I'd like it to be dynamic, specifically for the row count in each formula reference.

The columns headers will always be static, but the rows will vary depending on the amount of shipments we need to rerate. I know I could just jack the cell references to some very high numbers, but I'm worried about that affecting performance (if it won't, let me know and I'll be happy doing that).

This is what I have now. The set of data I was using for the macro only had 64 rows as will be evidenced in the SUMIF and COUNTIF formulas. One last note: the section titled "Column Totals" can be left alone, that section will be static as it's effectively the output of the summary and will always be in those cells.

Sub TestTable()
'
' TestTable Macro
'
' Keyboard Shortcut: Ctrl+t


'Summary Sheet

'Naming Summary Columns
Worksheets("Summary").Select
    Range("B3").Select
    ActiveCell.FormulaR1C1 = "Mail Class"
    Range("C3").Select
    ActiveCell.FormulaR1C1 = "Count of Postage"
    Range("D3").Select
    ActiveCell.FormulaR1C1 = "Sum of Postage"
    Range("E3").Select
    ActiveCell.FormulaR1C1 = "Sum of Rerate"
    Range("F3").Select
    ActiveCell.FormulaR1C1 = "Sum of Difference"


'List unique Mail Classes
Worksheets("Summary").Select
    Range("B4").Select
    ActiveCell.FormulaR1C1 = _
        "Ground Advantage"
    Range("B5").Select
    ActiveCell.FormulaR1C1 = _
        "Priority"
'Count Unique Mail Classes
    Range("C4").Select
    ActiveCell.FormulaR1C1 = _
        "=COUNTIF('Test Sheet'!R3C1:R64C1,""Ground Advantage"")"
    Range("C5").Select
    ActiveCell.FormulaR1C1 = "=COUNTIF('Test Sheet'!R3C1:R64C1,""Priority"")"
    Range("C3").Select
' Sum of Postage
    Range("D4").Select
    ActiveCell.FormulaR1C1 = _
        "=SUMIF('Test Sheet'!R3C1:R64C1,""Ground Advantage"",'Test Sheet'!R3C4:R64C4)"
    Range("D5").Select
    ActiveCell.FormulaR1C1 = _
        "=SUMIF('Test Sheet'!R3C1:R64C1,""Priority"",'Test Sheet'!R3C4:R64C4)"
'Sum of Rerate
    Range("E4").Select
    ActiveCell.FormulaR1C1 = _
        "=SUMIF('Test Sheet'!R3C1:R64C1,""Ground Advantage"",'Test Sheet'!R3C5:R64C5)"
    Range("E5").Select
    ActiveCell.FormulaR1C1 = _
        "=SUMIF('Test Sheet'!R3C1:R64C1,""Priority"",'Test Sheet'!R3C5:R64C5)"
'Sum of Difference
    Range("F4").Select
    ActiveCell.FormulaR1C1 = _
        "=SUMIF('Test Sheet'!R3C1:R64C1,""Ground Advantage"",'Test Sheet'!R3C6:R64C6)"
    Range("F5").Select
    ActiveCell.FormulaR1C1 = _
        "=SUMIF('Test Sheet'!R3C1:R64C1,""Priority"",'Test Sheet'!R3C6:R64C6)"
'Column Totals

    Range("C6").Select
    ActiveCell.FormulaR1C1 = _
        "=SUM('Summary'!R4C3:R5C3)"
    Range("D6").Select
    ActiveCell.FormulaR1C1 = _
        "=SUM('Summary'!R4C4:R5C4)"
    Range("E6").Select
    ActiveCell.FormulaR1C1 = _
        "=SUM('Summary'!R4C5:R5C5)"
    Range("F6").Select
    ActiveCell.FormulaR1C1 = _
        "=SUM('Summary'!R4C6:R5C6)"
    Range("G6").Select
    ActiveCell.FormulaR1C1 = _
        "Grand Total"
'Formatting Cells and Data type
    Cells.Select
    Cells.EntireColumn.AutoFit
    Range("D4:F6").Select
    Selection.Style = "Currency"
    Range("C3").Select
End Sub

If there's a better way to accomplish what I'm doing in a cleaner, dynamic way I'm open to hearing it. I've been reading through the VBA documentation but with my limited experience, I'm unsure what could really do the trick.


Solution

  • First, a quick VBA tip. If you see a pattern of Range.Select followed by ActiveCell.DoSomething, you should combine those into one line. Instead of

    Range("B3").Select
    ActiveCell.FormulaR1C1 = "Mail Class"
    

    write

    Range("B3").FormulaR1C1 = "Mail Class"
    

    As for making ranges dynamic, there are a lot of options. Here are a few:

    Tables

    Make the data on Test Sheet a table. Select the data and press Ctrl+T or on the Ribbon, Insert - Table. Then these lines

    Range("C4").Select
    ActiveCell.FormulaR1C1 = _
        "=COUNTIF('Test Sheet'!R3C1:R64C1,""Ground Advantage"")"
    

    assuming you named your table tblTest and the first column is titled "Type", the new formula would be

    Range("C4").FormulaR1C1 = "=COUNTIF(tblTest[Type],""Ground Advantage"")"
    

    As you add or remove rows from the table, this referencing (called Structured Table Reference) always points to the whole table.

    Since you're putting the Ground Advantage label in B4, that formula should probably be

    Range("C4").FormulaR1C1 = "=COUNTIF(tblTest[Type],B4)"
    

    Find the Last Row

    You start at the last row and use the .End method to go up until you hit the last row that has something in it.

    Dim lLastRow As Long
    lLastRow = Sheets("Test sheet").Range("A1048576").End(xlUp).Row
    Range("C4").FormulaR1C1 = "=COUNTIF('Test Sheet'!R3C1:R" & lLastRow & "C1,B4)"
    

    Since the formula is just a string, you can insert the last row you found in place of the 64 you had hardcoded.

    UsedRange

    Worksheets have a UsedRange property. You can use the Intersect method to limit a column to just the used range part. As you add and delete rows, the UsedRange can get a little out of whack. But it's never too small, only too large. And since you're only limiting this for performance reasons, it won't be a problem.

    Dim rRange As Range
    Dim sh As Worksheet
    Set sh = Sheets("Test Sheet")
    Set rRange = Intersect(sh.Columns(1), sh.UsedRange)
    Range("C4").FormulaR1C1 = "=COUNTIF('Test Sheet'!" & rRange.Address(, , xlR1C1) & ",B4)"
    

    If the sheet's UsedRange is A1:K256, then that Intersect command will return A1:A256. Then I used the Address property with the option xlR1C1 argument to build the string that is the formula.

    It's a bit of a shortcut to use the whole column in the Intersect. If you really need to start in row 3, you can change that to

    Set rRange = Intersect(sh.Range("A3:A1048576"), sh.UsedRange)