VBA Find Value in Column Delete all remaining Rows

Drrellik

Well-known Member
Joined
Apr 29, 2013
Messages
842
Office Version
  1. 365
  2. 2016
  3. 2013
  4. 2011
  5. 2010
Platform
  1. Windows
<code class="vb plain">
Code:
' remove top rows down to row containing "Msn #"
    Dim rng As Range
    Dim X   As Long
    Const strstart As String = "Msn #"
    X = Cells(Rows.Count, 1).End(xlUp).Row
    
    Set rng = Cells(1, 1).Resize(X).Find(what:=strstart, LookIn:=xlValues, lookat:=xlWhole)
        
    If Not rng Is Nothing Then
        Cells(1, 1).Resize(rng.Row).EntireRow.delete
        Set rng = Nothing
    End If
    
' Remove rows below "Name" in column B


Insert your code here.... :)
:)

I have searched and cant seem to find the right syntax for deleting down after a value is located in column B I managed to search the forum and get the preceding rows done. Any help would be appreciated.

Don</code>
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Assuming we are looking for the value Andy in column B and we want to delete all the rows below Andy
Modify Andy to your needs.

Try this:
Code:
Sub Delete_Rows()
'Modified 3-30-18 3:05 AM EDT
Application.ScreenUpdating = False
Dim SearchString As String
Dim SearchRange As Range
SearchString = "Andy"
Dim Lastrow As Long
Lastrow = Cells(Rows.Count, "B").End(xlUp).Row
Dim r As Long
Set SearchRange = Range("B1:B" & Lastrow).Find(SearchString, LookIn:=xlValues, lookat:=xlWhole)
If SearchRange Is Nothing Then MsgBox SearchString & "   Not found": Exit Sub
r = SearchRange.Row
Rows(r + 1 & ":" & Lastrow).Delete
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Or in one go:
Code:
Sub Maybe()
    Range("A1:A" & Columns(1).Find("Msn #", , , 1).Offset(-1).Row).EntireRow.Delete
    Range("A" & Columns(1).Find("Name", , , 1).Offset(1).Row & ":A" & Cells(Rows.Count, 1).End(xlUp).Row).EntireRow.Delete
End Sub
 
Upvote 0
Thank you I will try both ways, the Marco is a group of 5 or 6 things I want done to the sheet before printing and I am sure my piece meal approach to coding makes it run slow.

Thanks ~DR
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,906
Members
452,366
Latest member
TePunaBloke

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