Quantcast
Channel: Active questions tagged row - Stack Overflow
Viewing all articles
Browse latest Browse all 448

Excel VBA - Conditional logic to show/hide rows and sheets based on other cell values

$
0
0

Disclaimer: I'm a VBA noob :)

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)

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

Ultimately, this is the code I am doing for each section. The code below is repeated for each individual section I am evaluating. The code below is for 3 different sections I am evaluating Is there a way I can code this better for reusability and better performance? Or is there anything that stands out as a "dumb" way of doing this?

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 448

Trending Articles



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