How to Loop Table Range and Enter Function With Multiple Variables?

I have a table that is populated using a CountIfs formula. I would like to code this process into VBA without having to write each cell’s formula. I can use WorksheetFunction.CountIfs to automate individual results, but I haven’t been able to code this into a loop. I would like the function to reference the variable in column A (Associate) as well as the Dates in Row 2.

Here is the table I am attempting to populate, it is defined in the workbook as “TC_Month”:

Here is the section of code I have so far:

`Sub ConstData()

Dim wb As Workbook
Set wb = ThisWorkbook
Dim BD As Worksheet
Set BD = wb.Sheets("BuiltData")
Dim YD As Worksheet
Set YD = wb.Sheets("YearData")
Dim MD As Worksheet
Set MD = wb.Sheets("MonthData")

MD.Activate

Dim CurrentMonth As Date
CurrentMonth = DateSerial(Year(Date), Month(Date), 1)

Dim wk1 As Date
wk1 = CurrentMonth
Dim wk2 As Date
wk2 = DateAdd("d", 7, wk1)
Dim wk3 As Date
wk3 = DateAdd("d", 14, wk1)
Dim wk4 As Date
wk4 = DateAdd("d", 21, wk1)
Dim wk5 As Date
wk5 = DateAdd("d", 28, wk1)

With MD.Range("TC_Month")
.Cells(2, 2).Value2 = wk1
.Cells(2, 3).Value2 = wk2
.Cells(2. 4).Value2 = wk3
.Cells(2, 5).Value2 = wk4
.Cells(2, 6).Value2 = wk5
End With

With MD.Range("TC_Month")
Dim i As Integer
i = 3
Do Until i = 7
.Cells(i, 2).Value2 = WorksheetFunction.CountIfs(BD.Range("AF:AF"), _
MD.Range("TC_Month").Cells(i, 1), BD.Range("R:R"), ">250", _ 
BD.Range("AK:AK"), ">=" & CLng(wk1), BD.Range("AK:AK"), _
"<" & CLng(wk2))
i = i + 1
Loop
End With`

If I use the actual cell references, this function works. When using the Integer and Loop this code results in Excel freezing or an error. It also is only setup for the first column of the table, I haven’t been able to wrap my head around how the dates could be variable as well and the additional columns calculated.

If possible, I would appreciate help in adapting this code and function to work for an entire table. I could code each individual cell function, but that wouldn’t make for very clean code.

Thank you for your time!

4

Replacing the Do Loop with a For Loop simplifies the logic and is less prone to crashing.

    Dim r As Long
    For r = 3 To 7
        .Cells(r, 2).Value2 = WorksheetFunction.CountIfs(BD.Range("AF:AF"), _
            MD.Range("TC_Month").Cells(r, 1), BD.Range("R:R"), ">250", _
            BD.Range("AK:AK"), ">=" & (wk1), BD.Range("AK:AK"), _
            "<" & (wk2))
    Next

Using absolute and relative references in our formulas allow us to write formulas to multiple cells at once.

Sub ConstData()
    Dim CurrentMonth As Date
    CurrentMonth = DateSerial(Year(Date), Month(Date), 1)
    
    With ThisWorkbook.Sheets("MonthData").Range("TC_Month")
        .Range("B2:F2").Value = Array(CurrentMonth, CurrentMonth + 7, CurrentMonth + 14, CurrentMonth + 21, CurrentMonth + 28)
        .Range("B3:E7").Formula = "=COUNTIFS(BuiltData!$AF:$AF, $A3, BuiltData!$R:$R, "">250"", BuiltData!$AK:$AK, "">=""&B$2, BuiltData!$AK:$AK, ""<""&C$2)"
        .Range("F3:F7").Formula = "=COUNTIFS(BuiltData!$AF:$AF, $A3, BuiltData!$R:$R, "">250"", BuiltData!$AK:$AK, "">=""&F$2)"
         .Value = .Value ' This line converts the formulas into values 
    End With
End Sub

Refactored code

I simplified my code example for readability.

Sub ConstData()

    Dim wb As Workbook, BD As Worksheet, YD As Worksheet, MD As Worksheet
    Set wb = ThisWorkbook
    Set BD = wb.Sheets("BuiltData")
    Set YD = wb.Sheets("YearData")
    Set MD = wb.Sheets("MonthData")
    MD.Activate

    Dim CurrentMonth As Date
    CurrentMonth = DateSerial(Year(Date), Month(Date), 1)

    With MD.Range("TC_Month")
        Rem I perfer to use r for row and c for column for readability
        Dim r As Long, c As Long
    
        Rem Adding 7 to a Date is the same as DateAdd("d", 7, Date)
        For c = 2 To 6
            .Cells(2, c).Value = (c - 2) * 7 + CurrentMonth
        Next
    
        Dim weekStart As Date, weekEnd As Date
        Dim Associate As String
        
        Rem Rows 3 to 7
        For r = 3 To 7
            Associate = .Cells(r, 1).Value
            
            Rem Columns 2 to 5
            For c = 2 To 5
                weekStart = .Cells(2, c).Value
                weekEnd = .Cells(2, c + 1).Value
                .Cells(r, c).Value = WorksheetFunction.CountIfs(BD.Range("AF:AF"), _
                    Associate, _
                    BD.Range("R:R"), ">250", _
                    BD.Range("AK:AK"), ">=" & weekStart, _
                    BD.Range("AK:AK"), _
                    "<" & weekEnd)
            Next
            
            Rem Column 6 Should have a different fromula
            weekStart = .Cells(2, 6).Value
            .Cells(r, 6).Value = WorksheetFunction.CountIfs(BD.Range("AF:AF"), _
                Associate, _
                BD.Range("R:R"), ">250", _
                BD.Range("AK:AK"), ">=" & weekStart)
        Next
    End With
End Sub

Something like this should work (untested)

Sub ConstData()

    Dim wb As Workbook, BD As Worksheet, YD As Worksheet, MD As Worksheet
    Dim wk1 As Date, rngDates As Range, rngNames As Range, cD As Range, cN As Range
    Dim rngData As Range
    
    Set wb = ThisWorkbook
    Set BD = wb.Sheets("BuiltData")
    Set YD = wb.Sheets("YearData")
    Set MD = wb.Sheets("MonthData")    
    Set rngData = MD.Range("TC_Month")
    Set rngNames = rngData.Cells(3, 1).Resize(5)    'range with associates
    Set rngDates = rngData.Cells(2, 2).Resize(1, 5) 'range with dates
    
    wk1 = DateSerial(Year(Date), Month(Date), 1) 'start date
    
    For Each cD In rngDates 'for each date cell
        cD.Value2 = wk1     'populate date value
        wk1 = wk1 + 7       'add a week
        For Each cN In rngNames 'cells with associates
            'populate the value
            MD.Cells(cN.Row, cD.Column).Value = Application.CountIfs( _
                BD.Range("AF:AF"), cN.Value, _
                BD.Range("R:R"), ">250", _
                BD.Range("AK:AK"), ">=" & CLng(cD.Value), _
                BD.Range("AK:AK"), "<" & CLng(cD.Value + 7))
        Next cN
    Next cD
End Sub

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