How can I delete hidden rows after filter has been set?

pensix

New Member
Joined
Aug 9, 2018
Messages
2
Hi,
I currently have a workbook with multiple sheets that have a macro running to set a filter based on specific criteria from the main sheet. I would like to be able to have all items that are not a part of the filter (hidden) be deleted from the supporting sheets in an effort to reduce the size of the file. Here is the VBA code that I am presently running to set the filters. How can this code be modified to delete the non essential rows from the secondary tabs?
The current code is:

Sub Update()
'
' Update Macro
'
' Keyboard Shortcut: Ctrl+Shift+U
'
Sheets("AR 105 0000 Sub Account ").Select
Application.CutCopyMode = False
Application.CutCopyMode = False
Range("A5:AN10000").AdvancedFilter Action:=xlFilterInPlace, CriteriaRange:= _
Range("A3:AN4"), Unique:=False
Sheets(" TOTAL 6200 Sub Account ").Select
Application.CutCopyMode = False
Application.CutCopyMode = False
Range("A5:T163").AdvancedFilter Action:=xlFilterInPlace, CriteriaRange:= _
Range("A3:T4"), Unique:=False
Sheets("AP 204 0000 Sub Account").Select
Application.CutCopyMode = False
Application.CutCopyMode = False
Range("A5:AF10000").AdvancedFilter Action:=xlFilterInPlace, CriteriaRange:= _
Range("A3:AF4"), Unique:=False
Sheets("AP 204 6125 Sub Account").Select
Application.CutCopyMode = False
Application.CutCopyMode = False
Range("A5:V184").AdvancedFilter Action:=xlFilterInPlace, CriteriaRange:= _
Range("A3:U4"), Unique:=False
Sheets("SUMMARY").Select
Range("D3").Select
ActiveCell.FormulaR1C1 = "UPDATE COMPLETED"
Range("B13").Select
End Sub

Thank you in advance!
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Hi :)

TRY THIS ON A COPY OF YOUR SPREADSHET :)

Code:
[I]
Sub hiddendelete()[/I]
[I]For lp = 256 To 1 Step -1 'loop through all columns[/I]
[I]If Columns(lp).EntireColumn.Hidden = True Then Columns(lp).EntireColumn.Delete Else[/I]
[I]Next[/I]
[I]For lp = 65536 To 1 Step -1 'loop through all rows[/I]
[I]If Rows(lp).EntireRow.Hidden = True Then Rows(lp).EntireRow.Delete Else[/I]
[I]Next[/I]
[I]End Sub
[/I]

Just reference this macro in your other macro

Call hiddendelete

Good Luck :)
Mark
 
Upvote 0
Hi, thank you for your feedback. I've tried to include the code above however it only runs on the main sheet. How can I get this to run only on the sheets that are named "AR 105 0000 Sub Account" and "AP 204 0000 Sub Account"?
 
Upvote 0
Hi :)

If you just want it to run on those two sheets "AR 105 0000 Sub Account" and "AP 204 0000 Sub Account" then try this:

Code:
Sub Update()
 '
 ' Update Macro
 '
 ' Keyboard Shortcut: Ctrl+Shift+U
 '

Application.ScreenUpdating = False


     Sheets("AR 105 0000 Sub Account ").Select
     Application.CutCopyMode = False
     Application.CutCopyMode = False
     Range("A5:AN10000").AdvancedFilter Action:=xlFilterInPlace, CriteriaRange:= _
         Range("A3:AN4"), Unique:=False
[I]Call hiddendelete[/I]


Sheets(" TOTAL 6200  Sub Account  ").Select
     Application.CutCopyMode = False
     Application.CutCopyMode = False
     Range("A5:T163").AdvancedFilter Action:=xlFilterInPlace, CriteriaRange:= _
         Range("A3:T4"), Unique:=False

     Sheets("AP 204 0000 Sub Account").Select
     Application.CutCopyMode = False
     Application.CutCopyMode = False
     Range("A5:AF10000").AdvancedFilter Action:=xlFilterInPlace, CriteriaRange:= _
         Range("A3:AF4"), Unique:=False
Call [I]hiddendelete[/I]

Sheets("AP 204 6125  Sub Account").Select
     Application.CutCopyMode = False
     Application.CutCopyMode = False
     Range("A5:V184").AdvancedFilter Action:=xlFilterInPlace, CriteriaRange:= _
         Range("A3:U4"), Unique:=False
     Sheets("SUMMARY").Select
     Range("D3").Select
     ActiveCell.FormulaR1C1 = "UPDATE COMPLETED"
     Range("B13").Select

Application.ScreenUpdating = True

 End Sub

[I]Sub hiddendelete()[/I]
[I]For lp = 256 To 1 Step -1 'loop through all columns[/I]
[I]If Columns(lp).EntireColumn.Hidden = True Then Columns(lp).EntireColumn.Delete Else[/I]
[I]Next[/I]
[I]For lp = 65536 To 1 Step -1 'loop through all rows[/I]
[I]If Rows(lp).EntireRow.Hidden = True Then Rows(lp).EntireRow.Delete Else[/I]
[I]Next[/I]
[I]End Sub[/I]

Jus call the delete macro while the sheet is active. Also if you turn off Screenupdating the codes run quicker. IT MUST BE TURNED BACK ON AT THE END.

Good Luck,
Mark
 
Upvote 0

Forum statistics

Threads
1,214,827
Messages
6,121,812
Members
449,048
Latest member
greyangel23

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