需要一个脚本的Excel,删除行根据单词列表

yr9zkbsy  于 2022-12-14  发布在  其他
关注(0)|答案(2)|浏览(116)

我有一个在第2表的A列的单词列表,我需要一个脚本的excel,做以下:
1.检查第二张工作表中A列的第一个单词,然后按包含该单词的单元格筛选第一张工作表中的B列。
1.删除列C中不包含该单词的所有已筛选行。
然后它从A列的第二个工作表列表中迭代下一个单词,直到遍历所有单词。
示例:
A列第2页:有
B栏第1页:具有
第1页C栏:必须
在这种情况下,它将删除所有行,因为尽管B列包含“hav”,但C列不包含。

wswtfjt7

wswtfjt71#

您的描述很差,但我试了一下。在“Sheet1”上试试这段代码。用第二个工作表的名称替换“Sheet2”。这将检查“Sheet2”上的每个工作,并删除Sheet1上B列包含该单词的所有行。不确定C列是什么意思,但该条件应该很容易添加。
告诉我。

Sub Test()

    Dim LastRow As Long
    Dim LastRowS2 As Long
    Dim Word As String

    LastRowS2 = ThisWorkbook.Sheets("Sheet2").Cells(1, 1).End(xlDown).Row
    LastRow = Cells(1, 1).End(xlDown).Row

    For i = 2 To LastRowS2
        For j = 2 To LastRow
            Word = Split(ThisWorkbook.Sheets("Sheet2").Cells(i, "A").Text, " ")(0)
            If InStr(Cells(j, "B").Text, Word) > 0 Then
                If InStr(Cells(j, "C").Text, Word) > 0 Then
                    'Do nothing
                Else
                    Cells(j, "B").EntireRow.Delete
                    j = j - 1
                End If
            End If
        Next j
    Next i

End Sub
s4n0splo

s4n0splo2#

按条件删除列

链接

Workbook Download
代码

Sub DeleteColumnCriteria()

    ' Worksheet 1
    Const csheet1 As Variant = "Sheet1"   ' Worksheet Name/Index
    Const cFirstR1 As Long = 2            ' First Row
    Const cCol1 As Variant = "B"          ' Criteria Column 1
    Const cCol2 As Variant = "C"          ' Criteria COlumn 2
    ' Worksheet 2
    Const cSheet2 As Variant = "Sheet2"   ' Worksheet Name/Index
    Const cFirstR2 As Long = 2            ' First Row
    Const cCol As Variant = "A"           ' Criteria Column

    ' Worksheet 1
    Dim rngU As Range     ' Union Range
    Dim LastR1 As Long    ' Last Row Number
    Dim i As Long         ' Row Counter
    ' Worksheet 2
    Dim ws2 As Worksheet  ' Worksheet 2
    Dim LastR2 As Long    ' Last Row Number
    Dim j As Long         ' Row Counter

    Application.ScreenUpdating = False

    ' Calculate Last Row of Worksheet 2.
    Set ws2 = ThisWorkbook.Worksheets(cSheet2)
    LastR2 = ws2.Cells(ws2.Rows.Count, cCol).End(xlUp).Row

    With ThisWorkbook.Worksheets(csheet1)

        ' Calculate Last Row of Worksheet 1.
        LastR1 = .Cells(.Rows.Count, cCol1).End(xlUp).Row

        ' Accumulate ranges into Union Range.
        For i = cFirstR2 To LastR2 ' Loop through rows in Worksheet 2.
            For j = cFirstR1 To LastR1  ' Loop through rows in Worksheet 1.
                ' When value in cCol in Worksheet 2 is equal to cCol1 and
                ' not in cCol2 in Worksheet 1.
                If ws2.Cells(i, cCol) <> "" Then
                    If ws2.Cells(i, cCol) = .Cells(j, cCol1) _
                            And ws2.Cells(i, cCol) <> .Cells(j, cCol2) Then
                        If Not rngU Is Nothing Then   ' All other times.
                            Set rngU = Union(rngU, .Cells(j, 1))
                          Else                        ' First time only.
                            Set rngU = .Cells(j, 1)
                        End If
                    End If
                End If
            Next
        Next

    End With

    ' Delete rows in one go.
    If Not rngU Is Nothing Then
        rngU.EntireRow.Delete ' Hidden = True
    End If

    Application.ScreenUpdating = True

End Sub

相关问题