在公式中使用数组值-VBA

时间:2020-03-01 14:04:25

标签: arrays excel vba loops

真的会把友善的推向正确的方向。

我正在尝试使用数组作为变量,以基于COUNTIFS公式的值填充列,例如“单元格-使用数组的新元素作为变量的公式”。 因此,AF2-Brand2的优势 AF3-Brand3的标志 .. 现在,我仅获得整个范围的数组最后一个元素的值(并且我可以看到excel如何遍历其余部分)。尝试或多或少地了解了我所知道的一切,并浏览了先前的问题-无济于事。希望能有所帮助。

How it would look graphically

Option Explicit
Sub Summa_po()

Dim Mesyaz1 As Date
Dim Mesyaz2 As Date
Dim Mesyaz3 As Date
Dim Brand()
Dim i As Long

Worksheets(1).Activate

Mesyaz1 = DateAdd("m", -1, Now)
Mesyaz2 = DateAdd("m", -2, Now)
Mesyaz3 = DateAdd("m", -3, Now)
Brand = Array("Brand1", "Brand2", "Brand3", "Brand4")
For i = LBound(Brand) To UBound(Brand) 'Here I tried also For i=0 to UBound(Brand)
With ActiveSheet.Range("AF:AF")
.Formula = "=COUNTIFS(C:C," & RTrim(Month(Mesyaz3)) & ",H:H,""Head"",F:F," & Chr(34) & Brand(i) & Chr(34) & ")"
.AutoFill Destination:=ActiveSheet.Range("AF2:AF55") 'I used to draw it to the last cell but it is not a problem appararently
  End With
  Next i

[AF1] = "Head"
End Sub 

2 个答案:

答案 0 :(得分:0)

您的解释与您的代码不匹配,但是重新阅读它们后,您的最后一张图片证实了我的怀疑。

阅读评论并根据您的需要进行调整

编辑:调整了品牌阵列的计数器

代码:

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

让我知道它是否有效

答案 1 :(得分:0)

请替换您的下一个代码:

    For i = LBound(Brand) To UBound(Brand) 'Here I tried also For i=0 to UBound(Brand)
        With ActiveSheet.Range("AF:AF")
            .Formula = "=COUNTIFS(C:C," & RTrim(Month(Mesyaz3)) & ",H:H,""Head"",F:F," & Chr(34) & Brand(i) & Chr(34) & ")"
            .AutoFill Destination:=ActiveSheet.Range("AF2:AF55") 'I used to draw it to the last cell but it is not a problem appararently
       End With
   Next i

下一个:

  For i = LBound(Brand) To UBound(Brand) 
        Range("AF" & i + 2).Formula = "=COUNTIFS(C:C," & RTrim(Month(Mesyaz3)) & _
                   ",H:H,""Head"",F:F," & Chr(34) & Brand(i) & Chr(34) & ")"
  Next i

AutoFill在这里没有任何意义。您的品牌没有任何可重复的参考。它们只能通过代码添加...