Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    5 Star Lounger
    Join Date
    Oct 2001
    Location
    San Bernardino, California, USA
    Posts
    734
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I have a worksheet where I have manually hidden some rows if the value if column A (A1, a2, a3, etc) =0. Now I have created two buttons with assigned macros so I can unhide these rows or hide the rows. The code is below and it works fine.

    Sub Unhide()
    Rows("5:30").EntireRow.Hidden = False
    End Sub

    Sub hide()
    Dim i As Integer
    Application.ScreenUpdating = False
    For i = 5 To 30
    If Sheets("planning").Range("A" & i).Value = "0" Then
    Rows(i).EntireRow.Hidden = True
    End If
    Next i
    Application.ScreenUpdating = True
    End Sub

    ------
    I would like to be able to combine both of these actions into the same button as a toggle which would mean changing the assigned macro or running code based on the value of a flag as well as changing the text of that button depending on the value of that flag - something like click to unhide or click to hide.

    However, I don't know how to do this. Any advice or ideas?

  2. #2
    WS Lounge VIP rory's Avatar
    Join Date
    Dec 2000
    Location
    Burwash, East Sussex, United Kingdom
    Posts
    6,280
    Thanks
    3
    Thanked 191 Times in 177 Posts
    Is there a particular reason you aren't just using an autofilter, as a matter of interest?
    Regards,
    Rory

    Microsoft MVP - Excel

  3. #3
    5 Star Lounger
    Join Date
    Oct 2001
    Location
    San Bernardino, California, USA
    Posts
    734
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I had to correct my posting. Autofilter will work however, I wanted a slicker interface because I will likely be adding more to the macro/code later. - such as copying a range to the clipboard.

    [quote name='rory' post='766843' date='23-Mar-2009 08:55']Is there a particular reason you aren't just using an autofilter, as a matter of interest?[/quote]

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts
    You could use code like this, substituting the correct name of the button:
    Code:
    Sub ToggleHide()
      Dim wsh As Worksheet
      Dim btn As Shape
      Dim i As Long
      Set wsh = Worksheets("Planning")
      Set btn = wsh.Shapes("Button 1")
      Application.ScreenUpdating = False
      If btn.TextFrame.Characters.Text = "Hide" Then
    	For i = 5 To 30
    	  If wsh.Range("A" & i).Value = "0" Then
    		wsh.Rows(i).Hidden = True
    	  End If
    	Next i
    	btn.TextFrame.Characters.Text = "Unhide"
      Else
    	wsh.Range("A5:A30").EntireRow.Hidden = False
    	btn.TextFrame.Characters.Text = "Hide"
      End If
      Application.ScreenUpdating = True
    End Sub

  5. #5
    5 Star Lounger
    Join Date
    Oct 2001
    Location
    San Bernardino, California, USA
    Posts
    734
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks, This is perfect.
    [quote name='HansV' post='766987' date='24-Mar-2009 02:30']You could use code like this, substituting the correct name of the button:
    Code:
    Sub ToggleHide()
      Dim wsh As Worksheet
      Dim btn As Shape
      Dim i As Long
      Set wsh = Worksheets("Planning")
      Set btn = wsh.Shapes("Button 1")
      Application.ScreenUpdating = False
      If btn.TextFrame.Characters.Text = "Hide" Then
    [tab][/tab]For i = 5 To 30
    [tab][/tab]  If wsh.Range("A" & i).Value = "0" Then
    [tab][/tab][tab][/tab]wsh.Rows(i).Hidden = True
    [tab][/tab]  End If
    [tab][/tab]Next i
    [tab][/tab]btn.TextFrame.Characters.Text = "Unhide"
      Else
    [tab][/tab]wsh.Range("A5:A30").EntireRow.Hidden = False
    [tab][/tab]btn.TextFrame.Characters.Text = "Hide"
      End If
      Application.ScreenUpdating = True
    End Sub
    [/quote]

  6. #6
    5 Star Lounger
    Join Date
    Oct 2001
    Location
    San Bernardino, California, USA
    Posts
    734
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I thought it was perfect until I encountered an unanticipated condition. Now I realize I need to hide the row ONLY IF the value of A, D, & G = 0.

    Can I insert the AND (X,Y,Z) function into VB?

    Something like AND (A1=0, D1=0, G1=0)




    [quote name='Don_Sadler' post='767466' date='25-Mar-2009 22:54']Thanks, This is perfect.[/quote]

  7. #7
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts
    You can change the line

    If wsh.Range("A" & i).Value = "0" Then

    to

    If wsh.Range("A" & i).Value = "0" And wsh.Range("D" & i).Value = "0" And wsh.Range("G" & i).Value = "0" Then

  8. #8
    5 Star Lounger
    Join Date
    Oct 2001
    Location
    San Bernardino, California, USA
    Posts
    734
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you - I was trying something similar but it wasn't working.

    [quote name='HansV' post='768015' date='28-Mar-2009 11:42']You can change the line

    If wsh.Range("A" & i).Value = "0" Then

    to

    If wsh.Range("A" & i).Value = "0" And wsh.Range("D" & i).Value = "0" And wsh.Range("G" & i).Value = "0" Then[/quote]

  9. #9
    5 Star Lounger
    Join Date
    Oct 2001
    Location
    San Bernardino, California, USA
    Posts
    734
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I know I have seen this before but cannot find it. How do I put the cursor in a particular cell after completion of a macro? In other words, after the code has run, go to K2 then endsub.

    [quote name='Don_Sadler' post='768017' date='28-Mar-2009 11:54']Thank you - I was trying something similar but it wasn't working.[/quote]

  10. #10
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts
    Add the line

    Range("K2").Select

  11. #11
    5 Star Lounger
    Join Date
    Oct 2001
    Location
    San Bernardino, California, USA
    Posts
    734
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks Hans.

    [quote name='HansV' post='768064' date='28-Mar-2009 23:49']Add the line

    Range("K2").Select[/quote]

  12. #12
    5 Star Lounger
    Join Date
    Oct 2001
    Location
    San Bernardino, California, USA
    Posts
    734
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Sorry to keep coming back but I find that when I protect the sheet, I am unable to select or click on the button object.

    If I allow selecting locked and unlocked cells, then I can select and click the button but I get an error as it tries to run the macro. The error is

    'Unable to set the Hidden property of the Range class'

    Any way to lock the sheet yet still be able to click the button and run the macro?

    [quote name='Don_Sadler' post='768179' date='29-Mar-2009 18:44']Thanks Hans.[/quote]

  13. #13
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts
    If you tick the check box "Edit Objects" in the Protect Sheet dialog, the user will be able to click the button.

    To make it possible to hide or unhide rows, you can add a line

    ActiveSheet.Unprotect

    at the beginning of the macro, and a line

    ActiveSheet.Protect

    at the end. If you set a password, you can add this to both lines, enclosed in quotes:

    ActiveSheet.Unprotect "MyPassword"
    ...
    Activesheet.Protect "MyPassword"

  14. #14
    5 Star Lounger
    Join Date
    Oct 2001
    Location
    San Bernardino, California, USA
    Posts
    734
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Do you mean to literally type "MyPassword" or to actually have the password in quotes?

    [quote name='HansV' post='768335' date='30-Mar-2009 12:43']If you tick the check box "Edit Objects" in the Protect Sheet dialog, the user will be able to click the button.

    To make it possible to hide or unhide rows, you can add a line

    ActiveSheet.Unprotect

    at the beginning of the macro, and a line

    ActiveSheet.Protect

    at the end. If you set a password, you can add this to both lines, enclosed in quotes:

    ActiveSheet.Unprotect "MyPassword"
    ...
    Activesheet.Protect "MyPassword"[/quote]

  15. #15
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts
    [quote name='Don_Sadler' post='768338' date='30-Mar-2009 21:49']Do you mean to literally type "MyPassword" or to actually have the password in quotes?[/quote]
    You should replace MyPassword with the actual password, and enclose it in quotes. So if your password is reldaS_noD:

    ActiveSheet.Protect "reldaS_noD"

Page 1 of 2 12 LastLast

Posting Permissions

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