Unhide rows one by one using command button

wanders

New Member
Joined
Aug 10, 2022
Messages
6
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
I am trying to use a command button to unhide rows within my range. I have 8 command buttons each for a different range/table. Currently this button is unhiding all rows in all ranges. Not sure what I have done wrong here.


VBA Code:
Private Sub CommandButton1_Click()
 
    Dim i As Integer

    i = 18
    For i = 18 To 41
   Do
        If Rows(i).Hidden = True Then
        Rows(i).Hidden = False
        Exit Do
        ElseIf Rows(i).Hidden = False Then
        i = i + 1
        Else
    End If
    Loop Until i = 41
    
 If i = 41 Then
 MsgBox "There are no more rows for hourly labor remaining"
 Else
 End If
    Cancel = True
    
    Next i
End Sub
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
here is a picture of my spreadsheet!
1660170955275.png
 
Upvote 0
@wanders
Welcome to MrExcel
Since you have multiple command button that do the same thing then you can use a sub (i.e "toUnhide") and call it from each command button, like this:
VBA Code:
Sub toUnhide(a As Long, b As Long, msg As String)

    Dim i As Long
    
    If Rows(b).Hidden = False Then MsgBox msg: Exit Sub
    
    For i = a To b
        If Rows(i).Hidden = True Then Rows(i).Hidden = False:  Exit For
    Next i

End Sub

Sub CommandButton1_Click()

toUnhide 18, 21, "There are no more rows for hourly labor remaining"

End Sub

Sub CommandButton2_Click()

toUnhide 44, 65, "There are no more rows for ...."

End Sub

I've written the code for the first 2 commandbutton, you can do the rest.
The arguments are: the first row, the last row, and the message.
 
Upvote 0

Forum statistics

Threads
1,215,424
Messages
6,124,817
Members
449,190
Latest member
rscraig11

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