Toggle Button - hide / show rows

mark9988

Board Regular
Joined
Sep 30, 2005
Messages
90
Hello -

The code below works fine for hiding/unhiding a small number of rows. However, the macro takes a very long to process when there are several rows. Is there way to modify this code to improve its efficiency?

Thanks,


Private Sub ToggleButton2_Click()
'mark9988

Dim xAddress As String
Dim splitAddress As Variant
xAddress = "1,2,3,4,5,6,7,8,9,10" 'change this to the row numbers
splitAddress = Split(xAddress, ",")


If ToggleButton2.Value Then
For Each Var In splitAddress
Application.ActiveSheet.Rows(Var).Hidden = True
ToggleButton2.Caption = "Show Rows"
Next Var

Else
For Each Var In splitAddress
Application.ActiveSheet.Rows(Var).Hidden = False
ToggleButton2.Caption = "Hide Rows"
Next Var

End If
End Sub
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
How about
VBA Code:
Private Sub ToggleButton2_Click()
'mark9988

Rows("1:10").Hidden = Not Rows("1:10").Hidden
With ToggleButton2
   .Caption = IIf(Left(.Caption, 4) = "Show", "Hide Rows", "Show Rows")
End With
End Sub
 
Upvote 0
Solution
How about
VBA Code:
Private Sub ToggleButton2_Click()
'mark9988

Rows("1:10").Hidden = Not Rows("1:10").Hidden
With ToggleButton2
   .Caption = IIf(Left(.Caption, 4) = "Show", "Hide Rows", "Show Rows")
End With
End Sub
Excellent! Thank you!
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,239
Members
452,621
Latest member
Laura_PinksBTHFT

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