【问题标题】:Use array values in a formula - vba在公式中使用数组值 - vba
【发布时间】:2020-03-01 14:04:25
【问题描述】:

真的会很高兴友好地推动正确的方向。

我正在尝试使用数组作为变量来使用基于 COUNTIFS 公式的值填充列,例如“单元格 - 将数组的新元素作为变量的公式”。 所以 AF2 - Brand2 的计数 AF3 - Brand3 的计数 .. 现在我只得到整个范围的数组最后一个元素的值(我可以看到 excel 如何遍历其余元素)。或多或少地尝试了我所知道的一切,并查看了早期的问题 - 无济于事。不胜感激。

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 

【问题讨论】:

  • 但是,你的问题是什么?您收到错误消息吗?如果是,是什么错误以及在哪个代码行上。如果没有,您的代码会根据您的需要产生什么?
  • 正如我所写的,该列正在由具有数组最后一个元素的公式填充。所以例如(C:C;12;H:H;"Head";F:F;"品牌 4")。品牌 1、品牌 2、品牌 3 未保存
  • 一些示例数据会有所帮助。您正在将公式应用于整个列(这将需要一段时间 - 很肯定不是您想要的)并且您正在为 4 个品牌重复该过程。问题:您如何决定配方中应包含哪个品牌?
  • 但是,这就是您的代码所做的。请在End With 之前加上Stop,并在代码停止时检查公式。您将看到它使用第二个数组元素(第一个必须使用 0),然后是第三个。之前的公式将被最后一次迭代所取代......你觉得这些公式看起来如何?
  • 如果您直接在 Excel(不是 vba)中手动执行,公式会是什么?像这样...=COUNTIFS(C:C,12;H:H,"Head",F:F,"Brand1") + COUNTIFS(C:C,12,H:H,"Head",F:F,"Brand2") + COUNTIFS(C:C,12,H:H,"Head",F:F,"Brand3") + COUNTIFS(C:C,12,H:H,"Head",F:F,"Brand4")

标签: arrays excel vba loops


【解决方案1】:

您的解释与您的代码不符,但重新阅读它们并且您的上一张图片证实了我的怀疑。

阅读 cmets 并根据需要进行调整

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

代码:

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

让我知道它是否有效

【讨论】:

  • 非常感谢您的帮助!有一个运行时错误 9,单元格上的 subsript 超出范围。Formula = "=COUNTIFS(C:C," & RTrim(Month(Mesyaz3)) & ",H:H,""Head"",F :F," & Chr(34) & Brand(counter - 1) & Chr(34) & ")" 将深入研究它,可能会设法修复它
  • 我已经调整了数组计数器的管理方式。请用新代码替换代码并进行相应调整。告诉我进展如何
  • 完美运行!非常感谢你教我方法!太夸张了!
【解决方案2】:

请替换您的下一个代码:

    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 在这里没有任何意义。您的品牌没有任何要迭代的参考。它们只能由代码添加...

【讨论】:

  • 你是我个人的耶稣——把我从两天的痛苦中拯救出来!你是绝对正确的 - 自动填充是错误!!!
  • @Anna:恐怕,不完全是……AutoFill 只是糟糕的情况。这个技巧特别是通过使用Range.("AF" & i) 完成的。否则,您的迭代会在所有列上创建一个公式,但引用最后一个数组元素...
  • 大师,是的,我意识到了。只是从未遇到过这种填充范围的方法。它是如此优雅。接触 VBA 的时间不长。
猜你喜欢
  • 2021-02-01
  • 2015-01-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多