Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/25.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/0/vba/15.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
如何使用vba从excel中包含行索引的数组中选择特定行_Excel_Vba - Fatal编程技术网

如何使用vba从excel中包含行索引的数组中选择特定行

如何使用vba从excel中包含行索引的数组中选择特定行,excel,vba,Excel,Vba,我正在使用excel中的一个长列表,我必须选择符合条件的特定行。我设法创建了一个包含这些行的索引号的数组。我现在要做的就是选择那些行 代码如下: Sub Playground() Dim currentContract As String currentContract = "none" Dim CCIsPos As Boolean CCIsPos = False Dim asarray() As Integer Dim i As Integer ReDim asarray(1 To She

我正在使用excel中的一个长列表,我必须选择符合条件的特定行。我设法创建了一个包含这些行的索引号的数组。我现在要做的就是选择那些行

代码如下:

Sub Playground()

Dim currentContract As String
currentContract = "none"
Dim CCIsPos As Boolean
CCIsPos = False
Dim asarray() As Integer
Dim i As Integer

ReDim asarray(1 To Sheets("Playground").UsedRange.Rows.Count)
For Each Cell In Sheets("Playground").Range("E:E")

    matchRow = Cell.Row

    If Cell.Value <> currentContract Then
        currentContract = Cell.Value
        If Cells(matchRow, "J") > 0 Then
            CCIsPos = True
        Else
            CCIsPos = False
        End If
       End If
If CCIsPos Then
    i = i + 1
    asarray(i) = matchRow
End If
Next Cell

'Would need a function here selecting rows from the array "asarray"
'Rows(asarray).Select doesn't work.
End Sub

我想说你需要利用联合函数。按如下方式修改代码,我假设您已经检查并确认asarray包含正确的行索引,我将不研究这些部分

Sub Playground()

Dim currentContract As String
currentContract = "none"
Dim CCIsPos As Boolean
CCIsPos = False
Dim i As Integer
Dim selectionRange as Range

For Each Cell In Sheets("Playground").Range("E:E")
    matchRow = Cell.Row
    If Cell.Value <> currentContract Then
        currentContract = Cell.Value
        If Cells(matchRow, "J") > 0 Then
            CCIsPos = True
        Else
            CCIsPos = False
        End If
    End If
    If CCIsPos Then
        If Not selectionRange Is Nothing Then
            Set selectionRange = Union(selectionRange, Sheets("Playground").Rows(matchRow).EntireRow)
        Else
            Set selectionRange = Sheets("Playground").Rows(matchRow).EntireRow
        End If
    End If
Next Cell

selectionRange.Select

End Sub

我希望这能解决您的问题。

如前所述,此代码在第一次尝试联合时总是会出错,因为selectionRange在当时将为空,并且您不能联合空范围。需要测试selectionRange是否为nothing,如果为nothing,只需将其设置为符合条件的范围,否则如图所示。您还需要确保在分配对象变量(如范围)时使用Set关键字,因此应设置selectionRange=…您是对的,这也表明我应该在发布答案之前测试我的答案:我已进行了相关编辑,感谢您的警告。使用逗号、范围运算符,您可以创建一个字符串并按如下方式使用:rangea1、a4、a6.Entirerow.Select如果您有16383行以上的行,则使用整数类型是一个问题。建议将Integer的所有实例替换为Long,Long是一个32位整数值。整数类型仅为16位宽。看见