VBA Excel-“ .Find”从上至下搜索,以查找列K

时间:2018-08-07 16:37:30

标签: excel vba excel-vba

我的目标是在“ K”列中搜索特定值并返回特定结果。但是我面临的一个障碍是,在搜索该列时,它并没有从上到下进行排序,而是只是在“查找”中执行“添加”,即使它前面有“条件”。有没有办法使该列按单元格顺序读取?

Sub Find_Stuff()

Dim s As String
Dim rCell As Range
Dim lReply As Long
Dim firstaddress As String
Dim rngOriginal As Range
Dim Cell As Range
Dim n As Long


Set Cell = Columns("K:K").Find(What:="Add", LookIn:=xlValues, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If Not Cell Is Nothing Then
firstaddress = Cell.Address
Cell.Offset(0, -9).Resize(, 4).Insert shift:=xlDown
Cell.Offset(0, 1).Value = "add "
n = Range("K" & Rows.Count).End(xlUp).Row
Range("K9").AutoFill Destination:=Range("K9:K" & n), Type:=xlFillDefault
Cell.Select
Else
        Set Cell = Columns("K:K").Find(What:="Term", LookIn:=xlValues, _
        LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False)
        If Not Cell Is Nothing Then
        firstaddress = Cell.Address
        Cell.Offset(0, -4).Resize(, 4).Insert shift:=xlDown
        Cell.Offset(0, 1).Value = "term "
        n = Range("K" & Rows.Count).End(xlUp).Row
        Range("K9").AutoFill Destination:=Range("K9:K" & n), Type:=xlFillDefault
        Cell.Select
        Else
                Set Cell = Columns("K:K").Find(What:="Remove", LookIn:=xlValues, _
                LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
                MatchCase:=False, SearchFormat:=False)
                If Not Cell Is Nothing Then
                firstaddress = Cell.Address
                Cell.Offset(0, -4).Resize(, 4).Insert shift:=xlDown
                Cell.Offset(0, 1).Value = "Remove"
                n = Range("K" & Rows.Count).End(xlUp).Row
                Range("K9").AutoFill Destination:=Range("K9:K" & n), Type:=xlFillDefault
                Cell.Select
                Else
                        Columns("K:K").Select
                        Set Cell = Selection.Find(What:="New", After:=ActiveCell, LookIn:=xlValues, _
                        LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
                        MatchCase:=False, SearchFormat:=False)
                        If Not Cell Is Nothing Then
                        Cell.Offset(0, 1).Select
                        ActiveCell.FormulaR1C1 = _
                        "New"
                        Cell.Select

                        End If
                        On Error GoTo 0
                End If
                On Error GoTo 0
        End If
        On Error GoTo 0
End If
    On Error GoTo 0
End Sub

1 个答案:

答案 0 :(得分:0)

但是,我面临的一个障碍是,在搜索列时,它并没有从上到下进行排序,而是只是在“查找”中为“添加”执行,即使前面有“条件”。有没有办法使该列按单元格顺序读取?

这是.Find()的标准行为。这是MSDNAfter参数的说明:

  

要开始搜索的单元格。这对应于从用户界面进行搜索时活动单元格的位置。注意,After必须是该范围内的单个单元格。请记住,搜索是在此单元格之后开始的;直到方法回绕到该单元格之前,不搜索指定的单元格。如果不指定此参数,则搜索将在范围左上角的单元格之后开始。

要确保.Find()从第一个单元格开始搜索,您必须将最后一个单元格作为参数传递:

Sub TestMe()
    Dim myR     As Range
    Dim myS     As Range: Set myS = Range("B1:B5") 

    With myS
        Set myR = .Find(1)
        Debug.Print myR.Row
        Set myR = .Find(1, after:=.Cells(.Cells.Count))
        Debug.Print myR.Row
    End With
End Sub