+ Reply to Thread
Results 1 to 21 of 21

Specific macro or VBA code to hide and unhide rows

Hybrid View

  1. #1
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Specific macro or VBA code to hide and unhide rows

    Hi louvaek

    Welcome to the Forum!!!

    Here's another approach
    Option Explicit
    
    Private Sub Worksheet_Change(ByVal Target As Range)
      Dim OldValue As String, NewValue As String
      If Target.Cells.Count > 1 Then Exit Sub
      If Not Target.Column = 2 Then Exit Sub
      With Application
        .ScreenUpdating = False
        .EnableEvents = False
        .Undo
        OldValue = Target.Value  'store old value
        If Not OldValue = "" Then
          .Undo
          .ScreenUpdating = True
          .EnableEvents = True
          Exit Sub
        Else
          .Undo
          NewValue = Target.Value     'store new value
          Range(Target.Address).Offset(1, 0).EntireRow.Hidden = False
          .EnableEvents = True
          .ScreenUpdating = True
        End If
      End With
    End Sub
    John

    If you have issues with Code I've provided, I appreciate your feedback.

    In the event Code provided resolves your issue, please mark your Thread as SOLVED.

    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

  2. #2
    Registered User
    Join Date
    10-17-2013
    Location
    Ardooie
    MS-Off Ver
    Excel 2007
    Posts
    67

    Re: Specific macro or VBA code to hide and unhide rows

    Hi Jaslake,

    I've tried yours but it does nothing.
    I assume it's the same way I should insert the code? (right click tab, view code, copy paste)?

    Grt
    Kristof

  3. #3
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Specific macro or VBA code to hide and unhide rows

    Hi louvaek

    Here's your File with the Code embedded...
    Attached Files Attached Files

  4. #4
    Registered User
    Join Date
    10-17-2013
    Location
    Ardooie
    MS-Off Ver
    Excel 2007
    Posts
    67

    Re: Specific macro or VBA code to hide and unhide rows

    Thanks Jaslake.

    But something doesn't work. If I add a value, a row unhides indeed. But if I delete it again, it doesn't hide again
    What's the diff. between your solution and Olly's? Which one should I use if I really have lot's of names in that list, and lot's of names that should be added by users?

    Grtz
    Kristof

  5. #5
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Specific macro or VBA code to hide and unhide rows

    Hi Kristof

    The downside of Olly's Code is that the range of Cells is fixed. If that works for you then use it.
    If Not Intersect(Target, Range("B12:B17")) Is Nothing Then
    I'll look at this
    But if I delete it again, it doesn't hide again

  6. #6
    Registered User
    Join Date
    10-17-2013
    Location
    Ardooie
    MS-Off Ver
    Excel 2007
    Posts
    67

    Re: Specific macro or VBA code to hide and unhide rows

    Well Olly's works, but I just tested something and the code missed it effect afterwards. I deleted a row (a name that should not longer be in the list) in the B3:B11 range.
    After that, the code doesn't work anymore in B12 (B13 : B17 still works). If I delete another row, B13 doesn't work anymore.

    So Olly's code is clean and simple, but should be manually changed each time I delete a row in the sheet.
    I guess the code should have something with 'row()' included? I know a lot about formula's, but I am a real VBA newbie :-)

  7. #7
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Specific macro or VBA code to hide and unhide rows

    Hi Kristof

    There's a bit of mind numbing logic going on here, see if this works as required
    Option Explicit
    
    Private Sub Worksheet_Change(ByVal Target As Range)
      Dim OldValue As String, NewValue As String
      If Target.Cells.Count > 1 Then Exit Sub
      If Not Target.Column = 2 Then Exit Sub
      On Error GoTo Reset
      With Application
        .ScreenUpdating = False
        .EnableEvents = False
        .Undo
        OldValue = Target.Value  'store old value
        If Not OldValue = "" Then
          .Undo
          If Range(Target.Address).Offset(1, 0).Value = "" _
             Or Range(Target.Address).Offset(-1, 0).Value = "" Then
            Range(Target.Address).EntireRow.Hidden = True
          End If
          .ScreenUpdating = True
          .EnableEvents = True
          Exit Sub
        Else
          .Undo
          NewValue = Target.Value     'store new value
          Range(Target.Address).Offset(1, 0).EntireRow.Hidden = False
          .EnableEvents = True
          .ScreenUpdating = True
        End If
      End With
    Reset:
      Application.ScreenUpdating = True
      Application.EnableEvents = True
    End Sub
    Attached Files Attached Files

  8. #8
    Registered User
    Join Date
    10-17-2013
    Location
    Ardooie
    MS-Off Ver
    Excel 2007
    Posts
    67

    Re: Specific macro or VBA code to hide and unhide rows

    Jaslake,

    The unhiding works perfect. If I add a value in B13, B14 shows. If I add a value in B15, B15 shows.
    But when I delete B14, B15 does not unhide, it's B14 that hides ... and B15 stays visible.

  9. #9
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Specific macro or VBA code to hide and unhide rows

    Hi Kristof

    When you say "delete", are you deleting a Row or are you clearing the contents of the Cell?

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. use macro to find and replace specific code in vba
    By hopefulhart in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-07-2014, 09:46 AM
  2. [SOLVED] Code to run a Macro on a specific day of the week at a specific time.
    By mjfox52610 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-16-2013, 11:40 PM
  3. [SOLVED] Macro code to Copy specific rows into specific columns
    By macrofan2012 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 10-11-2012, 11:24 AM
  4. vba code to apply a macro to a specific tab
    By 2seas in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 01-09-2012, 02:03 PM
  5. Assistance with Creating a Specific Macro using VBA code
    By Pbeer in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 12-17-2008, 05:35 AM

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.6.0 RC 1