Using Command Button to hide & unhide selected Rows

BobH

New Member
Joined
Feb 18, 2020
Messages
14
Office Version
  1. 2019
Platform
  1. Windows
Hi I am trying to use a command button to hide a small range of rows if they are blank and the same button to unhide I am struggling with the code below would appreciate advice on this. The code i have so far is as follows Many Thanks Rob
VBA Code:
Private Sub CommandButton1_Click()
If CommandButton1.Caption = "Hide" Then
Dim c As Range
For Each c In Range("B4:b72")
If c.Value = "" Then
   c.EntireRow.Hidden = True
   CommandButton1.Caption = "Show"
   
Else

If CommandButton1.Caption = "Show" Then
 Rows("3:74").Select
    Selection.EntireRow.Hidden = False
   CommandButton1.Caption = "Hide"
Else
End If

End If
Next
End If
End Sub
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
How about
VBA Code:
Private Sub CommandButton1_Click()
If CommandButton1.Caption = "Hide" Then
   Dim c As Range
   For Each c In Range("B4:b72")
      If c.Value = "" Then c.EntireRow.Hidden = True
   Next c
   CommandButton1.Caption = "Show"
   
ElseIf CommandButton1.Caption = "Show" Then
   Rows("3:74").EntireRow.Hidden = False
   CommandButton1.Caption = "Hide"
End If
End Sub
 
Upvote 0
Solution
You're welcome & thanks for the feedback.
 
Upvote 0
How about
VBA Code:
Private Sub CommandButton1_Click()
If CommandButton1.Caption = "Hide" Then
   Dim c As Range
   For Each c In Range("B4:b72")
      If c.Value = "" Then c.EntireRow.Hidden = True
   Next c
   CommandButton1.Caption = "Show"
  
ElseIf CommandButton1.Caption = "Show" Then
   Rows("3:74").EntireRow.Hidden = False
   CommandButton1.Caption = "Hide"
End If
End Sub
Hi!

I have used your code 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!

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


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



This is your code I amended to my project.

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
 
Upvote 0

Forum statistics

Threads
1,224,817
Messages
6,181,144
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