View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.programming
cheetah[_2_] cheetah[_2_] is offline
external usenet poster
 
Posts: 1
Default code to hide/unhide rows

i'm looking for a way to create a button in excell that can be placed within
a spreadsheet to control the visiblity of a fixed number of rows. i
currently have the following code in place:

Private Sub CommandButton1_Click()
Dim myRng As Range

Set myRng = Me.Range("a2:a10")

myRng.EntireRow.Hidden = Not (myRng(1).EntireRow.Hidden)

End Sub

Private Sub CommandButton2_Click()
Dim myRng As Range

Set myRng = Me.Range("a12:a20")

myRng.EntireRow.Hidden = Not (myRng(1).EntireRow.Hidden)

End Sub

I am not VB savvy in the slightest sense and thankfully found this code
online, but I ran into a problem once a row is inserted between rows
containing the command button. Example:

Row 1 has Command Button 1 that controls the hiding/unhiding of rows 2-10
Row 11 has a Command Button 2 that controls the hiding/unhiding of rows 12-20

I insert a row above Row 11 (Command Button 2 shifts along with the row,
which is ideal), but by doing so the range in the VB code does not adjust to
the downward shift, causing Command Button 2's code to pertain to the wrong
range now that the button is in Row 12.

Is there a way to create a hide/unhide button with coding that adjusts it's
range after it's commanding button is shifted into a different row?

- cheetah