Is there something faster to erase lines?

Romano_odK

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

Got this code which works, but deleting those from a 10000 row table isn't really fast. Does anyone know if there is a faster way to do this?

Thank you for your time.

VBA Code:
    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, "J") >= "0") Or (Cells(r, "H") <> "1") Then
'           Delete row
            Rows(r).Delete
        End If
    Next r
    
    Application.ScreenUpdating = True
 
Perhaps you could show us what your table looks like with XL2BB?

Is it a formal Excel table?
Its a table filled through our SQL server.

Locatie controle 3.34 beta.xlsm
ABCDEFGHIJKLM
1Datum2023/11/16
2Export folderc:\temp\Locatie-overslagregels.xml
3OmschrijvingLocatie overslagregels
4Dagboek90
5Regels1
6ArtikelcodeOmschrijvingOntvgstdatumAantalKostprijsVrrdrekStd_locatieMagazijnLocatieVoorraadvan Locatienaar LocatieVerplaats
7100000Vito Glaserfix 111 6x2 mm wit - 10x25 m11/07/2023966,703901P02B1A000
8100000Vito Glaserfix 111 6x2 mm wit - 10x25 m11/07/2023966,703901P02B1P02B77
9100001Vito Glaserfix 111 6x2 mm zwart - 10x25 m18/04/2023966,703901P04B1A000
10100001Vito Glaserfix 111 6x2 mm zwart - 10x25 m18/04/2023966,703901P04B1P04B90
11100002Vito Glaserfix 111 6x3 mm wit - 10x25 m15/11/2023967,313901P02B1A000
12100002Vito Glaserfix 111 6x3 mm wit - 10x25 m15/11/2023967,313901P02B1AAP0
13100002Vito Glaserfix 111 6x3 mm wit - 10x25 m15/11/2023967,313901P02B1P02B104
14100003Vito Glaserfix 111 6x3 mm zwart - 10x25 m25/04/2023967,313901P04B1A000
15100003Vito Glaserfix 111 6x3 mm zwart - 10x25 m25/04/2023967,313901P04B1P04B19
Boekingen
Cell Formulas
RangeFormula
B5B5=COUNTA(#REF!)
 
Upvote 0

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Thanks for the XL2BB sample data. The sample shows two things to me ..
  1. Your original 'working' code checked up from the bottom, including row 6, so mine did too. In this sample however row 6 is a heading so I presume should not be considered for deletion? Please clarify.

  2. In this sample every row would be deleted because one of your code conditions is delete the row if column J is greater than or equal to 0. In this sample every row meets that condition. In your larger actual data are there any rows where col J is not >=0? If so, could we have another sample where some rows would be deleted and some would not?
 
Upvote 0
Usually it is faster to use filtering to define the range of rows that need deleting and delete them as a block

VBA Code:
'Delete all rows that meet criteria in column FilterColumn
'FilterRange must include a header row
Sub DeleteRows(FilterRange As Range, FilterColumn As Long, Criteria As Variant, WS As Worksheet)
    Dim DataRange As Range
    Dim DeleteRange As Range
   
    WS.AutoFilterMode = False
    Set DataRange = FilterRange.Offset(1).Resize(FilterRange.Rows.Count - 1)    'excludes header row
   
    Application.ScreenUpdating = False
   
    FilterRange.AutoFilter Field:=FilterColumn, Criteria1:=Criteria
    Set DeleteRange = Application.Intersect(FilterRange.SpecialCells(xlCellTypeVisible).EntireRow, DataRange.EntireRow)
   
    If Not DeleteRange Is Nothing Then
        DeleteRange.Delete                            'delete rows
    End If
    WS.AutoFilterMode = False
    Application.ScreenUpdating = True
End Sub

VBA Code:
Sub Example()
    Dim WS As Worksheet
    Dim rng As Range

    Set WS = ActiveSheet
    With WS
        Set rng = .Range("A6:M" & .Range("A" & .Rows.Count).End(xlUp).Row)
    End With
    
    DeleteRows rng, 8, "=1", WS
    DeleteRows rng, 10, ">=0", WS
End Sub
If you wanted to delete everything with a single call to Sub DeleteRows you would need to define a helper column with a boolean formula identifiying each row than needed to be deleted.
 
Upvote 1
Thanks for the XL2BB sample data. The sample shows two things to me ..
  1. Your original 'working' code checked up from the bottom, including row 6, so mine did too. In this sample however row 6 is a heading so I presume should not be considered for deletion? Please clarify.

  2. In this sample every row would be deleted because one of your code conditions is delete the row if column J is greater than or equal to 0. In this sample every row meets that condition. In your larger actual data are there any rows where col J is not >=0? If so, could we have another sample where some rows would be deleted and some would not?
Good afternoon Peter,

You are right about the headers, row 6 should have been row 7. Column J has indeed values less then 0 which should not happen but they do so I have to take them into account.



Locatie controle 3.34 beta.xlsm
ABCDEFGHIJ
6ArtikelcodeOmschrijvingOntvgstdatumAantalKostprijsVrrdrekStd_locatieMagazijnLocatieVoorraad
7100000Vito Glaserfix 111 6x2 mm wit - 10x25 m11/07/2023966,703901P02B1A000
8100000Vito Glaserfix 111 6x2 mm wit - 10x25 m11/07/2023966,703901P02B1P02B77
9100001Vito Glaserfix 111 6x2 mm zwart - 10x25 m18/04/2023966,703901P04B1A000
10100001Vito Glaserfix 111 6x2 mm zwart - 10x25 m18/04/2023966,703901P04B1P04B70
11100002Vito Glaserfix 111 6x3 mm wit - 10x25 m15/11/2023967,313901P02B1A000
12100002Vito Glaserfix 111 6x3 mm wit - 10x25 m15/11/2023967,313901P02B1AAP0
13100002Vito Glaserfix 111 6x3 mm wit - 10x25 m15/11/2023967,313901P02B1P02B-10
14100003Vito Glaserfix 111 6x3 mm zwart - 10x25 m25/04/2023967,313901P04B1A000
15100003Vito Glaserfix 111 6x3 mm zwart - 10x25 m25/04/2023967,313901P04B1P04B19
16100004Vito Glaserfix 111 6x4 mm wit - 10x25 m06/09/2023969,893901P02B1P02B81
17100004Vito Glaserfix 111 6x4 mm wit - 10x25 m06/09/2023969,893901P02B1P02C60
18100004Vito Glaserfix 111 6x4 mm wit - 10x25 m06/09/2023969,893901P02B1A000
19100005Vito Glaserfix 111 6x4 mm zwart - 10x25 m31/03/2021969,893901AAP1A000
20100005Vito Glaserfix 111 6x4 mm zwart - 10x25 m31/03/2021969,893901AAP1P04B53
21100006Vito Glaserfix 111 9x2 mm wit - 10x25 m07/11/20231207,743901P01B1A00-1
22100006Vito Glaserfix 111 9x2 mm wit - 10x25 m07/11/20231207,743901P01B1AAP0
23100006Vito Glaserfix 111 9x2 mm wit - 10x25 m07/11/20231207,743901P01B1P01B80
Boekingen
 
Upvote 0
Thanks for the new sample data. Due to the different header information discussed above, I made this one small change to your original code which you said worked ..

Rich (BB code):
Sub Post_1_Code()
    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 7 Step -1
'       See if columns H, I, or J is empty
        If (Cells(r, "J") >= "0") Or (Cells(r, "H") <> "1") Then
'           Delete row
            Rows(r).Delete
        End If
    Next r
   
    Application.ScreenUpdating = True
End Sub

.. and ran it on this latest sample data. This is the result

Romano_odK.xlsm
ABCDEFGHIJ
6ArtikelcodeOmschrijvingOntvgstdatumAantalKostprijsVrrdrekStd_locatieMagazijnLocatieVoorraad
7100002Vito Glaserfix 111 6x3 mm wit - 10x25 m15/11/2023967.313901P02B1P02B-10
8100006Vito Glaserfix 111 9x2 mm wit - 10x25 m07/11/20231207.743901P01B1A00-1
9
Sheet3 (2)


I then ran my code from post #3 with two tiny changes (again to adjust for the different header row) ...

Rich (BB 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("H7: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, 3) >= 0 Or a(i, 1) <> 1 Then
      b(i, 1) = 1
      k = k + 1
    End If
  Next i
  If k > 0 Then
    Application.ScreenUpdating = False
    With Range("A7").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

.. on the latest sample data. This is the result

Romano_odK.xlsm
ABCDEFGHIJ
6ArtikelcodeOmschrijvingOntvgstdatumAantalKostprijsVrrdrekStd_locatieMagazijnLocatieVoorraad
7100002Vito Glaserfix 111 6x3 mm wit - 10x25 m15/11/2023967.313901P02B1P02B-10
8100006Vito Glaserfix 111 9x2 mm wit - 10x25 m07/11/20231207.743901P01B1A00-1
9
Sheet3 (3)


the results are identical so not sure why you say your code works and my code ..
unfortunately it messes up my table totally.
 
Upvote 0
Solution
Thanks for the new sample data. Due to the different header information discussed above, I made this one small change to your original code which you said worked ..

Rich (BB code):
Sub Post_1_Code()
    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 7 Step -1
'       See if columns H, I, or J is empty
        If (Cells(r, "J") >= "0") Or (Cells(r, "H") <> "1") Then
'           Delete row
            Rows(r).Delete
        End If
    Next r
  
    Application.ScreenUpdating = True
End Sub

.. and ran it on this latest sample data. This is the result

Romano_odK.xlsm
ABCDEFGHIJ
6ArtikelcodeOmschrijvingOntvgstdatumAantalKostprijsVrrdrekStd_locatieMagazijnLocatieVoorraad
7100002Vito Glaserfix 111 6x3 mm wit - 10x25 m15/11/2023967.313901P02B1P02B-10
8100006Vito Glaserfix 111 9x2 mm wit - 10x25 m07/11/20231207.743901P01B1A00-1
9
Sheet3 (2)


I then ran my code from post #3 with two tiny changes (again to adjust for the different header row) ...

Rich (BB 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("H7: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, 3) >= 0 Or a(i, 1) <> 1 Then
      b(i, 1) = 1
      k = k + 1
    End If
  Next i
  If k > 0 Then
    Application.ScreenUpdating = False
    With Range("A7").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

.. on the latest sample data. This is the result

Romano_odK.xlsm
ABCDEFGHIJ
6ArtikelcodeOmschrijvingOntvgstdatumAantalKostprijsVrrdrekStd_locatieMagazijnLocatieVoorraad
7100002Vito Glaserfix 111 6x3 mm wit - 10x25 m15/11/2023967.313901P02B1P02B-10
8100006Vito Glaserfix 111 9x2 mm wit - 10x25 m07/11/20231207.743901P01B1A00-1
9
Sheet3 (3)


the results are identical so not sure why you say your code works and my code ..
Good morning,
My excuses for taking so long. I have tried it again and it works perfectly. I think it indeed had to do with the header at first, but I am not sure. Thank you for your time and efforts. I wish you a great day.
 
Upvote 0
Good news! Thanks for the follow-up. :)
Hopefully it should be very fast.
 
Upvote 0
Its a table filled through our SQL server.
Hi, if you're importing this table with Power Query (Data > Get Data) then you can remove the unwanted rows with the query editor without any code.
 
Upvote 0

Forum statistics

Threads
1,224,825
Messages
6,181,190
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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