Consulting

Results 1 to 10 of 10

Thread: VBA To Hide or Show Rows

  1. #1
    VBAX Regular
    Joined
    Dec 2016
    Posts
    10
    Location

    VBA To Hide or Show Rows

    Hi All,

    Thanks in advance for the help.

    I need to write a macro to do two things. My goal is to have it run so that if you click the button all cells in range G9:G125 that are equal to 0, their corresponding rows will hide. When you click the button again, all rows from 9:125 will show.

    Not sure if that is even possible, but I thought I'd give it a shot.

    Thanks again

  2. #2
    VBAX Expert
    Joined
    Aug 2004
    Posts
    810
    Location
    give this a try
    create a button and leave the control name as CommandButton1
    Sub Toggle()
        Dim rRange As Range
        Dim rCell As Range
        Dim lRow As Long
        
        Application.ScreenUpdating = False
        Set rRange = Range("G9:G125")
        For Each rCell In rRange
            If rCell = 0 And Sheet1.CommandButton1.Caption = "Hide" Then
                rCell.EntireRow.Hidden = True
            Else
                rCell.EntireRow.Hidden = False
            End If
        Next rCell
        
        If Sheet1.CommandButton1.Caption = "Hide" Then
            Sheet1.CommandButton1.Caption = "Show"
        Else
            Sheet1.CommandButton1.Caption = "Hide"
        End If
        Application.ScreenUpdating = True
    End Sub
    by the way, first time you run it, probably "won't" do anything, the second click will actually trigger the macro correctly. This is because your button caption may not be setup properly, unless you edit the button first...

  3. #3
    VBAX Regular
    Joined
    Dec 2016
    Posts
    10
    Location
    Hi JKwan

    Thanks for responding but unfortunately it didn't work. I'm not too familiar with writing the code, so I can make any tweaks. Here's what I did, and maybe you can offer some insight:

    -Inserted an ActiveX Control Button
    -Right Click on button and selected 'View Code'
    -Pasted above code into module
    -Saved and closed out of VBA window
    -Clicked on the button
    -It did not work, Excel opened up VBA window and a pop up showed Compile Error: Expected End Sub

    I know I'm asking a lot to have you walk me through this, so I thank you very much. I really appreciate it.

  4. #4
    VBAX Expert
    Joined
    Aug 2004
    Posts
    810
    Location
    Here is the complete work
    Attached Files Attached Files

  5. #5
    VBAX Regular
    Joined
    Dec 2016
    Posts
    10
    Location
    JKwan,

    Your file is perfect and exactly what I am looking for. Unfortunately, when I try and put that into my file, it does not work. I've tried a few different ways to 'copy and paste', but nothing has worked. Do you have any thoughts?

    Thanks again in advance.

  6. #6
    VBAX Expert
    Joined
    Aug 2004
    Posts
    810
    Location
    Well, I guess you did it incorrectly.... If the data is not sensitive in nature, you can attach it and I can put the code into the sheet for you.

  7. #7
    VBAX Regular
    Joined
    Dec 2016
    Posts
    10
    Location
    Hello Again,

    I put this on the shelf for a bit, but now I'm working on this again. I still cannot seem to make it work, even though your file worked perfectly. I have attached a sample of what the sheet looks like.

    Any help would be much appreciated.

    Thanks!
    Attached Files Attached Files

  8. #8
    VBAX Expert
    Joined
    Aug 2004
    Posts
    810
    Location
    give this a spin and see.
    Attached Files Attached Files

  9. #9
    VBAX Regular
    Joined
    Dec 2016
    Posts
    10
    Location
    That is perfect. You are a life saver!

  10. #10
    VBAX Expert
    Joined
    Aug 2004
    Posts
    810
    Location
    Glad I can help

Posting Permissions

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