Macro - end of range is unknown (varies)

gomes123

New Member
Joined
Jun 16, 2021
Messages
19
Office Version
  1. 2007
Platform
  1. Windows
I have an excel macro to filter and delete rows based on some criteria. It works, but sometimes the last row changes.

So I've just put it as 9999, but sometimes the last row could be more than 9999. Is there a way to improve the code? Thanks!


VBA Code:
Sub Macro3()
With Worksheets("Sheet1")
     .Range("M1:M9999").AutoFilter 1, "<100000"
     .Range("M2:M9999").SpecialCells(xlCellTypeVisible).EntireRow.Delete
     .AutoFilterMode = False
     
          .Range("J1:J9999").AutoFilter 1, "<30"
     .Range("J2:J9999").SpecialCells(xlCellTypeVisible).EntireRow.Delete
     .AutoFilterMode = False
     
     
               .Range("c1:C9999").AutoFilter 1, "<=0.3"
     .Range("C2:C9999").SpecialCells(xlCellTypeVisible).EntireRow.Delete
     .AutoFilterMode = False
     
End With
End Sub
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Try the following on a copy of your data (assumes the figures in columns C, J and M are numbers & not text)

VBA Code:
Option Explicit
Sub Delete_Rows_Multi_Criteria()
    Application.ScreenUpdating = False
    Dim ws As Worksheet
    Set ws = Worksheets("Sheet1")
    Dim LRow As Long, LCol As Long, i As Long, a, b
    LRow = ws.Cells.Find("*", , xlFormulas, , xlByRows, xlPrevious).Row
    LCol = ws.Cells.Find("*", , xlFormulas, , xlByColumns, xlPrevious).Column + 1
    
    a = Range(ws.Cells(2, 3), ws.Cells(LRow, 13))
    ReDim b(1 To UBound(a), 1 To 1)
    For i = 1 To UBound(a)
        If a(i, 1) <= 0.3 Or a(i, 8) < 30 Or a(i, 11) < 100000 Then b(i, 1) = 1
    Next i
    
    ws.Cells(2, LCol).Resize(UBound(a)) = b
    i = WorksheetFunction.Sum(ws.Columns(LCol))
    If i > 0 Then
        ws.Range(ws.Cells(2, 1), ws.Cells(LRow, LCol)).Sort Key1:=ws.Cells(2, LCol), _
        order1:=xlAscending, Header:=xlNo
        ws.Cells(2, LCol).Resize(i).EntireRow.Delete
    End If
    Application.ScreenUpdating = True
End Sub
 
Upvote 1
Solution
Try the following on a copy of your data (assumes the figures in columns C, J and M are numbers & not text)

VBA Code:
Option Explicit
Sub Delete_Rows_Multi_Criteria()
    Application.ScreenUpdating = False
    Dim ws As Worksheet
    Set ws = Worksheets("Sheet1")
    Dim LRow As Long, LCol As Long, i As Long, a, b
    LRow = ws.Cells.Find("*", , xlFormulas, , xlByRows, xlPrevious).Row
    LCol = ws.Cells.Find("*", , xlFormulas, , xlByColumns, xlPrevious).Column + 1
   
    a = Range(ws.Cells(2, 3), ws.Cells(LRow, 13))
    ReDim b(1 To UBound(a), 1 To 1)
    For i = 1 To UBound(a)
        If a(i, 1) <= 0.3 Or a(i, 8) < 30 Or a(i, 11) < 100000 Then b(i, 1) = 1
    Next i
   
    ws.Cells(2, LCol).Resize(UBound(a)) = b
    i = WorksheetFunction.Sum(ws.Columns(LCol))
    If i > 0 Then
        ws.Range(ws.Cells(2, 1), ws.Cells(LRow, LCol)).Sort Key1:=ws.Cells(2, LCol), _
        order1:=xlAscending, Header:=xlNo
        ws.Cells(2, LCol).Resize(i).EntireRow.Delete
    End If
    Application.ScreenUpdating = True
End Sub
Thanks! This works, most appreciated!
 
Upvote 0

Forum statistics

Threads
1,215,481
Messages
6,125,057
Members
449,206
Latest member
Healthydogs

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