简体   繁体   English

如何在Excel表格中搜索文本,然后删除整个行?

[英]How do I search a excel table for a text and then delete that entire row?

I need a macro that takes the inputed text from a cell and searches through the first column of a table for that text. 我需要一个宏,该宏从单元格中获取输入的文本,并在表的第一列中搜索该文本。 If it finds the text, it will delete that whole row but if it does not find that text then it shows a message box saying it was not found. 如果找到文本,它将删除整行,但是如果找不到该文本,则会显示一个消息框,指出未找到。 I have been looking through other similar questions but none of them search for text found in a cell. 我一直在寻找其他类似的问题,但没有一个搜索在单元格中找到的文本。 My code I have right now looks like this. 我现在拥有的代码如下所示。

Sub Remove1()  

Dim rng As Range 
Dim InputRng As Range 
Dim DeleteRng As Range 
Dim DeleteStr As String 

Set InputRng = Worksheets(1).Range("JobNumbers") 
DeleteStr = Worksheets(1).Range("Q8") 
For Each rng In InputRng     
    If rng.Value = DeleteStr Then        
        If DeleteRng Is Nothing Then             
            Set DeleteRng = rng         
        Else             
            Set DeleteRng = Application.Union(DeleteRng, rng)         
       End If     
    End If 
Next 
DeleteRng.EntireRow.delete  

End Sub 

When I try to run, it highlights the DeleteRng.EntireRow.delete row. 当我尝试运行时,它突出显示DeleteRng.EntireRow.delete行。

You want to check again if DeleteRng is Nothing before deleting rows: 您要在删除行之前再次检查DeleteRng是否为Nothing

If DeleteRng Is Nothing Then
    MsgBox "There's nothing to delete"
Else
    DeleteRng.EntireRow.delete
End If

If this won't help, you can try to add rows to DeleteRng : 如果这样做没有帮助,您可以尝试向DeleteRng添加行:

If DeleteRng Is Nothing Then
    Set DeleteRng = rng.EntireRow
Else
    Set DeleteRng = Application.Union(DeleteRng, rng.EntireRow)
End If

And at the end: 最后:

DeleteRng.Delete

When you delete the row, you have to loop through in the reverse direction. 删除行时,必须反向循环。

This example accepts the value to be searched, searches the value in Column A and deletes the entire row if found. 本示例接受要搜索的值,在A列中搜索该值,如果找到则删除整行。 If none of the value matches, then a message is shown. 如果没有一个值匹配,则显示一条消息。

Sub remove()
Dim i As Long, str As String, match As Boolean
str = InputBox("Enter the value to be searched")
match = False
For i = Cells(Rows.Count, 1).End(xlUp).Row To 1 Step -1
    If Cells(i, 1) = str Then
        Rows(i).Delete
        match = True
    End If
Next i
If match = False Then
    MsgBox "Value not found"
End If
End Sub

Alter this code as per your needs. 根据您的需要更改此代码。

This is how I do what you are asking about. 这就是我要做什么。

The code below is set up in my test worksheets as follows: 下面的代码在我的测试工作表中设置如下:

On Sheet1 in column "A" I have a list of names entered down the column just to have some values to work with. 在Sheet1的“ A”列中,我列出了在该列中输入的名称,以使用一些值。

On Sheet2 in column "A" I have a Header in cell "A1" and then in a value typed into cell "A2" that I want to compare to each cell in Sheet1, column "A" 在Sheet2的“ A”列中,我在单元格“ A1”中有一个页眉,然后在一个要键入到单元格“ A2”中的值中,我想与Sheet1的每个单元格的“ A”列进行比较

I placed a button on the worksheet. 我在工作表上放置了一个按钮。 When the button is clicked the code runs. 单击该按钮后,代码将运行。 When a match is found then the entire row is deleted. 找到匹配项后,整个行将被删除。 I have a message box that tells me the number of rows deleted. 我有一个消息框,告诉我删除的行数。

There are other ways to go about doing this. 还有其他方法可以执行此操作。 Maybe some of them are simpler than my technique. 也许其中一些比我的技术更简单。 You should be able to adapt my code to fit your needs, though. 不过,您应该能够修改我的代码以适合您的需求。 I hope this helps. 我希望这有帮助。

Option Explicit

Sub Button2_Click()
    'Declare Variables
    Dim lastRow As Long
    Dim counter_1 As Long
    Dim counter_2 As Long
    Dim enteredValue As String
    Dim checkCellSheet_2 As String
    Dim checkRangeSheet_1 As String
    Dim answer As Long
    Dim matchCount As Integer
    Dim strMatchCount As String

    'Instantiate Variables
    checkCellSheet_2 = Sheet2.Cells(2, 1).Value
    enteredValue = checkCellSheet_2
    checkRangeSheet_1 = Sheet1.Range("A" & Rows.Count).End(xlUp).Value
    lastRow = Sheet1.Range("A1").CurrentRegion.Rows.Count
    matchCount = 0

    'Look in cell "A2" on Sheet 2 and delete
    'rows when a match is found in column "A"
    'On Sheet 1

    For counter_1 = lastRow To 1 Step -1
        Worksheets("Sheet1").Activate
        If checkCellSheet_2 = checkRangeSheet_1 Then
            For counter_2 = lastRow To 1 Step -1
                If Cells(counter_2, 1) = enteredValue Then
                    Rows(counter_2).Delete
                    matchCount = matchCount + 1
                End If
            Next
        ElseIf checkCellSheet_2 <> checkRangeSheet_1 Then
         'Do Nothing
        End If
        checkRangeSheet_1 = Sheet1.Range("A" & counter_1).Value
    Next counter_1

    strMatchCount = CStr(matchCount)
    answer = MsgBox("There were " + strMatchCount + " rows on Sheet 1, " _
                    + "with matching values in Column A of Sheet 1, deleted.")
End Sub

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何删除 Excel 表中的一行,该表位于使用 VBA 的用户窗体搜索字段找到的另一张表上? - How do I delete a row in an Excel Table that is on another Sheet that was found with a Userform search field using VBA? Excel 宏/VBA - 如果一列中的单元格为空,如何删除整行? - Excel Macro / VBA - How do I delete an entire row if a cell in one column is blank? 如果特定列中的单元格有数据,如何告诉 Excel 宏删除整行? - How do I tell an excel macro to delete the entire row if cell in a specific column have data? 我如何获取python在CSV文件中搜索字典中的项目,然后打印出整个Excel行…谢谢 - How do I get python to search a csv file for items in a dictionary then print out the entire excel row…Thanks 如果行中的一个单元格包含错误,如何删除整行? - How do I delete an entire row if a cell in the row contains an error? 如何选择字符串的第一个实例并删除整行 - How do I select the first instance of a string and delete the entire row 仅当该行中有多个单元格为空时,才如何从Excel中的表中删除整行? (VBA) - How to delete an entire row from a table in excel only if multiple cells are empty in that row? (VBA) excel vba 如果单元格包含GREP搜索,则删除整行 - excel vba Delete entire row if cell contains the GREP search 如何在Excel中告诉单元格包含整个行的总和 - How do i tell a cell to contain the sum of a entire row in excel VBA Excel删除表行并上移,而不是整行 - VBA Excel Delete Table rows and shift up, not entire row
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM