1

如何使用 col S 中每个单元格的值来提供变量“CatchPhrase”...?我需要从 col S 中的每个单元格中选择包含值的所有行。

问题是 col S 有 1996 个不同的数字,而 col A 有 628790 个数字。

Sub SelectManyRows()
Dim CatchPhrase As String
Dim WholeRange As String
Dim AnyCell As Object
Dim RowsToSelect As String

CatchPhrase = "10044"

'first undo any current highlighting
Selection.SpecialCells(xlCellTypeLastCell).Select
WholeRange = "A1:" & ActiveCell.Address
Range(WholeRange).Select
On Error Resume Next ' ignore errors

For Each AnyCell In Selection
 If InStr(UCase$(AnyCell.Text), UCase$(CatchPhrase)) Then
    If RowsToSelect <> "" Then
        RowsToSelect = RowsToSelect & "," ' add group separator
    End If
    RowsToSelect = RowsToSelect & Trim$(Str$(AnyCell.Row)) & ":" &  Trim$(Str$(AnyCell.Row))
 End If
Next 

On Error GoTo 0 ' clear error 'trap'
Range(RowsToSelect).Select
End Sub

我需要的示例: 在此处输入图像描述

4

2 回答 2

1

使用与是否可以在不循环的情况下用符合特定条件的行号填充数组的方法?

您可以从column A(我在此示例中使用)返回与以下A1:A200列表匹配的数字数组S1:S9

Sub GetEm()
Dim x
x = Filter(Application.Transpose(Application.Evaluate("=if(NOT(ISERROR(MATCH(A1:A200,$S$1:S9,0))),a1:a200,""x"")")), "x", False)
End Sub

第二个子直接选择这些单元格

Sub GetEm2()
Dim x1
x1 = Join(Filter(Application.Transpose(Application.Evaluate("=if(NOT(ISERROR(MATCH(A1:A200,$S$1:S9,0))),""a""&row(a1:a200),""x"")")), "x", False), ",")
Application.Goto Range(x1)
End Sub
于 2013-10-03T11:25:55.330 回答
0

考虑:

Sub dural()
    Dim rS As Range, wf As WorksheetFunction
    Dim N As Long, aryS As Variant, rSelect As Range
    Dim i As Long, v As Variant
    '
    '     Make an array from column S
    '
    N = Cells(Rows.Count, "S").End(xlUp).Row
    Set wf = Application.WorksheetFunction
    Set rS = Range("S1:S" & N)
    aryS = wf.Transpose(rS)
    '
    '     Loop down column A looking for matches
    '
    Set rSelect = Nothing
    N = Cells(Rows.Count, "A").End(xlUp).Row
    For i = 1 To N
        v = Cells(i, 1).Value
        If v = Filter(aryS, v)(0) Then
            If rSelect Is Nothing Then
                Set rSelect = Cells(i, 1)
            Else
                Set rSelect = Union(Cells(i, 1), rSelect)
            End If
        End If
    Next i
    '
    '     Select matching parts of column A
    '
    rSelect.Select
End Sub
于 2013-10-03T11:29:00.117 回答