What is the best way to erase lines in a table that are hidden

Romano_odK

Active Member
Joined
Jun 4, 2020
Messages
380
Office Version
  1. 365
Platform
  1. Windows
Good morning,
At first I searched through the internet for VAB coding to erase lines that are hidden by a filter. But the most of what I found are to complex for me to change or do not work properly. I simply only want to erase the lines that are hidden in a table. Who can advice me in this matter?
Thank you for you time.

Book1
ABCDEFGHIJK
1ArtikelcodeOmschrijvingKostprijsOntvgstdatumAantalWaardeVrrdrekStd_locatieMagazijnLocatieVoorraad
2100000Vito Glaserfix 111 6x2 mm wit - 10x25 m2,0011/07/202396103901P02B1P02B8,0
3100001Vito Glaserfix 111 6x2 mm zwart - 10x25 m2,0018/04/202396103901P04B1P04B8,0
4100002Vito Glaserfix 111 6x3 mm wit - 10x25 m2,0011/07/202396103901P02B1P02B8,0
5100003Vito Glaserfix 111 6x3 mm zwart - 10x25 m2,0025/04/202396103901P04B1P04B8,0
6100004Vito Glaserfix 111 6x4 mm wit - 10x25 m2,0006/09/202396103901P02B1P02B8,0
7100004Vito Glaserfix 111 6x4 mm wit - 10x25 m2,0006/09/202396103901P02B1P02C8,0
8100005Vito Glaserfix 111 6x4 mm zwart - 10x25 m2,0031/03/202196103901AAP1P04B8,0
9100006Vito Glaserfix 111 9x2 mm wit - 10x25 m2,0013/09/202360103901P01B1P01B8,0
10100007Vito Glaserfix 111 9x2 mm zwart - 10x25 m2,0016/08/202360103901P03B1P03B8,0
11100008Vito Glaserfix 111 9x3 mm wit - 10x25 m2,0016/08/2023200103901Q07A1Q07A8,0
12100008Vito Glaserfix 111 9x3 mm wit - 10x25 m2,0016/08/2023200103901Q07A1Q09B8,0
14100009Vito Glaserfix 111 9x3 mm zwart - 10x25 m2,0001/08/2023200103901Q07A1Q07B8,0
16100009Vito Glaserfix 111 9x3 mm zwart - 10x25 m2,0001/08/2023200103901Q07A1Q07A8,0
17100010Vito Glaserfix 111 9x4 mm wit - 10x25 m2,0027/06/2023200103901Q08A1Q07B8,0
18100010Vito Glaserfix 111 9x4 mm wit - 10x25 m2,0027/06/2023200103901Q08A1Q08A8,0
19100011Vito Glaserfix 111 9x4 mm zwart - 10x25 m2,0004/04/2023200103901Q07A1Q07A8,0
20100012Vito Glaserfix 111 9x5 mm wit - 10x10 m2,0023/08/202360103901P01B1P01B8,0
21100013Vito Glaserfix 111 9x5 mm zwart - 10x10 m2,0015/06/202160103901P03B1P03B8,0
22100014Vito Glaserfix 111 9x6 mm wit - 10x10 m2,0006/10/202060103901P01B1P01B8,0
23100015Vito Glaserfix 111 9x6 mm zwart - 10x10 m2,0018/04/202360103901P03B1P03B8,0
24101000Vito Glaserfix 111 9x1 mm wit - 33 m2,0023/05/20232448103901O01A1O01A8,0
25101000Vito Glaserfix 111 9x1 mm wit - 33 m2,0023/05/20232448103901O01A1O01C8,0
26101001Vito Glaserfix 111 9x1 mm zwart - 33 m2,0011/07/20232473103901O01A1O01A8,0
27101001Vito Glaserfix 111 9x1 mm zwart - 33 m2,0011/07/20232473103901O01A1O01B8,0
28
Sheet1
 
Good morning,
This is very fast but also erases my table and does not delete the lines. Fast yes, but not as functional as I would like it to be. Thank you for our time.
If the table is an actual table (not just a range) then try this (change "Table1" to suit):
VBA Code:
Sub Romano_odK_2()
Dim r As Range, q As Range, a As Range, b As Range
Dim tbl As ListObject

Set tbl = ActiveSheet.ListObjects("Table1")

Set q = tbl.DataBodyRange.Columns(1)
Set r = q.SpecialCells(xlCellTypeVisible)

tbl.AutoFilter.ShowAllData

r.EntireRow.Hidden = True
q.SpecialCells(xlCellTypeVisible).Rows.ClearContents
q.EntireRow.Hidden = False

tbl.Range.Sort Key1:=tbl.Range.Cells(1), Order1:=xlAscending, Header:=xlYes

'first blank row
Set a = tbl.Range.Cells(1).End(xlDown).Offset(1)
'last row
Set b = a.End(xlDown)

If Not Intersect(b, tbl.DataBodyRange) Is Nothing Then
    Range(a, b).Rows.Delete xlUp
Else 'if there's only 1 blank row
    a.Rows.Delete xlUp
End If

End Sub
 
Upvote 0
Solution

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
If the table is an actual table (not just a range) then try this (change "Table1" to suit):
VBA Code:
Sub Romano_odK_2()
Dim r As Range, q As Range, a As Range, b As Range
Dim tbl As ListObject

Set tbl = ActiveSheet.ListObjects("Table1")

Set q = tbl.DataBodyRange.Columns(1)
Set r = q.SpecialCells(xlCellTypeVisible)

tbl.AutoFilter.ShowAllData

r.EntireRow.Hidden = True
q.SpecialCells(xlCellTypeVisible).Rows.ClearContents
q.EntireRow.Hidden = False

tbl.Range.Sort Key1:=tbl.Range.Cells(1), Order1:=xlAscending, Header:=xlYes

'first blank row
Set a = tbl.Range.Cells(1).End(xlDown).Offset(1)
'last row
Set b = a.End(xlDown)

If Not Intersect(b, tbl.DataBodyRange) Is Nothing Then
    Range(a, b).Rows.Delete xlUp
Else 'if there's only 1 blank row
    a.Rows.Delete xlUp
End If

End Sub
This is blazing fast and works great. Thank you. Have a great weekend.
 
Upvote 0
You're welcome, glad to help & thanks for the feedback.:)
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,176
Members
453,021
Latest member
Justyna P

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