Delete entire rows with negative numbers in Amount Column - VBA

robw824

New Member
Joined
Jan 11, 2024
Messages
2
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hi. My fairly large spreadsheet has 23 columns and can have any number of rows (current one 969). I want to use VBA to delete the entire row for every row that contains a negative number in column K ($ amount)
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Hi, would this help you ?

It checks for the last row of data in Column A (eg. Col. 1 or "A" MUST have data in each cell, no blanks, else there is room for error (eg it could miss the last few rows..)

VBA Code:
Sub delete_row()

Dim datalastrow, x As Long

datalastrow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row ' find the last row of data in colA

For x = datalastrow To 1 Step -1

    If ActiveSheet.Range(Cells(x, 11), Cells(x, 11)) < 0 Then  'K equates to Col 11 here
      
        Range(Cells(x, 11), Cells(x, 11)).EntireRow.Delete

    End If

Next x

End Sub
 
Upvote 0
Hi.
There are 5 different data types, RV, DA, DZ, RR, RT (in column L) I only want to delete the rows with negative numbers with the type RV.
I had the sheet filtered by type RV. but when running your script it deleted all negative rows in all types. I thought it would only delete the rows displayed.
Is there a way to change it so that it only deletes the negative rows with neg amounts in column K and "RV" in Column L?
AccountCompanyLineDocumentAssignmentDateDueTerms
Arrears
C/OAmountType
38308​
1130​
1​
910026792​
910026792​
12/19/202312/19/2023A00320
0​
(35,789.60)RV
38308​
1130​
1​
98708995​
98708995​
3/28/20223/28/2022A003651
0​
-2381.40RR
38308​
1130​
1​
90018113​
90018113​
11/8/202311/8/2023A00361
0​
(32,523.40)RV
38308​
1130​
1​
910057714​
910057714​
1/4/20241/4/2024A0034
0​
(17,225.65)RV
38308​
1130​
1​
99379853​
99379853​
1/17/20231/17/2023A003356
0​
(12,962.29)RV
38308​
1130​
3​
1400565408​
240​
12/1/20221/1/2023
0​
372
0​
-26.24DZ
 
Upvote 0
Hi, just add the AND portion to the code as such :

VBA Code:
Sub delete_row()

Dim datalastrow, x As Long

datalastrow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row ' find the last row of data in colA

For x = datalastrow To 1 Step -1

    If ActiveSheet.Range(Cells(x, 11), Cells(x, 11)) < 0 AND ActiveSheet.Range(Cells(x, 12), Cells(x, 12)) = "RV" Then  'K equates to Col 11 here
      
        Range(Cells(x, 11), Cells(x, 11)).EntireRow.Delete

    End If

Next x

End Sub
 
Upvote 0
Rather than cycling through about 1,000 rows individually, what about deleting all the relevant ones at once?

VBA Code:
Sub Del_rows_v1()
  Application.ScreenUpdating = False
  If ActiveSheet.FilterMode Then ActiveSheet.ShowAllData
  With Range("A1").CurrentRegion
    .AutoFilter Field:=12, Criteria1:="RV"
    .AutoFilter Field:=11, Criteria1:="<0"
    If .Columns(1).SpecialCells(xlVisible).Count > 1 Then .Offset(1).Resize(.Rows.Count - 1).EntireRow.Delete
    .AutoFilter Field:=11
    .AutoFilter Field:=12
  End With
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution
Peter,

that definately works .. (I had no doubt :)) .. but I guess I'm struggling to understand actually this terminology a little, and what its doing after you've filtered your data. Can you elaborate a little as this method seems far more efficient ?

VBA Code:
 If .Columns(1).SpecialCells(xlVisible).Count > 1 Then .Offset(1).Resize(.Rows.Count - 1).EntireRow.Delete
 
Upvote 0
Rich (BB code):
If .Columns(1).SpecialCells(xlVisible).Count > 1 Then .Offset(1).Resize(.Rows.Count - 1).EntireRow.Delete

After the filter you will have the headings and probably some other rows of data. If there happened to be no rows of data visible after the filters are applied and you omitted the red part above, then the code would delete all the data below the headings even though it is in hidden rows. So the red part is checking to make sure there is at least one row of visible data to delete.

If you want to check, remove the red part of the code and test on some data that has no negative RV rows
 
Upvote 0

Forum statistics

Threads
1,215,069
Messages
6,122,956
Members
449,096
Latest member
Anshu121

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