【发布时间】:2019-08-07 22:17:32
【问题描述】:
我正在处理一个长长的 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
【问题讨论】:
-
使用逗号“,”范围运算符,您可以创建一个字符串并像这样使用它:
range("a1, a4, a6").Entirerow.Select -
如果您有超过 16383 行,使用
Integer类型是有问题的。建议将Integer的所有实例替换为Long,这是一个32 位整数值。Integer类型只有 16 位宽。 (见stackoverflow.com/a/31816532/380384)