Inefficient code for hiding multiple sets of rows based on a formula driven cell value

PaulinhoC

New Member
Joined
Sep 15, 2021
Messages
8
Office Version
  1. 365
Platform
  1. Windows
Hi all, I have a worksheet 'C' where 'cell 'C5' is derived from a formula that is pulling from another worksheet. I have the following code to certain rows based on the value of 'C5'.
e.g. If 'C5'="Risk", then: rows 193:251 = Hidden ; rows 8:192 = Unhidden
If 'C5'="TEK", then rows 8:168 = Hidden; rows 169:192 = Unhidden, rows 193:251 = Hidden
If 'C5'=0 then rows 8:251 = Unhidden

I am finding that as I start filling out the worksheet with more and more data (includes functions in columns F, G, H, J, K, L), the code is running inefficient and in an endless loop when "TEK" is the value in C5.

Can someone point me to why my code is running so inefficient and endless looping when "TEK" is the value triggering which rows to hide?
Many thanks in advance.

VBA Code:
Private Sub Worksheet_Calculate()
Dim Area As Range
Set Area = Range("C5")

Select Case Area
        Case Is = "Risk": Rows("8:192").Hidden = True
                            Rows("193:251").Hidden = False
        Case Is = "TEK":     Rows("169:192").Hidden = False
                          Rows("8:168").Hidden = True
                          Rows("193:251").Hidden = True
        Case Is = 0:        Rows("8:251").Hidden = False
End Select
End Sub
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
See if this helps:

VBA Code:
Private Sub Worksheet_Calculate()
'
'
'   Turn Settings off
      Application.ScreenUpdating = False                                    ' Turn Screen Updating off
         Application.Calculation = xlCalculationManual                      ' Turn AutoCalculation off
        Application.EnableEvents = False                                    ' Turn EnableEvents off
'
    Dim Area As Range
'
    Set Area = Range("C5")
'
    Select Case Area
        Case "Risk"
              Rows("8:192").Hidden = True
            Rows("193:251").Hidden = False
        Case "TEK"
            Rows("169:192").Hidden = False
              Rows("8:168").Hidden = True
            Rows("193:251").Hidden = True
        Case 0
            Rows("8:251").Hidden = False
    End Select
'
'   Turn Settings back on
    Application.EnableEvents = True                                                             ' Turn EnableEvents back on
    Application.Calculation = xlCalculationAutomatic                                            ' Turn AutoCalculation back on
    Application.ScreenUpdating = True                                                           ' Turn Screen Updating back on
End Sub
 
Upvote 0
Solution
See if this helps:

VBA Code:
Private Sub Worksheet_Calculate()
'
'
'   Turn Settings off
      Application.ScreenUpdating = False                                    ' Turn Screen Updating off
         Application.Calculation = xlCalculationManual                      ' Turn AutoCalculation off
        Application.EnableEvents = False                                    ' Turn EnableEvents off
'
    Dim Area As Range
'
    Set Area = Range("C5")
'
    Select Case Area
        Case "Risk"
              Rows("8:192").Hidden = True
            Rows("193:251").Hidden = False
        Case "TEK"
            Rows("169:192").Hidden = False
              Rows("8:168").Hidden = True
            Rows("193:251").Hidden = True
        Case 0
            Rows("8:251").Hidden = False
    End Select
'
'   Turn Settings back on
    Application.EnableEvents = True                                                             ' Turn EnableEvents back on
    Application.Calculation = xlCalculationAutomatic                                            ' Turn AutoCalculation back on
    Application.ScreenUpdating = True                                                           ' Turn Screen Updating back on
End Sub

That has done the trick!!! JohnnyL you are AMAZING!!!!

Thank you so much!!
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,191
Members
452,616
Latest member
intern444

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top