Forum Discussion

BrianBrucker's avatar
BrianBrucker
Copper Contributor
Dec 29, 2023

vba hide a row syntax

This is what I am trying to hide by selecting and deselecting box k box s is the start of rows to be hidden and u is the last row to be hidden.  I don't know what the proper syntax is to achieve this is

 

Sub Hide_Rows_Based_On_Cell_Value()

StartRow = 19

EndRow = 200

ColNum = 10

For i = StartRow To EndRow

If Cells(i, ColNum).Value = “True” Then

Rows("$S$"i":$U$"i").EntireRow.Hidden = False
Else
Rows("$S$"i":$U$"i").EntireRow.Hidden = True
End If

Next i
End Sub

When Box K19 is selected it hides lines 20-28 . These are all the current sections that can be populated and the rows each has to hide.

Check box,      Rows to hide
IF CELL K__ TRUE Hide rows S - U
K19 20 - 28
K30 31 - 37
K39 40 - 42
K44 45 - 49
K51 52 - 53
K55 56 - 63
K65 66 - 76
K78 79 - 82
K84 85 - 90
K92 93 - 96
K98 99 - 101
K103 104 - 110
K112 113 - 119
K121 122 - 126
K128 129 - 142
K144 146 - 147
K149 150 - 152
K154 155 - 161
K163 164 - 169
K171 172 - 176
K178 179 - 181

 

3 Replies

  • SnowMan55's avatar
    SnowMan55
    Bronze Contributor

    BrianBrucker As you are already using VBA code, I will assume that you are aware of how anyone who can edit the workbook can easily insert malware.  If you are not aware, we can have a separate discussion (offline).


    See the attached workbook.  I have not attempted to reproduce all the shading, borders, and cell merging that your worksheet apparently has.  But the solutions contained in the workbook (or in yours) should not be affected by this absence.  Read the _Info worksheet for more information.


    Row 145 is not included in your range of rows to hide; I don't know if that's a mistake, or if it contains special data that is not to be hidden.


    Importantly, it appears that you are not declaring your variables, and (preferably) including an Option Explicit at the top of each code module (which includes form modules, class modules, and standard modules, the last of which Microsoft now ambiguously just calls modules).  I strongly recommend that you use Option Explicit statements and declare each variable.  Without them, when you mistype a variable name, VBA silently creates a new variable for the misspelling, effectively initialized to zero or an empty string.  (Such bugs can be hard to find.)  With them, the VB Editor will inform you of the undeclared variable, and stop execution (allowing you to fix the problem, usually without terminating execution).


    The VB Editor will automatically include an Option Explicit statement when it creates a code module if this option is selected (under Tools | Options…):


    The main problem was that you need the code to extract the row numbers from the S and U cells, and then use those numbers in the argument for the Rows collection.  I declared additional variables and added code for that.  So the resulting statement that hides (or unhides) is:

    Rows(FirstRowToHide & ":" & LastRowToHide).EntireRow.Hidden = True '(or False, for unhiding)


    Your code did run slowly (about 1.2 seconds on my computer), but this slowness was almost eliminated by including an

    Application.ScreenUpdating = False

    statement at the top of the existing executable code, and an

    Application.ScreenUpdating = True

    statement after the other executable code (before the end of the procedure).  That change alone speeds it up to about 0.1 seconds on my computer, nearly the same speed as djclements' code.


    I presume you are assigning your macro to each checkbox. Though a bit wasteful in processing, that works.

    =================== BUT

    But why use VBA at all, especially considering the security risks?  Excel has an outlining (grouping) feature.  See the Outlined worksheet, where I added a column and defined half of the possible groups.

     

    • BrianBrucker's avatar
      BrianBrucker
      Copper Contributor
      Thanks. I am new to VBA code, not really sure of what I am doing. This grouping rows I did not know about. I have never taken any courses on it and this has helped a lot. Thanks.
  • djclements's avatar
    djclements
    Bronze Contributor

    BrianBrucker If I've understood you correctly, the "Populate this section" checkbox shown in your screenshot is linked to cell K19 and controls rows 20 through 28. There are 21 checkboxes in total on the spreadsheet, each linked to a different cell (K19, K30, K39, K44, etc.) and each controlling their corresponding sections (rows 20-28, 31-37, 40-42, 45-49, etc.). If so, the following code should do the trick:

     

    Sub ShowHideSections()
    'Map the checkbox cells and their corresponding rows in an array
        Dim sections As String, data As Variant
        sections = "K19,20:28;K30,31:37;K39,40:42;K44,45:49;K51,52:53;" & _
            "K55,56:63;K65,66:76;K78,79:82;K84,85:90;K92,93:96;K98,99:101;" & _
            "K103,104:110;K112,113:119;K121,122:126;K128,129:142;K144,146:147;" & _
            "K149,150:152;K154,155:161;K163,164:169;K171,172:176;K178,179:181"
        data = Split(sections, ";")
    
    'Loop through each linked cell and show/hide their corresponding rows
        Dim ws As Worksheet, i As Long, arr As Variant
        Set ws = Application.ActiveSheet
        Application.ScreenUpdating = False
        For i = LBound(data) To UBound(data)
            arr = Split(data(i), ",")
            If ws.Range(arr(0)).Value Then
                ws.Rows(arr(1)).Hidden = False
            Else
                ws.Rows(arr(1)).Hidden = True
            End If
        Next i
        Application.ScreenUpdating = True
    End Sub

     

    The Split method is used to extract the corresponding cell and row references from the delimited string, then show/hide each section accordingly, which is faster than looping through each row in each section to show/hide them one at a time.

Resources