Delete all negative Number in a range.

bennyys

New Member
Joined
Jun 22, 2017
Messages
17
Hi,
I am working on delete negative number in a range(in my case is in column J from row 19 to the last row with data).
as of now I use this kind of code

Code:
<code style="margin: 0px; padding: 0px; font-style: inherit; font-weight: inherit; line-height: 12px;">'delete negative data on col J
Dim LR As Long, i As Long
LR = Range("J" & Rows.Count).End(xlUp).row
For i = LR To 20 Step -1
'    If Range("J" & i).value < 0 Then Rows(i).Delete
Next i</code>


This code working fine just a little bit slow if the amount of data increase.

My question is, are there any fastest way to delete all negative number(including other data in the same row) other then the one I use above?

all your help will be greatly appreciated.

Regards,
Benny
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Benny, this should be much faster if you have large data. There was a small discrepancy between your description and your code about which row to start at. I have used row 20 in my code, thinking that perhaps row 19 may be a heading. Anyway, adjust if needed. See how it goes in a copy of your workbook & let us know if there are any problems.

Rich (BB code):
Sub DeleteNegatives()
  Dim a As Variant, b As Variant
  Dim nc As Long, i As Long, k As Long, lastrow As Long
  
  With Rows("20:" & Range("J" & Rows.Count).End(xlUp).Row)  '<- Check first row of actual data
    nc = .Find(What:="*", After:=.Cells(1, 1), LookIn:=xlValues, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious, SearchFormat:=False).Column + 1
    a = .Columns(10).Value  '<- Column 10 = Column "J"
    ReDim b(1 To UBound(a), 1 To 1)
    For i = 1 To UBound(a)
      If a(i, 1) < 0 Then
        b(i, 1) = 1
        k = k + 1
      End If
    Next i
    If k > 0 Then
      Application.ScreenUpdating = False
      .Columns(nc).Value = b
      .Sort Key1:=.Columns(nc), Order1:=xlAscending, Header:=xlNo, OrderCustom:=1, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal
      .Resize(k).EntireRow.Delete
      Application.ScreenUpdating = True
      End If
  End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,828
Messages
6,181,210
Members
453,023
Latest member
alabaz

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