Home > Enterprise >  Hide multiple range of rows based on differents cell
Hide multiple range of rows based on differents cell

Time:07-22

Currently i have a working script exept that it is a bit slow due to the fact that it is hidding row 1 by 1 when i actually need to hide 58 rows per loop, i know it is possible to hide a range of loop and made it work but only for the first 58 rows, dosen't hide the next 58. There is in total 1740 rows so i am hidding up to 1740 time instead of 30 time.

So the goal of the script is to hide rows as to not print this part of the worksheet, there is in total 30 sheet of paper that can be printed in this worksheet. each sheet is 58 row and what determine if the sheet is to be printed is if cell I3, for the first sheet, have a text or if it is emtpy or have a space, the next sheet is I63, then I119 and so on.

enter image description here

the code in question is this one and is working by hiding row 1 by 1 :

Sub Hide_column_and_Row_FR_3_XX_Fiche_Erreur()

Dim NbreLigne As Integer
Dim hh As Variant
Dim zz As Variant
Dim tableau As range


Set wrkshtDoc = ActiveWorkbook.Worksheets("FR-3-XX_Fiche d'erreur")
Set tableau = wrkshtDoc.range("A1:L1740")
hh = 1

NbreLigne = tableau.Rows.Count

For k = 3 To NbreLigne

            If tableau(k, 9) = " " Or tableau(k, 9) = Empty Then
               For zz = 1 To 58
               wrkshtDoc.Rows(hh).Hidden = True
               hh = hh   1
               Next zz
                  
            Else
               For zz = 1 To 58
               wrkshtDoc.Rows(hh).Hidden = False
               hh = hh   1
               Next zz
            End If
        
        k = k   57
          
Next k

End Sub

I tried different way to select multiples rows but i alway end up being block behind ''missing object''. i can do code inside a cell in a worksheet but VBA is a different thing... i tried to reuse as much code as possible that was already in this document to make something...

Thanks.

CodePudding user response:

Use Step to step 58 rows at a time and Resize to pick up all 58 rows:

Sub Hide_column_and_Row_FR_3_XX_Fiche_Erreur()

    Dim NbreLigne As Long
    Dim tableau As Range
    
    
    Set wrkshtDoc = ActiveWorkbook.Worksheets("FR-3-XX_Fiche d'erreur")
    Set tableau = wrkshtDoc.Range("A1:L1740")
    
    NbreLigne = tableau.Rows.Count
    
    Dim k As Long
    For k = 3 To NbreLigne Step 58
        tableau(k, 9).Resize(58, 1).EntireRow.Hidden = (tableau(k, 9) = " " Or tableau(k, 9) = Empty)
    Next k
End Sub
  • Related