Jump to content

Delete Hidden (Unfiltered) Rows in Excel


Recommended Posts

Hello,

I am trying to keep only the filtered rows in an excel sheet but couldn't find a fast way to do it for a 1k+ rows file.

Tried going through each row to see if it's hidden or not and if it is, delete it but it's not fast:

Local $oExcel = _Excel_Open()
    Local $File = _Excel_BookOpen($oExcel, $Filepath)
    Local $LastRow = $File.Activesheet.Range("A1000000").End(-4162).Row

    _Excel_FilterSet($File, Default, Default, 1, "Test")
    For $j=1 To $LastRow
        If $File.Activesheet.Range("A"&$j).EntireRow.Hidden Then _Excel_RangeDelete($File.Activesheet,$j&":"&$j)
    Next

I found a VBA macro that works great but I am having a hard time converting it to be able to use it in my script:

Sub RemoveHiddenRows()
    Dim oRow As Range, rng As Range
    Dim myRows As Range
    Set myRows = Intersect(Sheets("Sheet1").Range("A:A").EntireRow, Sheets("Sheet1").UsedRange)

    For Each oRow In myRows.Columns(1).Cells
        If oRow.EntireRow.Hidden Then
            If rng Is Nothing Then
                Set rng = oRow
            Else
                Set rng = Union(rng, oRow)
            End If
        End If
    Next

    If Not rng Is Nothing Then rng.EntireRow.Delete
End Sub

I would really appreciate it if anyone can help me so I can use this part in my autoit script.

Thanks.

Link to comment
Share on other sites

  • Moonscarlet changed the title to Delete Hidden (Unfiltered) Rows in Excel

https://www.autoitscript.com/wiki/Excel_Filter

describes how to copy all filtered (visible) rows. Should be easy to modify the example to delete all invisible rows 😃

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Set parameter $bScreenUpdating in _Excel_Open to False (and check the remarks in the help file).
This prevents Excel from updating the screen after each line delete.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...