Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.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 将特定行添加到另一个数组_Arrays_Vba_Excel_Sorting - Fatal编程技术网

Arrays 将特定行添加到另一个数组

Arrays 将特定行添加到另一个数组,arrays,vba,excel,sorting,Arrays,Vba,Excel,Sorting,我有一个代码,其中它遍历所有行,查看列I是否包含单词“MISS”,并将其添加到数组中。现在我想让它知道这些行是存储在其中的,而是将不在一个数组中的行存储到另一个数组中。我目前的代码是: Sub tester() max_targets = 6 Dim RowsToMiss(1 To 6) lRow = 2 Do Until Sheets("Result").Cells(lRow, 1) = "" If Sheets("Result").Cells(lRow, 9) = "MISS"

我有一个代码,其中它遍历所有行,查看列I是否包含单词“MISS”,并将其添加到数组中。现在我想让它知道这些行是存储在其中的,而是将不在一个数组中的行存储到另一个数组中。我目前的代码是:

Sub tester()

max_targets = 6
Dim RowsToMiss(1 To 6)

lRow = 2
Do Until Sheets("Result").Cells(lRow, 1) = ""
    If Sheets("Result").Cells(lRow, 9) = "MISS" Then
        RowsToMiss(Sheets("Result").Cells(lRow, 4)) = lRow
    End If
    lRow = lRow + 1
Loop

lRow = 2
Do Until Sheets("Result").Cells(lRow, 1) = ""
    If RowsToMiss(Sheets("Result").Cells(lRow, 4)) <> lRow Then
        'read the row in!
        Else
    End If
    lRow = lRow + 1
Loop
End Sub
子测试仪()
最大目标=6
昏暗的RowsToMiss(1到6)
lRow=2
直到工作表(“结果”)。单元格(lRow,1)=“
如果表格(“结果”)。单元格(lRow,9)=“未命中”,则
RowsToMiss(表(“结果”)。单元格(lRow,4))=lRow
如果结束
lRow=lRow+1
环
lRow=2
直到工作表(“结果”)。单元格(lRow,1)=“
如果行未命中(表(“结果”)。单元格(lRow,4))lRow,则
“读这一行!
其他的
如果结束
lRow=lRow+1
环
端接头

比如@brainac我不确定你在问什么,但我已经下了赌注。这将创建另一个数组,该数组存储不包含未命中的行号

Sub tester()

Dim max_targets As Long, lRow As Long, i As Long

max_targets = 6
Dim RowsToMiss(1 To 6)
Dim RowsToHit()

For lRow = 2 To Sheets("Result").Cells(Rows.Count, 1).End(xlUp).Row
    If Sheets("Result").Cells(lRow, 9) = "MISS" Then
        RowsToMiss(Sheets("Result").Cells(lRow, 4)) = lRow
    Else
        i = i + 1
        ReDim Preserve RowsToHit(1 To i)
        RowsToHit(i) = lRow
    End If
Next lRow

End Sub

比如@brainac,我不确定你在问什么,但我已经下了赌注。这将创建另一个数组,该数组存储不包含未命中的行号

Sub tester()

Dim max_targets As Long, lRow As Long, i As Long

max_targets = 6
Dim RowsToMiss(1 To 6)
Dim RowsToHit()

For lRow = 2 To Sheets("Result").Cells(Rows.Count, 1).End(xlUp).Row
    If Sheets("Result").Cells(lRow, 9) = "MISS" Then
        RowsToMiss(Sheets("Result").Cells(lRow, 4)) = lRow
    Else
        i = i + 1
        ReDim Preserve RowsToHit(1 To i)
        RowsToHit(i) = lRow
    End If
Next lRow

End Sub

如果需要向集合动态添加值,VBA会提供类型
集合
,这比
数组
更好,因为它的大小可以动态增加。因此,在您的情况下,需要循环第二列,检查单元格中的值,并将它们添加到两个可用集合之一:

Option Explicit

Sub TestMe()

    Dim withMiss        As New Collection
    Dim withoutMiss     As New Collection
    Dim currentRow      As Long

    currentRow = 2
    With Worksheets(1)
        Do Until .Cells(currentRow, 1) = vbNullString
            If UCase(.Cells(currentRow, 1)) = "MISS" Then
                withMiss.Add currentRow
            Else
                withoutMiss.Add currentRow
            End If
        currentRow = currentRow + 1
        Loop
    End With

    Dim cnt As Long

    Debug.Print "Rows with MISS"
    For cnt = 1 To withMiss.Count
        Debug.Print withMiss.item(cnt)
    Next cnt

    Debug.Print "Rows without MISS"
    For cnt = 1 To withoutMiss.Count
        Debug.Print withoutMiss.item(cnt)
    Next cnt

End Sub
如果1的2、3、4和8单元格中的。列中有“未命中”一词:


如果需要向集合动态添加值,VBA会提供类型
集合
,这比
数组
更好,因为它的大小可以动态增加。因此,在您的情况下,需要循环第二列,检查单元格中的值,并将它们添加到两个可用集合之一:

Option Explicit

Sub TestMe()

    Dim withMiss        As New Collection
    Dim withoutMiss     As New Collection
    Dim currentRow      As Long

    currentRow = 2
    With Worksheets(1)
        Do Until .Cells(currentRow, 1) = vbNullString
            If UCase(.Cells(currentRow, 1)) = "MISS" Then
                withMiss.Add currentRow
            Else
                withoutMiss.Add currentRow
            End If
        currentRow = currentRow + 1
        Loop
    End With

    Dim cnt As Long

    Debug.Print "Rows with MISS"
    For cnt = 1 To withMiss.Count
        Debug.Print withMiss.item(cnt)
    Next cnt

    Debug.Print "Rows without MISS"
    For cnt = 1 To withoutMiss.Count
        Debug.Print withoutMiss.item(cnt)
    Next cnt

End Sub
如果1的2、3、4和8单元格中的。列中有“未命中”一词:


你的问题不清楚。是否希望有两个数组,一个包含“未命中”行,另一个包含其余行?是的,我希望为它找到的“未命中”行创建一个数组,但为数组中尚未存在的所有其他行创建另一个数组,也可能包含“未命中”或“命中”@brainac@J.Smith您可能需要添加一些数据和期望输出的示例。您的问题不清楚。是否希望有两个数组,一个包含“未命中”行,另一个包含其余行?是的,我希望为它找到的“未命中”行创建一个数组,但另一个数组则为尚未包含在数组中的所有其他行创建一个数组,其中可能包含“未命中”或“命中”@brainac@J.Smith您可能需要添加一些数据和所需输出的示例