【问题标题】:Excel VBA Array-variable stepped formula that returns only the modified steps/valuesExcel VBA 数组变量步进公式,仅返回修改后的步骤/值
【发布时间】:2017-09-08 23:11:52
【问题描述】:

我有 130000 行的大型数据集,其中包含一个时间列和两个数据列。我已将所有数据复制到一个变体中,并在每 200 行上执行一个 Stepped 平均函数。第 1 列保持不变,但我目前将平均第 2 列和第 3 列的结果放在范围的中点,例如。第 100 行。我只想将具有更改数据的行从所有三个列返回到电子表格。我只是 VBA 的新手,对于什么是可能的或如何只获取这些数据没有任何想法。

我之前使用 select if 循环类型的方法来过滤电子表格上的数据,而不是先将数据复制到变体中,但宏运行起来很慢。

Dim DataRange As Variant
Dim Irow As Long
Dim Icol As Integer
Dim Mean As Double
Dim Span As Long

'Copies the data to the VBA variant
DataRange = Range("A12:C" & Cells(Rows.Count, "B").End(xlUp).Row)

'new time interval devided by the original time interval to get half the number of cells for range
Span = Round(Range("H10") / Range("B9") / 2, 0)

'want to select the mid point between the span as the starting point
For Irow = LBound(DataRange) + Span To UBound(DataRange) - Span Step Span * 2
    For Icol = 2 To 3
        Mean = DataRange(Irow, Icol)

       'Average the values in the array
       Mean = WorksheetFunction.Average(DataRange(Irow - Span, Icol), DataRange(Irow + Span, Icol))

       DataRange(Irow, Icol) = Mean

   Next Icol
Next Irow

'writes all the results back to the range at once
Range("D12:F" & Cells(Rows.Count, "B").End(xlUp).Row).Value = DataRange 

【问题讨论】:

  • 所以你想清除之前的数据,只保留计算的平均值?
  • 最后,我希望将它们放在原始数据旁边的三个新列中。即列 D 到 F 或在计算它们的新变量中,以便在数组完成后粘贴它们
  • 或者它们可以从“DataRange”数组中删除,因为平均函数中没有重叠

标签: arrays excel variables array-formulas vba


【解决方案1】:

所以我找到了一个解决方案,并认为我会分享。不完美,因为结果输出在每行之间有一个额外的空间。我在确定行输出时一定有错误,但我似乎可以计算出在哪里。

   Dim DataRange As Variant
   Dim Irow As Long
   Dim Icol As Integer
   Dim Mean As Double
   Dim Span As Long
   Dim ResultRange As Variant

'Copies the data to the VBA variant
DataRange = Range("A12:C" & Cells(Rows.Count, "A").End(xlUp).Row)
ResultRange = Range("E12:G" & Cells(Rows.Count, "A").End(xlUp).Row)

   'new time interval / original time interval to get half number of cells for range
    Span = Round(Range("E3") / Range("B9") / 2, 0)

    'want to select the mid point between the span as the starting point
    For Irow = LBound(DataRange) + Span To UBound(DataRange) - Span Step Span * 2
    For Icol = 2 To 3

    'Average the values in the array
    Mean = WorksheetFunction.Average(DataRange(Irow - Span, Icol), DataRange(Irow + Span, Icol))

    If DataRange(Irow, 1) >= Range("e6") Then

    ResultRange((Irow - 1) / (Span * 2), Icol) = Mean
    ResultRange((Irow - 1) / (Span * 2), 1) = DataRange(Irow, 1)

    End If
    Next Icol
    Next Irow

    'writes all the results back to the range at once
   Range("e12:g" & Cells(Rows.Count, "B").End(xlUp).Row).Value = ResultRange

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多