Quantcast
Viewing all articles
Browse latest Browse all 452

Show/hide rows and sheets based on other cell values

I have a workbook with a lot of worksheets.
On Sheet1, I have a lot of "sections".
In each of these "sections", I have a cell (e.g., B31), that can be either "Yes" or "No".

If the cell is "Yes", then I want to:

  • show rows 32 to 46 on the same sheet (Sheet1)
  • show rows 19 to 36 on a different sheet (Sheet 2)
  • show Sheet3

If the cell is "No", then I want to:

  • hide rows 32 to 46 on the same sheet (Sheet1)
  • hide rows 19 to 36 on a different sheet (Sheet 2)
  • hide Sheet3

This is the premise for 30 different "sections" on Sheet1.
Each of these section will evaluate a "Yes" or "No" value in different cells and show/hide the next 15 rows in the same sheet, show/hide rows Sheet2, and hide a certain worksheet entirely (ranging from Sheet3 to Sheet 30).

I have it working, but I feel I've coded it poorly.
Maintenance on this will be a bear if someone adds sections.
There are times I get a black screen for a few seconds because it is taking a long time to run.

The code is repeated for each section. The code below is for three sections.

Is there a way I can code this better for reusability and performance?

Dim ScopeChange As RangeDim Module_1 As VariantDim Module_2 As VariantDim Module_3 As VariantModule_1 = Range("B31").ValueModule_2 = Range("B49").ValueModule_3 = Range("B67").ValueSet ScopeChange = Range("B31")If Not ScopeChange Is Nothing Then    Application.EnableEvents = False    Select Case Module_1        Case "Yes": Rows("32:46").EntireRow.Hidden = False                    Worksheets("Sheet2").Rows("19:36").EntireRow.Hidden = False                    Worksheets("Sheet3").Visible = True        Case "No": Rows("32:46").EntireRow.Hidden = True                    Worksheets("Sheet2").Rows("19:36").EntireRow.Hidden = True                    Worksheets("Sheet3").Visible = False    End Select    Application.EnableEvents = TrueEnd IfSet ScopeChange = Range("B49")If Not ScopeChange Is Nothing Then    Application.EnableEvents = False    Select Case Module_2        Case "Yes": Rows("50:64").EntireRow.Hidden = False                    Worksheets("Sheet2").Rows("37:54").EntireRow.Hidden = False                    Worksheets("Sheet4").Visible = True        Case "No": Rows("50:64").EntireRow.Hidden = True                    Worksheets("Sheet2").Rows("37:54").EntireRow.Hidden = True                    Worksheets("Sheet4").Visible = False    End Select    Application.EnableEvents = TrueEnd IfSet ScopeChange = Range("B67")If Not ScopeChange Is Nothing Then    Application.EnableEvents = False    Select Case Module_3        Case "Yes": Rows("68:82").EntireRow.Hidden = False                    Worksheets("Sheet2").Rows("55:72").EntireRow.Hidden = False                    Worksheets("Sheet5").Visible = True        Case "No": Rows("68:82").EntireRow.Hidden = True                    Worksheets("Sheet2").Rows("55:72").EntireRow.Hidden = True                    Worksheets("Sheet5").Visible = False    End Select    Application.EnableEvents = TrueEnd IfEnd Sub

Viewing all articles
Browse latest Browse all 452

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>