Removing row if there is not a specific text in columns

bloomingflower

New Member
Joined
Mar 9, 2020
Messages
19
Office Version
  1. 2019
Platform
  1. Windows
Hi,

Please help me make a code that will be asking for name and then if this name is not present from A - H column, macro should remove this row. I have such code, but it is not working. I think that there is a problem with selecting range, but amount of rows differs, so I decided to choose the whole column.
VBA Code:
Sub Removerows()
Application.ScreenUpdating = False
Name = InputBox("Enter name")

Dim xRow As Range
    Dim rng As Range
    Dim WorkRng As Range
    On Error Resume Next
    Set WorkRng = Range("A:H").Select
    Application.ScreenUpdating = False
    For i = WorkRng.Rows.Count To 1 Step -1
        Set xRow = WorkRng.Rows(i)
        Set rng = xRow.Find(Name, LookIn:=xlValues)
        If rng Is Nothing Then
            xRow.Delete
        End If
    Next
Application.ScreenUpdating = True

End Sub
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Test the code below on a copy of your sheet as you are deleting data.

VBA Code:
Sub Removerows()
    Dim myName As String, i As Long
    Application.ScreenUpdating = False
 
    myName = InputBox("Enter name")

 
    For i = Columns("A:H").Find("*", , xlValues, , xlByRows, xlPrevious).Row To 1 Step -1
        If IsError(Application.Match(myName, Intersect(Rows(i), Columns("A:H")), 0)) Then Rows(i).Delete
    Next

    Application.ScreenUpdating = True

End Sub
 
Last edited:
Upvote 0
Test the code below on a copy of your sheet as you are deleting data.

VBA Code:
Sub Removerows()
    Dim myName As String, i As Long
    Application.ScreenUpdating = False

    myName = InputBox("Enter name")


    For i = Columns("A:H").Find("*", , xlValues, , xlByRows, xlPrevious).Row To 1 Step -1
        If IsError(Application.Match(myName, Intersect(Rows(i), Columns("A:H")), 0)) Then Rows(i).Delete
    Next

    Application.ScreenUpdating = True

End Sub
Thank you, it works great, but it is removing headers as well, could you please tell me how to change it so it takes all rows from 2?
 
Upvote 0
Rich (BB code):
For i = Columns("A:H").Find("*", , xlValues, , xlByRows, xlPrevious).Row To 2 Step -1

Only reason it was 1 in the code is that is what you had it set as in post #1 ;) so I had to assume that you had no headers as it wasn't stated otherwise.
 
Upvote 0
Rich (BB code):
For i = Columns("A:H").Find("*", , xlValues, , xlByRows, xlPrevious).Row To 2 Step -1

Only reason it was 1 in the code is that is what you had it set as in post #1 ;)
Thank you very much, it works perfectly :love:
 
Upvote 0

Forum statistics

Threads
1,214,641
Messages
6,120,691
Members
448,978
Latest member
rrauni

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top