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

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
63,974
Office Version
  1. 365
Platform
  1. Windows
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
 
Solution

BobH

New Member
Joined
Feb 18, 2020
Messages
14
Office Version
  1. 2019
Platform
  1. Windows
Thank you works perfect Cheers Rob
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
63,974
Office Version
  1. 365
Platform
  1. Windows
You're welcome & thanks for the feedback.
 

Forum statistics

Threads
1,144,342
Messages
5,723,821
Members
422,518
Latest member
quack_quack

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
Top