VBA Find Value in Column Delete all remaining Rows

Drrellik

Well-known Member
Joined
Apr 29, 2013
Messages
834
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

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
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,216,225
Messages
6,129,604
Members
449,520
Latest member
TBFrieds

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