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.

New contributor

Paulonious1 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

1

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)

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật