Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays VBA循环向被错误覆盖的数组添加行?_Arrays_Excel_Vba_Loops_Overwrite - Fatal编程技术网

Arrays VBA循环向被错误覆盖的数组添加行?

Arrays VBA循环向被错误覆盖的数组添加行?,arrays,excel,vba,loops,overwrite,Arrays,Excel,Vba,Loops,Overwrite,我一直在为一艘潜艇工作,在同一个问题上被困了一小段时间,希望有人能找到一个简单的解决办法 我有一个按站点名称排列的数据行(每个站点大约6行(月)),简单地说,我想为每个站点名称,将适当的数据行提取到一个变量/(数组?)中,以便稍后使用进行一些“后端”计算 到目前为止,我掌握的代码是: Sub Electrical_Checks() Dim a As Integer Dim i As Integer Dim ElectricalData As Variant a = Worksheets("1

我一直在为一艘潜艇工作,在同一个问题上被困了一小段时间,希望有人能找到一个简单的解决办法

我有一个按站点名称排列的数据行(每个站点大约6行(月)),简单地说,我想为每个站点名称,将适当的数据行提取到一个变量/(数组?)中,以便稍后使用进行一些“后端”计算

到目前为止,我掌握的代码是:

Sub Electrical_Checks()

Dim a As Integer
Dim i As Integer
Dim ElectricalData As Variant

a = Worksheets("1. Electrical Checks_Yes_No_CFC").Cells(Rows.Count, 1).End(xlUp).Row

' get electrical data per station
For Each Cell In Worksheets("Total Checks").Range("StationNames") 'for each station name in the StationNames dynamic range in Total Checks sheet
c = 0
For i = 1 To a 'if match in Checks sheet, extract row to ElectricalData
    If Worksheets("1. Electrical Checks_Yes_No_CFC").Cells(i, 3) = Cell Then
    c = c + 1
    ElectricalData = Application.Transpose(Worksheets("1. Electrical Checks_Yes_No_CFC").Rows(i).Columns("A:T")) 'transpose to make ReDim Preserve work

    ReDim Preserve ElectricalData(1 To 20, 1 To c) 'add new column

    End If
Next i
Debug.Print ElectricalData 'my inelegant way to bring up an error to check in locals window

Next Cell

End Sub
所以对我来说,这看起来像一个嵌套的for循环(对于每个站点,对于每条线路),只取一个站点,我通过“电气检查”表找到包含站点名称的行,“提取”匹配的适当行,当发现一个新行时,我尝试使用transpose和ReDim Preserve将新的转置行添加到ElectricalData数组中-这将生成一个包含20行6列(每月一列)的2D数组

然而,我在每次迭代I时发现,数据提取得很好,但一直覆盖第一列,而不是沿数组保存,如图所示:

其中,0.310018值肯定是最后一个月的参数。当我在脚本中按F8键时,ElectricalData中的列数增加了1,但数据总是保存在第一列中,而不是移动。如果有人知道为什么空列保持为空(我是否错误地使用了ReDim Preserve?)

非常感谢,, C

类似这样的东西:

Sub Electrical_Checks()

    Dim a As Long
    Dim i As Long

    Dim ElectricalData() As Variant, shtECYN As Worksheet
    Dim d, n As Long, m As Long, Cell, c As Long

    Set shtECYN = Worksheets("1. Electrical Checks_Yes_No_CFC")

    a = shtECYN.Cells(Rows.Count, 1).End(xlUp).Row

    ' get electrical data per station
    'for each station name in the StationNames dynamic range in Total Checks sheet
    For Each Cell In Worksheets("Total Checks").Range("StationNames")

        'how many matching lines?
        n = Application.CountIf(shtECYN.Cells(i, 3).Resize(a, 1), Cell.Value)
        ReDim Preserve ElectricalData(1 To 20, 1 To n) '<<< size the array to match
        c = 0
        For i = 1 To a 'if match in Checks sheet, extract row to ElectricalData
            If shtECYN.Cells(i, 3) = Cell.Value Then
                c = c + 1
                d = shtECYN.Rows(i).Columns("A:T")
                For m = 1 To UBound(d, 2)
                    ElectricalData(m, c) = d(1, m)
                Next m
            End If
        Next i
        'check the array content (for debugging purposes)
        Sheets("test").Range("A1").Resize(20, n).Value = ElectricalData
    Next Cell

End Sub
子电气检查()
暗淡如长
我想我会坚持多久
Dim ElectricalData()作为变量,shtECYN作为工作表
尺寸d,n为长,m为长,单元,c为长
Set shtECYN=工作表(“1.电气检查\u是\u否\u CFC”)
a=shtECYN.Cells(Rows.Count,1).End(xlUp).Row
'获取每个站点的电气数据
'对于总检查表中StationNames动态范围中的每个桩号名称
对于工作表中的每个单元格(“总检查”)。范围(“站点名称”)
“有多少条匹配的线?
n=Application.CountIf(shtECYN.Cells(i,3).调整大小(a,1),Cell.Value)

ReDim Preserve ElectricalData(1到20,1到n)“每次点击匹配行时,您都要将
ElectricalData
重新创建为一个新数组,然后调整其大小(每次点击多添加一列)。更好的方法可能是使用
Application.Countif()
首先确定匹配行的数量,然后在进入循环之前调整数组的大小。@TimWilliams我试图编写一个解决方案,在循环过程中调整数组的大小-真的没有办法这样调整和保留多维数组吗?你可以确定调整和保留数组的大小(最后一个维度仅适用于多维数组)-问题是你每次通过循环都会覆盖你的数组。哦,蒂姆,这很有效,我正在慢慢地了解应用程序。countif(VBA还不是我的强项!)你这样做节省了我太多时间,非常感谢!