Remove Useless Data by Rows

gripper

Board Regular
Joined
Oct 29, 2002
Messages
176
Hi Board,

I am looking for a VBA solution to remove data that is useless that does not need my attention. I need to remove this useless data by rows.

Here is the scenario:
I import a .csv file throughout my day. The first row is titles which should not be touched.
Rows 2 through x (1000's) of rows have sequential data that has to stay in order.

What I want help accomplishing is in Column "D", which is the most populated column and do the following.
Starting at row 2 (first row of data) look in Column "D" and go down the column until the first integer is found that is not "0". Then go back up 5 rows and delete everything from row 2 down to this row and get rid of all that unnecessary data. This useless data may be 20 rows or upto 1000 rows.

I am not sure how to anchor to column "D" and perform this cleaning.

Any help would be greatly appreciated.

Thank you
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Try this on a copy of your data
VBA Code:
Option Explicit
Sub gripper()
    Dim rng As Range, c As Range, i As Long
    Set rng = Range("D2", Cells(Rows.Count, "D").End(xlUp))
    
    For Each c In rng
        If IsNumeric(c) And c > 0 Then
            If CInt(c.Value) / c.Value = 1 Then
                i = c.Row
                If i > 7 Then
                    Range(Cells(2, 1), Cells(i - 6, 1)).EntireRow.Delete
                    Exit For
                End If
            End If
        End If
    Next c
End Sub
 
Upvote 0
Slight amendment
VBA Code:
Option Explicit
Sub gripper_2()
    Dim rng As Range, c As Range, i As Long
    Set rng = Range("D2", Cells(Rows.Count, "D").End(xlUp))
    
    For Each c In rng
        If IsNumeric(c) And c <> 0 Then
            If CInt(c.Value) / c.Value = 1 Then
                i = c.Row
                If i > 7 Then
                    Range(Cells(2, 1), Cells(i - 6, 1)).EntireRow.Delete
                    Exit For
                End If
            End If
        End If
    Next c
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,666
Messages
6,120,806
Members
448,990
Latest member
rohitsomani

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