代码之家  ›  专栏  ›  技术社区  ›  peetman

查找所选单元格上方的第一个非空行

vba
  •  0
  • peetman  · 技术社区  · 7 年前

    Machine 1 在工作表中 Grupos Produção 我想退票 ******* Grupo 1 *******

    ********** Grupo  1  ********** 
        Machine 1
        Machine 2
    

    到目前为止,我有以下几点,但它没有返回我所需要的。

        Dim FindString As String
        Dim Rng As Range
        FindString = Lcell.Value
        If Trim(FindString) <> "" Then
            With Sheets("Grupos Produção").Range("A:Z")
                Set Rng = .Find(What:=FindString, _
                                After:=.Cells(.Cells.Count), _
                                LookIn:=xlValues, _
                                LookAt:=xlWhole, _
                                SearchOrder:=xlByRows, _
                                SearchDirection:=xlNext, _
                                MatchCase:=False)
                If Not Rng Is Nothing Then
                    Application.Goto Rng, True
                    upperRow = .Cells(Rng.Row, Rng.Column - 1).End(xlDown).Row
                Else
                    MsgBox "Nothing found"
                End If
            End With
        End If
    
    3 回复  |  直到 7 年前
        1
  •  1
  •   Sam    7 年前

    我不确定我是否理解您的代码,但我认为您正在寻找这样的代码:

    ...
    ...
    If Not Rng Is Nothing Then
        Do While Rng.Row > 1 And Rng.Offset(-1, 0).Value <> ""
            Set Rng = Rng.Offset(-1, 0)
        Loop
    ...
    

    一旦找到了单元格,它就会一路向上,直到找到一个空单元格,并在找到之前停止。

        2
  •  0
  •   Darren Bartrup-Cook    7 年前

    这将在所选单元格上方找到第一个非空单元格(表示该行不是空的)。

    您需要检查:

    • 如果所选内容上方的所有单元格都为空,则它将从工作表的底部开始,直到再次到达所选内容。

    正如你所说的第一个非空白单元格 * 通配符进行搜索,并使其从使用的选择中查找 xlPrevious .

    rng 如果整张纸都是空的,就什么都没有了。

    Sub Test()
    
        Dim Rng As Range
    
        With ThisWorkbook.Worksheets("Sheet1").Range("A:Z")
            Set Rng = .Cells.Find(What:="*", _
                                  After:=Selection, _
                                  LookIn:=xlValues, _
                                  LookAt:=xlWhole, _
                                  SearchOrder:=xlByRows, _
                                  SearchDirection:=xlPrevious)
    
            If Not Rng Is Nothing Then
                If Rng.Address = Selection.Address Or Rng.Row > Selection.Row Then
                    MsgBox "Nothing found"
                Else
                    Rng.Select
                End If
            End If
    
        End With
    
    End Sub
    
        3
  •  0
  •   peetman    7 年前

    根据山姆的回答,我设法得到了我需要的东西

        If Not Rng Is Nothing Then
                        Do While Rng.Row > 1 And Rng.Offset(-1, 0).Value <> ""
                            Set Rng = Rng.Offset(-1, 0)
                        Loop
                        grupo = Rng.Offset(-1, -1).Value
                        Lcell.Value = grupo
                    End If