use .autofilter & delete to remove a section of data and not the whole row issue

sous2817

Well-known Member
Joined
Feb 22, 2008
Messages
2,276
Hello everyone,

Currently I'm using:

Code:
    lr = .Range("C" & Rows.Count).End(xlUp).Row
    If lr = 1 Then lr = 2
    Set rng = .Range("A1:D" & lr)
        .Range("B2:B" & lr).Formula = "=IF(A2="""",VLOOKUP(C2,$G$2:$H$" & lr2 & ",2,0),VLOOKUP(A2,$G$2:$H$" & lr2 & ",2,0))"
        .Range("B2:B" & lr).Calculate
        .Range("B2:B" & lr).Value = .Range("B2:B" & lr).Value
    With rng
        .AutoFilter Field:=2, Criteria1:="#N/A"
        'assume the first row had headers
        On Error Resume Next
        .Offset(1, 0).Resize(.Rows.Count - 1, 1). _
            SpecialCells(xlCellTypeVisible).Delete Shift:=xlUp
        On Error GoTo 0
        
        'remove the autofilter
        .AutoFilter
    End With

to delete all of the resulting "#N/A"s from my formula. It works fine except it deletes the entire row and not just the area between .Range("A1:D" & lr). Am I missing something, or can someone help me tweak the code so that it only deletes the within the area and not the whole row.

Thanks in advance!
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
probably because I recycled some code and didn't notice. but even when I take it out:

Code:
.Offset(1, 0).SpecialCells(xlCellTypeVisible).Delete Shift:=xlUp

the whole row is still deleted. I just need A:D deleted. I could do it w/ a loop, but I'm trying to avoid that.
 
Upvote 0
You can only delete entire rows in an AutoFilter range. Try it manually and you will see - Excel will prompt 'Delete entire row?'.
 
Upvote 0
ah...well there you go. Thanks for the tip! I'll keep that in mind moving forward.
 
Upvote 0

Forum statistics

Threads
1,214,827
Messages
6,121,803
Members
449,048
Latest member
greyangel23

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