簡體   English   中英

根據單元格值刪除行

[英]Delete rows with based on cell value

我正在嘗試在Sheet2中的A列中搜索Sheet1中的A1值。

如果存在,我想刪除Sheet2中的整個行。

如果不存在,請打開該消息框。


這是我所擁有的,但是我在實際刪除該行時很掙扎:

Sub Delete_Rows()
Dim FindString As String
Dim Rng As Range
FindString = Sheets("Sheet1").Range("A1")
If Trim(FindString) <> "" Then
    With Sheets("Sheet2").Range("A:A")
        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
            'I can't figure out how to delete the row
        Else
            MsgBox "Not Found"
        End If
    End With
End If
End Sub

這是代碼:

  1. 遍歷Sheet1的A列中的所有值,
  2. 在工作表2的A列中查找所有匹配項(使用FindNext方法)
  3. 並刪除匹配的行

試試看 :

Sub test_user5472539()
Dim Ws1 As Worksheet, _
    Ws2 As Worksheet, _
    LastRow As Long, _
    FindString As String, _
    FirstAddress As String, _
    cF As Range

Set Ws1 = ActiveWorkbook.Sheets("Sheet1")
Set Ws2 = ActiveWorkbook.Sheets("Sheet2")
LastRow = Ws1.Range("A" & Ws1.Rows.Count).End(xlUp).Row

For i = 1 To LastRow
    FindString = Ws1.Range("A" & i)
    If Trim(FindString) <> "" Then
        Ws2.Range("A1").Activate
        With Ws2.Range("A:A")
            'First, define properly the Find method
            Set cF = .Find(What:=FindString, _
                        After:=.Cells(1, 1), _
                        LookIn:=xlValues, _
                        LookAt:=xlWhole, _
                        SearchOrder:=xlByRows, _
                        SearchDirection:=xlNext, _
                        MatchCase:=False, _
                        SearchFormat:=False)

            'If there is a result, keep looking with FindNext method
            If Not cF Is Nothing Then
                FirstAddress = cF.Address
                Do
                    cF.EntireRow.Delete
                    Set cF = .FindNext(cF)
                'Look until you find again the first result
                Loop While Not cF Is Nothing And cF.Address <> FirstAddress
            Else
                MsgBox "Not Found"
            End If
        End With
    Else
    End If
Next i

End Sub

這是基於的示例

您不需要循環。 您可以使用.Autofilter比循環更快。

Sub Sample()
    Dim ws1 As Worksheet, ws2 As Worksheet
    Dim delRange As Range
    Dim lRow As Long
    Dim strSearch As String

    Set ws1 = Sheet1: Set ws2 = Sheet2

    strSearch = ws1.Range("A1").Value

    With ws2
        '~~> Remove any filters
        .AutoFilterMode = False

        lRow = .Range("A" & .Rows.Count).End(xlUp).Row

        With .Range("A1:A" & lRow)
            .AutoFilter Field:=1, Criteria1:="=" & strSearch
            Set delRange = .Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow
        End With

        '~~> Remove any filters
        .AutoFilterMode = False
    End With

    If delRange Is Nothing Then
       MsgBox "Not Found"
    Else
        delRange.Delete
    End If
End Sub

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM