Detele Rows in a range

Chandresh

Board Regular
Joined
Jul 21, 2009
Messages
146
Hi All,

Below macro is working fine and deleting the data till the last row in the excel , could you please help me if the same macro can delete ony the data in a range F6:F4009


Sub testdelcells()
Windows("Chan.xlsm").Activate
Sheets("Test").Select
With ActiveSheet
.AutoFilterMode = False
With Range("F6", Range("F" & Rows.Count).End(xlUp))
.AutoFilter 1, "*DUMMY*"
On Error Resume Next
.Offset(1).SpecialCells(12).EntireRow.delete
End With
.AutoFilterMode = False
End With
End Sub


Thanks
Chand
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
I think you just need to replace the word Delete with Clear. If you want to keep formula in cells, use ClearContents instead.
 
Upvote 0
Meaning if any row contains DUMMY, the entire row is deleted. Therefore the range probably reduced in number of rows, correct?

Is there anything else beyond F4009 that you do not want to delete?
 
Upvote 0
yes there are data beyond F4009 but we don't want to delete it (we will not have DUMMY after F4009, DUMMY will be only in the range F6:F4009)
 
Upvote 0
Then you must find out how many rows get deleted.
1) Get number of rows from F6 to the end
2) Perform DUMMY delete
3) Get the number of rows from F6 to the end
4) Redefine range F6:Fx
4) ClearContents in that new defined range

Your code

Sub testdelcells()
Windows("Chan.xlsm").Activate
Sheets("Test").Select
With ActiveSheet
.AutoFilterMode = False
With Range("F6", Range("F" & Rows.Count).End(xlUp))
.AutoFilter 1, "*DUMMY*"
On Error Resume Next
.Offset(1).SpecialCells(12).EntireRow.delete
End With
.AutoFilterMode = False
End With
End Sub

Looking back at your code above, looks like you filter data from F6 until end of all rows, not until F4009. Am I missing something here? Probably you do not have DUMMY after F4009.

Anyway, you can find total rows (n) by adding bold lines
With Range("F6", Range("F" & Rows.Count).End(xlUp))
m=.Rows.Count
.AutoFilter 1, "*DUMMY*"
On Error Resume Next
.Offset(1).SpecialCells(12).EntireRow.delete
.AutoFilterMode = False ' this line is relocated
n=.Rows.Count
End With
Range("F6", Range("F" & (m-n)).ClearContents
End Sub

Not test.
 
Upvote 0
@Chandresh, try

VBA Code:
Sub testdelcells()
    Windows("Chan.xlsm").Activate
    With Sheets("Test")
        .AutoFilterMode = False
        With .Range("F5:F4009")
            .AutoFilter 1, "*DUMMY*"
            On Error Resume Next
            .Offset(1).Resize(.Rows.Count - 1).SpecialCells(12).EntireRow.Delete
        End With
        .AutoFilterMode = False
    End With
End Sub

If you want to keep formula in cells, use ClearContents instead.
@Zot ClearContents removes the formula as well, it doesn't clear formatting.
 
Upvote 0
@Chandresh, try

VBA Code:
Sub testdelcells()
    Windows("Chan.xlsm").Activate
    With Sheets("Test")
        .AutoFilterMode = False
        With .Range("F5:F4009")
            .AutoFilter 1, "*DUMMY*"
            On Error Resume Next
            .Offset(1).Resize(.Rows.Count - 1).SpecialCells(12).EntireRow.Delete
        End With
        .AutoFilterMode = False
    End With
End Sub


@Zot ClearContents removes the formula as well, it doesn't clear formatting.
? My bad. Thanks for correcting.
 
Upvote 0

Forum statistics

Threads
1,215,050
Messages
6,122,868
Members
449,097
Latest member
dbomb1414

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