delete range of cells, given criteria

emantzoo

Board Regular
Joined
Nov 6, 2013
Messages
80
I want to use a macro to delete the range A:F in every row where the value in column E is equal to "del".

I came up with this, but it does not work:

Code:
For i = 1 To 35000
If Cells(i, 5) = "del" Then
    With Range(Cells(i, 1), Cells(i, 6)).Select
    
    Selection.Delete Shift:=xlUp
   
    End With
End If
Next i

So.. please help :biggrin:
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Try this macro. When deleting rows you should start from the bottom and work up.
Code:
Sub DelRange()
    Application.ScreenUpdating = False
    Dim i As Long
    Dim LastRow As Long
    LastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    For i = LastRow To 1 Step -1
        If Cells(i, 5) = "del" Then
            Range(Cells(i, 1), Cells(i, 6)).Delete Shift:=xlUp
        End If
    Next i
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
emantzoo,

If your worksheet does not have anything in it to the right of column F, then try the following.


Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

Code:
Option Explicit
Sub DeleteRows()
' hiker9, 01/24/2014, ME752836
Dim r As Long
For r = 3500 To 1 Step -1
  If Cells(r, 5) = "del" Then Rows(r).Delete
Next r
End Sub

Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension .xlsm

Then run the DeleteRows macro.
 
Upvote 0
emantzoo,

Sample raw data:


Excel 2007
ABCDEFG
1A1B1C1D1E1F1G1
2A2B2C2D2delF2G2
3A3B3C3D3E3F3G3
4A4B4C4D4delF4G4
5A5B5C5D5E5F5G5
6A6B6C6D6delF6G6
7A7B7C7D7E7F7G7
8A8B8C8D8delF8G8
9A9B9C9D9E9F9G9
10A10B10C10D10delF10G10
11A11B11C11D11E11F11G11
12A12B12C12D12delF12G12
13A13B13C13D13E13F13G13
14A14B14C14D14delF14G14
15A15B15C15D15E15F15G15
16A16B16C16D16delF16G16
17A17B17C17D17E17F17G17
18A18B18C18D18delF18G18
19A19B19C19D19E19F19G19
20A20B20C20D20delF20G20
21
Sheet1


After the new macro using two arrays in memory, and, no deleting any rows:


Excel 2007
ABCDEFG
1A1B1C1D1E1F1G1
2A3B3C3D3E3F3G2
3A5B5C5D5E5F5G3
4A7B7C7D7E7F7G4
5A9B9C9D9E9F9G5
6A11B11C11D11E11F11G6
7A13B13C13D13E13F13G7
8A15B15C15D15E15F15G8
9A17B17C17D17E17F17G9
10A19B19C19D19E19F19G10
11G11
12G12
13G13
14G14
15G15
16G16
17G17
18G18
19G19
20G20
21
Sheet1


Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

Code:
Option Explicit
Sub ReorgDataArrays()
' hiker9, 01/24/2014, ME752836
Dim a As Variant, o As Variant
Dim i As Long, ii As Long
Dim lr As Long, n As Long
lr = Cells.Find("*", , xlValues, xlWhole, xlByRows, xlPrevious, False).Row
a = Range("A1:F" & lr)
n = Application.CountIf(Columns(5), "del")
ReDim o(1 To UBound(a, 1) - n, 1 To UBound(a, 2))
For i = 1 To UBound(a, 1)
  If a(i, 5) <> "del" Then
    ii = ii + 1
    o(ii, 1) = a(i, 1): o(ii, 2) = a(i, 2): o(ii, 3) = a(i, 3)
    o(ii, 4) = a(i, 4): o(ii, 5) = a(i, 5): o(ii, 6) = a(i, 6)
  End If
Next i
Range("A1:F" & lr).ClearContents
Range("A1").Resize(UBound(o, 1), UBound(o, 2)) = o
End Sub

Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension .xlsm

Then run the ReorgDataArrays macro.
 
Upvote 0

Forum statistics

Threads
1,203,328
Messages
6,054,754
Members
444,748
Latest member
knowak87

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