0

I am trying to hide multiple rows in an excel worksheet which are empty using following code however i am getting error message "Argument not optional". What could be wrong in the code?

Sub Attendance_Manday()
Dim sht1 As Worksheet
Dim row_count, col_count As Integer
Dim mainrange As Range
Dim startcell As Range
Set startcell = Range("B1")
Set sht1 = Sheets("Mandays")
row_count = Sheets("Mandays").Cells(Rows.Count, startcell.Column).End(xlUp).Row
col_count = Sheets("Mandays").Cells(startcell.Row, Columns.Count).End(xlToLeft).Offset(1, -2).Column
Set mainrange = sht1.Range(startcell.Address & ":" & sht1.Cells(row_count, col_count).Address)
mainrange.Range.SpecialCells(xlCellTypeBlanks).Rows.Hidden = True
End Sub

1 Answers1

0

Based on your code and assuming first row in your sheet is never empty you could do something like that

Sub Attendance_Manday()
    Dim sht1 As Worksheet
    Dim row_count As Long, col_count As Long
    Dim mainrange As Range
    Dim startcell As Range
    Set startcell = Range("B1")
    Set sht1 = Sheets("Mandays")
    row_count = Sheets("Mandays").Cells(Rows.Count, startcell.Column).End(xlUp).Row
    col_count = Sheets("Mandays").Cells(startcell.Row, Columns.Count).End(xlToLeft).Offset(1, -2).Column
    Set mainrange = sht1.Range(startcell.Address & ":" & sht1.Cells(row_count, col_count).Address)
    
    
    Dim i As Long
    For i = 1 To col_count - 1
        mainrange.AutoFilter field:=i, Criteria1:="="
    Next i
    
    Dim rg As Range
    Set rg = mainrange.SpecialCells(xlCellTypeVisible)
    mainrange.AutoFilter

    rg.Rows.EntireRow.Hidden = True
    rg.Rows(1).EntireRow.Hidden = False
    
    
        
End Sub

An if you turn off screenupdating etc. it should be pretty fast as well

Storax
  • 11,158
  • 3
  • 16
  • 33