Making it filter and not delete the lines

Romano_odK

Active Member
Joined
Jun 4, 2020
Messages
379
Office Version
  1. 365
Platform
  1. Windows
Good morning,

Got this code and by definition it works, but its really slow so I am looking for either another and fast solution or make it a filter only. So that last part, can this be done with this code, so to make it filter only?

Thank you for your time.

VBA Code:
Dim lr As Long
    Dim r As Long
    
    Application.ScreenUpdating = False
    
'   Find last row in column A with data
    lr = Cells(Rows.Count, "A").End(xlUp).Row
    
'   Loop through all rows backwards, up to row 6
    For r = lr To 6 Step -1
'       See if columns H, I, or J is empty
        If (Cells(r, "I") <> "AAP") Or (Cells(r, "J") = 0) Then
'           Delete row
            Rows(r).Delete
        End If
    Next r
    
    Application.ScreenUpdating = Tru
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Little bit of a guess without seeing any sample data, but give this a try with a copy of your data.

VBA Code:
Sub Del_Rows()
  Dim a As Variant, b As Variant
  Dim nc As Long, i As Long, k As Long
 
  nc = Cells.Find(What:="*", LookIn:=xlFormulas, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column + 1
  a = Range("I6:J" & Range("A" & Rows.Count).End(xlUp).Row).Value
  ReDim b(1 To UBound(a), 1 To 1)
  For i = 1 To UBound(a)
    If a(i, 1) <> "AAP" Or a(i, 2) = 0 Then
      b(i, 1) = 1
      k = k + 1
    End If
  Next i
  If k > 0 Then
    Application.ScreenUpdating = False
    With Range("A6").Resize(UBound(a), nc)
      .Columns(nc).Value = b
      .Sort Key1:=.Columns(nc), Order1:=xlAscending, Header:=xlNo
      .Resize(k).EntireRow.Delete
    End With
    Application.ScreenUpdating = True
  End If
End Sub
 
Upvote 0
Little bit of a guess without seeing any sample data, but give this a try with a copy of your data.

VBA Code:
Sub Del_Rows()
  Dim a As Variant, b As Variant
  Dim nc As Long, i As Long, k As Long
 
  nc = Cells.Find(What:="*", LookIn:=xlFormulas, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column + 1
  a = Range("I6:J" & Range("A" & Rows.Count).End(xlUp).Row).Value
  ReDim b(1 To UBound(a), 1 To 1)
  For i = 1 To UBound(a)
    If a(i, 1) <> "AAP" Or a(i, 2) = 0 Then
      b(i, 1) = 1
      k = k + 1
    End If
  Next i
  If k > 0 Then
    Application.ScreenUpdating = False
    With Range("A6").Resize(UBound(a), nc)
      .Columns(nc).Value = b
      .Sort Key1:=.Columns(nc), Order1:=xlAscending, Header:=xlNo
      .Resize(k).EntireRow.Delete
    End With
    Application.ScreenUpdating = True
  End If
End Sub
Unfortunately it crashes, so I added the top part of this sheet. Its more then 6000 lines so I hope this will do.

Locatie controle 3.34 beta - Copy.xlsm
ABCDEFGHIJKLM
1Datum2024/02/06
2Export folderc:\temp\Locatie-overslagregels.xml
3OmschrijvingLocatie overslagregels
4Dagboek90
5Regels1
6ArtikelcodeOmschrijvingOntvgstdatumAantalKostprijsVrrdrekStd_locatieMagazijnLocatieVoorraadvan Locatienaar LocatieVerplaats
7100000Vito Glaserfix 111 6x2 mm wit - 10x25 m11/07/2023961,003901P02B1P02B32
8100000Vito Glaserfix 111 6x2 mm wit - 10x25 m11/07/2023962,003901P02B1A000
9100001Vito Glaserfix 111 6x2 mm zwart - 10x25 m18/04/2023963,003901P04B1P04B-20
10100001Vito Glaserfix 111 6x2 mm zwart - 10x25 m18/04/20239641,003901P04B1A000
11100002Vito Glaserfix 111 6x3 mm wit - 10x25 m15/11/20239654,003901P02B1A000
12100002Vito Glaserfix 111 6x3 mm wit - 10x25 m15/11/2023965,003901P02B1AAP0
13100002Vito Glaserfix 111 6x3 mm wit - 10x25 m15/11/2023964,003901P02B1P02B-69
14100003Vito Glaserfix 111 6x3 mm zwart - 10x25 m08/01/2024964,003901P04B1A000
15100003Vito Glaserfix 111 6x3 mm zwart - 10x25 m08/01/2024965,003901P04B1AAP0
16100003Vito Glaserfix 111 6x3 mm zwart - 10x25 m08/01/2024965,003901P04B1P04B91
17100004Vito Glaserfix 111 6x4 mm wit - 10x25 m06/09/2023964,003901P02B1A000
18100004Vito Glaserfix 111 6x4 mm wit - 10x25 m06/09/2023969,893901P02B1P02C60
Boekingen
Cell Formulas
RangeFormula
B5B5=COUNTA(#REF!)
 
Upvote 0
You said the code in post 1 works.
For the sample data shown in post 3 the post 1 code deletes every line of data and the heading row in row 6.
My code in does exactly the same thing but faster, which is what you asked for as I understood it.

So, ..
  1. Should the code only check row 7 and below, not row 6 and below as per your original code?

  2. What about you give some relevant sample data where not every row would get deleted? .. and then manually delete the rows that should be deleted and post that separately also with XL2BB?
 
Upvote 0

Forum statistics

Threads
1,215,071
Messages
6,122,963
Members
449,094
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