Ваше объяснение не соответствует вашему коду, но перечитывание их и ваше последнее изображение подтвердили то, что я подозревал.
Прочитайте комментарии и настройте их в соответствии с вашими потребностями
РЕДАКТИРОВАТЬ: Настроил счетчик для массива бренда
Код:
Sub Summa_po()
Dim targetSheet As Worksheet
Dim targetRange As Range
Dim cell As Range
Dim Mesyaz1 As Date
Dim Mesyaz2 As Date
Dim Mesyaz3 As Date
Dim Brand() As Variant
Dim firstRow As Long
Dim lastRow As Long
Dim counter As Long
' If you need to work with a specific sheet change the next line to ThisWorkbook.Worksheets("NAME OF THE SHEET")
Set targetSheet = ThisWorkbook.ActiveSheet
' Define brands array
Brand = Array("Brand1", "Brand2", "Brand3", "Brand4")
' Define start row
firstRow = 2
' Find last row in column AF uncomment next line
' lastRow = targetSheet.Cells(targetSheet.Rows.Count, "AF").End(xlUp).Row
' Or set directly the last row
lastRow = 55
' Calc previous months
Mesyaz1 = DateAdd("m", -1, Date)
Mesyaz2 = DateAdd("m", -2, Date)
Mesyaz3 = DateAdd("m", -3, Date)
' Define the target range in column AF
Set targetRange = targetSheet.Range("AF" & firstRow & ":AF" & lastRow)
' Loop through each cell in range
For Each cell In targetRange.Cells
If counter = UBound(Brand) + 1 Then
counter = 1
Else
counter = counter + 1
End If
cell.Formula = "=COUNTIFS(C:C," & Month(Mesyaz3) & ",H:H,""Head"",F:F," & Chr(34) & Brand(counter - 1) & Chr(34) & ")"
Next cell
' Are you assigning this to a name? better use Thisworkbook.Names("")
[AF1] = "Head"
End Sub
Дайте мне знать, если это работает