vba to delete rows based on a cell value

cjcass

Well-known Member
Joined
Oct 27, 2011
Messages
679
Office Version
  1. 2016
Platform
  1. Windows
Hi,

I'm looking for some vba that will delete all rows in Sheet1 that contain a value in Column CL that is less than the value in cell CG3. It needs to delete rows from CL9 down and the last row in CL is variable. Column CL may contain thousands of rows of data so I guess I'm after a 'non-looping' solution. The value in CG3 will be a percentage, so if it is set to, say 50%, then all rows that have a value in Column CL that is less than 50% will get deleted.

Any help much appreciated.
 

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.
Try:
VBA Code:
Option Explicit
Sub delete()
Dim lr&
Application.ScreenUpdating = False
ActiveSheet.AutoFilterMode = False
lr = Cells(Rows.Count, "CL").End(xlUp).Row
    With Range("CL8:CL" & lr)
        .AutoFilter field:=1, Criteria1:="<" & Range("CG3").Value
        .SpecialCells(xlCellTypeVisible).EntireRow.delete
    End With
ActiveSheet.AutoFilterMode = False
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution
try this:

VBA Code:
Option Explicit

Sub foo()
Application.ScreenUpdating = False
Dim i As Long, lr As Long
Dim r As Range
Set r = Range("CG3")
lr = Cells(Rows.Count, "CL").End(xlUp).Row
For i = lr To 9 Step -1
If Range("CL" & i) < r Then
Range("CL" & i).EntireRow.Delete
End If
Next i
Application.ScreenUpdating = True
MsgBox "completed"
End Sub
 
Upvote 0
Thank you both for your time and help with this. Both solutions worked fine.
That said, when targetting 15,000 rows the autofilter solution was faster.
Many thanks again :)
 
Upvote 0

Forum statistics

Threads
1,214,987
Messages
6,122,613
Members
449,090
Latest member
vivek chauhan

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