Question: I am using the following vba codes which im using to hide a set of rows and unhide rows depending on if a cell contains text or not, and they are causing my excel spreadsheet to be slow and unresponsive and causing the egg timer to show for about 10 seconds.

If I take the code out It speeds things up so what can I do to my codes to get them to speed up and not take so long? perhaps there is a better way of structuring the code but im really new to vba so am not sure what I would need to do, would appreciate someone's help thanks.

the reason I am using worksheet change and worksheet selection change is so that whether a user clicks on a cell or not the page still updates

```
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
       If Range("K22").Value <> "" Then
    Application.ScreenUpdating = False
    Rows("25:38").EntireRow.Hidden = False
    Rows("40:48").EntireRow.Hidden = True
    ElseIf Range("K22").Value = "" Then
    Rows("25:38").EntireRow.Hidden = True
    Rows("40:48").EntireRow.Hidden = False
    End If
    Application.ScreenUpdating = True
End Sub

Private Sub Worksheet_Change(ByVal Target As Range)
       If Range("K22").Value <> "" Then
    Application.ScreenUpdating = False
    Rows("25:38").EntireRow.Hidden = False
    Rows("40:48").EntireRow.Hidden = True
    ElseIf Range("K22").Value = "" Then
    Rows("25:38").EntireRow.Hidden = True
    Rows("40:48").EntireRow.Hidden = False
    End If
    Application.ScreenUpdating = True
End Sub

```

Answer: 
The main issue is from the Worksheet\_Change event, but it could be applied to any event.

The worksheet change is triggering each time you hide a column, so it's trying several times to hide the same columns, before (eventually) failing with an out of memory error:

*Hide these columns... Oh, a worksheet change... Hide these columns... Oh, A worksheet change... Hide th...*

To avoid this, you need to use

```
Application.EnableEvents = False

```

when you decide you are going to make changes, and

```
Application.EnableEvents = True

```

when done.  

You may also want to put some error handling that turns the events on again, as if something else occurs that stops the code from running, the triggers will be turned off, and the spreadsheet will no longer update as you expect it to.

```
Private Sub Worksheet_Change(ByVal Target As Range)
Application.ScreenUpdating = False
Application.EnableEvents = False
If Range("K22").Value <> "" Then
    Rows("25:38").Hidden = False
    Rows("40:48").Hidden = True
Else
    Rows("25:38").Hidden = True
    Rows("40:48").Hidden = False
End If
Application.EnableEvents = True
Application.ScreenUpdating = True
End Sub

```
This works instantly for me:

```
Application.ScreenUpdating = False
Select Case Range("K22")
Case Is <> ""
    Rows("25:38").Hidden = False
    Rows("40:48").Hidden = True
Case Else
    Rows("25:38").Hidden = True
    Rows("40:48").Hidden = False
End Select
Application.ScreenUpdating = True

```