Excel vba to Find Cell and Delete Rows

miniminh02

New Member
Joined
Sep 14, 2011
Messages
3
I have a value in "D8" and another value in "G8".

I'd like to have a macro that would find the value in "D8" in column B starting with "B13". Once it finds the value I'd like it to delete all rows above that value up to row "B13".

Then I'd like it to find the value in "G8" in column B starting with "B13" and delete all the rows below that value down to "B100".

Is that possible? I'm sorry, I am completely lost :confused:.

Thanks so much for your help :stickouttounge:.

Minh

...or instead of having values in D8 and G8, a pop up that asks for values to look for would be nice too.
 
Last edited:

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Code:
Sub Delete_Above_and_Below()

    Dim Found As Range
    
    Application.ScreenUpdating = False
    
    If Range("D8").Value <> "" Then
        Set Found = Range("B13:B100").Find( _
                          What:=Range("D8").Value, _
                          LookIn:=xlValues, _
                          LookAt:=xlWhole, _
                          SearchOrder:=xlByRows, _
                          SearchDirection:=xlNext, _
                          MatchCase:=False)
        If Not Found Is Nothing Then Range("B13", Found).Delete Shift:=xlShiftUp
    End If
    
    If Range("G8").Value <> "" Then
        Set Found = Range("B13:B100").Find(What:=Range("D8").Value)
        If Not Found Is Nothing Then Range(Found, "B100").Delete Shift:=xlShiftUp
    End If
        
    Application.ScreenUpdating = True
    
End Sub
 
Last edited:
Upvote 0
This deletes the rows and not just the cells in column B and corrects a typo in the previous code.

Code:
Sub Delete_Above_and_Below()

    Dim Found As Range
    
    Application.ScreenUpdating = False
    
    If Range("D8").Value <> "" Then
        Set Found = Range("B13:B100").Find( _
                          What:=Range("D8").Value, _
                          LookIn:=xlValues, _
                          LookAt:=xlWhole, _
                          SearchOrder:=xlByRows, _
                          SearchDirection:=xlNext, _
                          MatchCase:=False)
        If Not Found Is Nothing Then Range("B13", Found)[COLOR="Red"].EntireRow[/COLOR].Delete
    End If
    
    If Range("G8").Value <> "" Then
        Set Found = Range("B13:B100").Find(What:=Range("[COLOR="Blue"]G8[/COLOR]").Value)
        If Not Found Is Nothing Then Range(Found, "B100")[COLOR="Red"].EntireRow[/COLOR].Delete
    End If
        
    Application.ScreenUpdating = True
    
End Sub
 
Upvote 0
Thank you for your help!! It worked!!

Now, is there a way to make it work with dates since the format is different?
 
Upvote 0
Thank you so much! That was the exactly the problem, I had the dates stored as text. You're wonderful!

Much thanks,
Minh
 
Upvote 0

Forum statistics

Threads
1,224,505
Messages
6,179,151
Members
452,891
Latest member
JUSTOUTOFMYREACH

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