Using Command Button to hide & unhide selected Rows in a table

Mark Pearmain

New Member
Joined
Aug 23, 2022
Messages
5
Office Version
  1. 365
Platform
  1. Windows
Hi!

I have used your code from another thread which works really well, however, I am running this on 250 rows and this takes quite some time to execute when hiding the rows, whereas the show part is instant. I tried running a macro to "TurnOffStuff" which turns off screen updating and calculations but this did not help. Please can you suggest any improvements to make this quicker?

many thanks for your help!

VBA Code:
Private Sub CommandButton1_Click()
If CommandButton1.Caption = "Hide" Then
Sub TurnOffStuff()
Dim c As Range
For Each c In Range("D8:D267")
If c.Value = "" Then c.EntireRow.Hidden = True
Sub TurnOnStuff()

Next c
CommandButton1.Caption = "Show"
Sub TurnOffStuff()

ElseIf CommandButton1.Caption = "Show" Then
Rows("8:267").EntireRow.Hidden = False
CommandButton1.Caption = "Hide"
Sub TurnOnStuff()

End If
End Sub

These are my helper macros:

VBA Code:
Sub TurnOffStuff()
Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False
Application.EnableEvents = False
End Sub

VBA Code:
Sub TurnOnStuff()
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
Application.EnableEvents = True
End Sub
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Hi & welcome to MrExcel.
Does column D contain formulae?
 
Upvote 0
Ok, how about
VBA Code:
Private Sub CommandButton1_Click()
   Dim c As Range, Rng As Range
   
   Sub TurnOffStuff()
   If CommandButton1.Caption = "Hide" Then
      For Each c In Range("D8:D267")
         If c.Value = "" Then
            If Rng Is Nothing Then Set Rng = c Else Set Rng = Union(Rng, c)
         End If
      Next c
      If Not Rng Is Nothing Then Rng.EntireRow.Hidden = True
   ElseIf CommandButton1.Caption = "Show" Then
      Rows("8:267").EntireRow.Hidden = False
      CommandButton1.Caption = "Hide"
   End If
   Sub TurnOnStuff()
End Sub
 
Upvote 0
Solution
Hi Fluff,
Thanks for the quick reply!

The code works instantly to hide the blank rows, however the button does not revert to "Show" once the rows are hidden so I cannot unhide them.

VBA Code:
Private Sub CommandButton1_Click()
   Dim c As Range, Rng As Range
   
   Call TurnOffStuff
   If CommandButton1.Caption = "Hide" Then
      For Each c In Range("D8:D267")
         If c.Value = "" Then
            If Rng Is Nothing Then Set Rng = c Else Set Rng = Union(Rng, c)
         End If
      Next c
      If Not Rng Is Nothing Then Rng.EntireRow.Hidden = True
   ElseIf CommandButton1.Caption = "Show" Then
      Rows("8:267").EntireRow.Hidden = False
      CommandButton1.Caption = "Hide"
   End If
   Call TurnOnStuff
End Sub
 
Upvote 0
You need to add
VBA Code:
CommandButton1.Caption = "Show"
after the Next c line
 
Upvote 0
Glad to help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,176
Members
453,021
Latest member
Justyna P

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