Speeding up some slow code

jUStPunkin

Board Regular
Joined
Mar 23, 2009
Messages
67
I have a spreadsheet that I have protected (simply so that people don't change my formulas, either intentionally or accidentally). As I'm not as concerned with the intentional changes, I don't have it password protected, just protected.

In any case, on a particular worksheet, I give people a choice via a dropdown box. Based on their chose, I hide certain rows on the worksheet.

Back in access 2003, it worked fine. Now, in 2007, every single time someone changes any cell value on that worksheet, it takes forever. What I believe is happening is that it is running that hide/unhide code that I have written.

Can anyone offer some advice on how to speed this up - changes to the code, a better way to do what I want, etc.

Thanks in advance!

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Worksheets("spouts").Unprotect
    Select Case Worksheets("spouts").Range("$G$3")
        Case Is = "Holes"
            Worksheets("spouts").Range("$A$44:$N$67").EntireRow.Hidden = True
            Worksheets("spouts").Range("$A$26:$N$42").EntireRow.Hidden = False
        Case Is = "Slots"
            Worksheets("spouts").Range("$A$27:$N$29").EntireRow.Hidden = True
            Worksheets("spouts").Range("$A$34:$N$42").EntireRow.Hidden = True
            Worksheets("spouts").Range("$A$47:$N$50").EntireRow.Hidden = True
            Worksheets("spouts").Range("$A$44:$N$46").EntireRow.Hidden = False
            Worksheets("spouts").Range("$A$51:$N$67").EntireRow.Hidden = False
            
        Case Else
            Worksheets("spouts").Range("$A$27:$N$67").EntireRow.Hidden = False
    End Select
   Worksheets("spouts").Protect
End Sub

Thanks,
Brenda
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Thanks for the suggestion, but that didn't help. I'm pretty sure it's the hiding/unhiding that is causing the slow down. If I comment out that code, it acts the way I expect it to act, with no slow down at all.

Thanks,
Brenda
 
Upvote 0
You should limit the Change event to the particular cell:

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    With Worksheets("spouts")
        If Target.Address = "$G$3" Then
            Application.ScreenUpdating = False
            .Unprotect
            Select Case Target.Value
            Case "Holes"
                .Rows("44:67").Hidden = True
                .Rows("26:42").Hidden = False
            Case "Slots"
                .Rows("27:29").Hidden = True
                .Rows("34:42").Hidden = True
                .Rows("47:50").Hidden = True
                .Rows("44:46").Hidden = False
                .Rows("51:67").Hidden = False
            Case Else
                .Rows("27:67").Hidden = False
            End Select
            .Protect
        End If
    End With
End Sub

Also, use the With...End With statement to increase the maintainability of your code.
 
Upvote 0
You're welcome, thanks for testing and providing feedback.
 
Upvote 0

Forum statistics

Threads
1,224,884
Messages
6,181,569
Members
453,054
Latest member
arz007

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