【问题标题】:Do task for each date in table VBA为表 VBA 中的每个日期执行任务
【发布时间】:2019-11-11 09:01:13
【问题描述】:

我有这个 VBA,它根据我的表“Analysetable”中的值创建一个数组

Sub BestilAnalyser()

Dim i As Integer
Dim j As Integer
Dim n As Integer
Dim OrderArray() As Variant
Dim xArray() As Variant


Application.ScreenUpdating = False


For i = 1 To Range("AnalyseTable").Rows.Count

    If Not IsEmpty(Range("AnalyseTable[Date]")(i)) And _
        WorksheetFunction.CountIf(Range("AnalyseTable[Bund]")(i).Offset(0, 1).Resize(1, 12), "=1") > 0 Then

        ReDim Preserve OrderArray(n)

        OrderArray(n) = i

        ReDim Preserve xArray(0 To 12 - 1, n)

        For j = 0 To 12 - 1
            If Range("AnalyseTable[Bund]")(i).Offset(0, 1 + j).Value = 1 Then
                xArray(j, n) = "x"
            End If
        Next j

        n = n + 1

    End If

Next i

Call WriteToOrderForm(OrderArray, xArray)

Call WriteEmail

For i = 0 To n - 1
    For j = 1 To 12
        If xArray(j - 1, i) = "x" Then
            Range("AnalyseTable[Bund]")(OrderArray(i)).Offset(0, j) = 2
        End If
    Next j
Next i

Application.ScreenUpdating = True

End Sub

所以我想要帮助的是如何编写 VBA,以便它为 col“AnalyseTable[Date]”中的每个日期执行代码。同一个日期很可能会出现多次,然后为所有 1. Nov、所有 3. Nov 等运行 VBA。

我希望这是可行的。

【问题讨论】:

  • 我很困惑,一旦你使用名称Analysetable,其他情况下使用AnalyseTable 和另一个AnalyseTabel,你知道吗?
  • 是的,我错过了,现在更正了

标签: excel vba for-loop


【解决方案1】:

可能是正常的for循环

Dim cel As Range
For Each cel In Range("AnalyseTable[Date]")
    Debug.Print cel.Value
    ' do some your code
Next cel

【讨论】:

  • 我已经尝试过该解决方案,这也是我的 goto 解决方案,但我只是设法让 VBA 使用所有数据运行 400 次。也许我应该减少测试运行..
猜你喜欢
  • 2013-12-29
  • 1970-01-01
  • 2011-01-16
  • 1970-01-01
  • 2010-09-09
  • 2018-02-22
  • 1970-01-01
  • 1970-01-01
  • 2020-09-28
相关资源
最近更新 更多