arrays - 如何将值传递给数组然后遍历数据集

标签 arrays excel vba loops dynamic

我正在尝试构建一个循环以从 E 列中对 200 个连续行(或从输入框确定的行数)求和,然后将范围向下移动一个单元格并重新计算(循环),直到它遍历所有使用的单元格在命名的工作表中。

出于循环的目的,数组的长度/大小是静态的,它是通过输入框向用户请求的,并且在循环完成之前保持不变。

到目前为止,我已经使用静态单元格引用完成了一次计算,例如。 “E2:E201”如下图所示:

ClosingPrice200Array = Range("E2:E201").Value

所以我知道代码可以计算范围的总和并除以一个变量。

我遇到的问题包括:
  • 我想将接下来的 200 行从 E3:E202、E4:E203 等一直到最后使用的行求和,而不用命名范围的每次变化。

  • 为此,我声明了行变量,确认我计算了正确的行数并尝试使用行变量将第一个范围(E2:E201)传递给命名数组,但是我收到“编译错误:无法分配从我的代码到数组”:
    Sub Calculate200DMA()
    
        Dim Dma200current As Double
        Dim SumLast200Days As Long
        Dim MovingAverageLength As Integer
        MovingAverageLength = Application.InputBox("Input Required", "Moving Average Length", 200, , , , , 1)
        Dim ClosingPrice200Array(MovingAverageLength) as Variant 
    

    '不记得数组大小是否需要为常数,但我可以创建其他数组变量来满足需要,因此请告知我是否无法在每次运行宏时动态调整数组大小。
    Dim FirstRow As Integer
    Dim Lastrow As Integer
    Dim CurrentRow As Integer
    Dim NumberOfRows As Long
    
    'count number of rows to be used in the For loop
    Sheets("Data processing").Select
    NumberOfRows = ActiveSheet.UsedRange.Rows.Count
    
    FirstRow = 2
    CurrentRow = FirstRow + (MovingAverageLength - 1)
    Lastrow = CurrentRow
    

    '这是导致编译错误的行:
    ClosingPrice200Array = Sheets("Data processing").Range(Cells(FirstRow, 5), Cells(Lastrow, 5)).Value
    

    '其余的似乎在计算我想要的数字方面工作正常:
    SumLast200Days = Excel.WorksheetFunction.Sum(ClosingPrice200Array)
    Dma200current = SumLast200Days / MovingAverageLength
    Sheets("Data processing").Cells(CurrentRow, 10).Value = Dma200current
    
    'then i want to increment to the For loop but i have not written the start nor end loop yet because not sure exactly how to write (have been googling how to populate the array first)
    FirstRow = FirstRow + 1
    Lastrow = Lastrow + 1
    CurrentRow = CurrentRow + 1
    
  • 此外,输入框也没有提示输入范围的长度(行数)。这是我的输入框代码:
    Dim MovingAverageLength As Integer MovingAverageLength = Application.InputBox("Input Required", "MovingAverageLength", 200, , , , , 1)

  • 预先感谢您的帮助。到目前为止,在我的谷歌搜索(这里或其他地方)中找不到完全解决问题的东西。

    约翰

    最佳答案

    您可以直接使用WorksheetFunction.Average()功能和使用范围:

    Option Explicit
    
    Sub Calculate200DMA()
        Dim MovingAverageLength As Long, LastRow As Long
        MovingAverageLength = Application.InputBox("Input Required", "Moving Average Length", 200, , , , , 1)
    
        Dim FirstRow As Long, CurrentRowOffset As Long, LastRowOffset As Long
    
        FirstRow = 2
        With Sheets("Data processing")
            LastRow = .Cells(.Rows.Count, 4).End(xlUp).Row
            LastRowOffset = (LastRow - FirstRow + 1) - MovingAverageLength ' set the last offset from first row
            With .Cells(FirstRow, 4).Resize(MovingAverageLength) ' reference the first range to sum
                For CurrentRowOffset = 0 To LastRowOffset
                    .Cells(.Rows.Count).Offset(CurrentRowOffset, 6).Value = WorksheetFunction.Average(.Offset(CurrentRowOffset))
                Next
            End With
        End With
    End Sub
    

    关于arrays - 如何将值传递给数组然后遍历数据集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49891047/

    相关文章:

    python - 从 3D numpy 数组中选择多个补丁

    c++ - 内部地址空间重用与在循环内创建的 vector

    excel - 抓取本地 HTML 文件

    excel - 使用 vlookup 从 Sheet2 读取 Sheet1 上的值

    Excel VBA : how to apply bold format to all words before ":"?

    excel - 使用 VBA 将 Excel 数据保存为 csv - 删除文件末尾的空白行以保存

    arrays - Ruby——当我调用一个数组的方法时,是否需要将方法中的参数指定为数组的名称?

    vba - 计算连胜

    arrays - 我正在尝试通过 for each 循环将值添加到数组中

    c - C 中数组的动态内存分配