+ Reply to Thread
Results 1 to 7 of 7

Thread: Button to hide and unhide rows

  1. #1
    Valued Forum Contributor
    Join Date
    07-10-2008
    Location
    UK
    Posts
    219

    Button to hide and unhide rows

    Good morning everyone

    I need a button which, when clicked will hide rows 6&7. Also when its clicked the caption needs to change to 'Show Examples'.

    After the rows have been hidden, when the button is clicked again the rows must become visible and the caption change back to 'Hide Examples'.

    Obviously this should be looped so it can be clicked unlimited times.

    Any ideas?

  2. #2
    Valued Forum Contributor Stuie's Avatar
    Join Date
    09-17-2006
    Location
    Suffolk, UK
    MS-Off Ver
    Excel 2003/2007
    Posts
    432
    Hi twofootgiant,

    Try this code:

    Private Sub btn1_Click()
    
    Select Case btn1.Caption
        Case "Hide Examples"
            Range("A6").EntireRow.Hidden = True
            Range("A7").EntireRow.Hidden = True
            btn1.Caption = "Show Examples"
        
        Case "Show Examples"
            Range("A6").EntireRow.Hidden = False
            Range("A7").EntireRow.Hidden = False
            btn1.Caption = "Hide Examples"
             
    End Select
    End Sub
    There are only 10 types of people in the world:
    Those who understand binary, and those who don't!

  3. #3
    Valued Forum Contributor
    Join Date
    07-10-2008
    Location
    UK
    Posts
    219

    .

    Iv sorted it using:

    If Rows("6:7").EntireRow.Hidden = False Then 
        Rows("6:7").EntireRow.Hidden = True 
    Else 
        Rows("6:7").EntireRow.Hidden = False 
    End If
    Its just the caption changing bit I need now :D

  4. #4
    Valued Forum Contributor Stuie's Avatar
    Join Date
    09-17-2006
    Location
    Suffolk, UK
    MS-Off Ver
    Excel 2003/2007
    Posts
    432
    You can use this peice of code:

    btn1.Caption =
    just change btn1 to the button name and then the text after the = that you want it to say (but the code i posted i think would be more simple to amend in the future and potensionally give less space for error)
    There are only 10 types of people in the world:
    Those who understand binary, and those who don't!

  5. #5
    Valued Forum Contributor
    Join Date
    07-10-2008
    Location
    UK
    Posts
    219
    thanks..That works perfectly

  6. #6
    Valued Forum Contributor Stuie's Avatar
    Join Date
    09-17-2006
    Location
    Suffolk, UK
    MS-Off Ver
    Excel 2003/2007
    Posts
    432
    No problem, glad i could help
    There are only 10 types of people in the world:
    Those who understand binary, and those who don't!

  7. #7
    Forum Guru, retired Admin royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    25,639
    This is a bit shorter

    Option Explicit
    
    Private Sub btn1_Click()
        Rows("6:7").EntireRow.Hidden = Not Rows("6:7").EntireRow.Hidden
        Select Case btn1.Caption
            Case "Show Examples": btn1.Caption = "Hide Examples"
            Case "Hide Examples": btn1.Caption = "Show Examples"
        End Select
    End Sub
    Hope that helps.

    RoyUK
    --------
    If you are pleased with a member's answer then use the Star icon to rate it, if you are pleased enough to part with cash consider a donation to Children in Need

    For Excel Tips & Solutions, free examples and tutorials why not check out my downloads

    New members please read & follow the Forum Rules

    Remember to mark your questions Solved and rate the answer(s)

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.2.0