Excel error 1004 "Unable to get SumIfs property of WorksheetFunction class" appearing inconsistently

15 Views Asked by At

Option Explicit

Sub UpdateWeeklySummary() Dim dailyCountSheet As Worksheet Dim weeklySummarySheet As Worksheet Dim processRange As Range Dim processCell As Range Dim weekRange As Range Dim weekCell As Range Dim currentWeek As Integer Dim weeklyCount As Integer

Set dailyCountSheet = Worksheets("Daily Count")
Set weeklySummarySheet = Worksheets("WeeklySummary")

'Loop through each row in the process range
Set processRange = dailyCountSheet.Range("A2:A5")
For Each processCell In processRange
    'Loop through each column in the week range
    Set weekRange = dailyCountSheet.Range("B1:F1")
    For Each weekCell In weekRange
        'Calculate the week number for the current column
        currentWeek = WeekNum(weekCell.Value)
        
        'Calculate the total count for the current week and process
        weeklyCount = Application.WorksheetFunction.SumIfs(dailyCountSheet.Range("B2:F5"), dailyCountSheet.Range("A2:A5"), processCell.Value, dailyCountSheet.Range("B1:F1"), ">=" & weekCell.Value, dailyCountSheet.Range("B1:F1"), "<=" & DateAdd("d", 6, weekCell.Value))
        
        'Update the corresponding cell in the weekly summary table
        weeklySummarySheet.Cells(processCell.Row, currentWeek + 1).Value = weeklyCount
    Next weekCell
Next processCell

MsgBox "Weekly summary updated successfully."

End Sub

Function WeekNum(dt As Date) As Integer WeekNum = DatePart("ww", dt, vbMonday) End Function

I need to pull the data from daily count sheet and calculate each week wise in weekly summary sheet.

0

There are 0 best solutions below