Forum Discussion
vba hide a row syntax
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.
- BrianBruckerJan 03, 2024Copper ContributorThanks. 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.