+ Reply to Thread
Results 1 to 9 of 9

Hide/Unhide row based on cell value

Hybrid View

  1. #1
    Registered User
    Join Date
    07-10-2019
    Location
    Nova Scotia, Canada
    MS-Off Ver
    2013
    Posts
    4

    Hide/Unhide row based on cell value

    Hi, I am very new to writing VBA so for many of you this will most likely be a very simple question. I want to hide/unhide rows based on whether or not a cell have a value in it.
    If L18 in blank then Row 171 is hided; if L19 is blank then Row 172 is hidden and so on to L168. I have it working for one cell/Row but how do I set it to do this for the range?
    Here is what I have:

    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    
    Application.ScreenUpdating = False
    
    Select Case Range("L18").Value = ""
    
    Case True: Rows("171").Hidden = True
    
    Case False: Rows("171").Hidden = False
    
    End Select
    
    End Sub
    Last edited by jeffreybrown; 07-11-2019 at 12:46 PM. Reason: Please use code tags!

  2. #2
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2404
    Posts
    24,866

    Re: Hide/Unhide row based on cell value

    Welcome to the Forum TrenchRT!

    I would do it with a loop. Also, Select Case is overkill here. And if you turn off screen updating, remember to turn it back on.
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
       
       Dim R As Long ' row
       
       Application.ScreenUpdating = False
       
       For R = 18 To 168
          Rows(R + 153).Hidden = (Cells(R, "L") = "")
       Next R
    
       Application.ScreenUpdating = True
    
    
    End Sub
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

  3. #3
    Valued Forum Contributor dmcgov's Avatar
    Join Date
    11-11-2015
    Location
    Florida, USA
    MS-Off Ver
    Office 365 Business
    Posts
    1,518

    Re: Hide/Unhide row based on cell value

    this solution will work as well:

    Private Sub Worksheet_Change(ByVal Target As Range)
        Dim wb     As Workbook
        Dim ws     As Worksheet
        Set wb = ThisWorkbook
        Set ws = wb.Sheets("Sheet3")
        Dim keycells As Range
        Set keycells = ws.Range("L18:L168")
        Dim z      As Long
        
            With ws
            If Not Intersect(keycells, Range(Target.Address)) Then
                If Target.Value = "" Then
                    'hide row
                    z = 153 + Split(Target.Address, "$")(2)
                    Rows(z).Hidden = True
                Else
                    'unhide row
                    z = 153 + Split(Target.Address, "$")(2)
                    Rows(z).Hidden = False
                End If
            End If
        End With
    End Sub
    more code that what 6stringjazzer has but maybe this will work for you better.

    so copy this and put it in the sheet code (sheet1, sheet2, etc)
    Last edited by dmcgov; 07-11-2019 at 01:26 PM. Reason: optimized code

  4. #4
    Registered User
    Join Date
    07-10-2019
    Location
    Nova Scotia, Canada
    MS-Off Ver
    2013
    Posts
    4

    Re: Hide/Unhide row based on cell value

    Wow you guys are awesome! And thanks for the welcome.
    the solution from 6StringJazzer works but when I run the solution from dmcgov I get

    Run-time error "13"
    Type mismatch

    and debugger highlights on this line

    If Not Intersect(keycells, Range(Target.Address)) Then

  5. #5
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2404
    Posts
    24,866

    Re: Hide/Unhide row based on cell value

    Quote Originally Posted by TrenchRT View Post
    when I run the solution from dmcgov I get

    Run-time error "13"
    Type mismatch

    and debugger highlights on this line

    If Not Intersect(keycells, Range(Target.Address)) Then
    It should be
    If Not Intersect(keycells, Target) Then

  6. #6
    Valued Forum Contributor dmcgov's Avatar
    Join Date
    11-11-2015
    Location
    Florida, USA
    MS-Off Ver
    Office 365 Business
    Posts
    1,518

    Re: Hide/Unhide row based on cell value

    it should be

    If Not Intersect(keycells, Target) Is Nothing Then

  7. #7
    Registered User
    Join Date
    07-10-2019
    Location
    Nova Scotia, Canada
    MS-Off Ver
    2013
    Posts
    4

    Re: Hide/Unhide row based on cell value

    Thanks again guys, this is working great!

  8. #8
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2404
    Posts
    24,866

    Re: Hide/Unhide row based on cell value

    Quote Originally Posted by TrenchRT View Post
    Thanks again guys, this is working great!
    If your question has been answered please mark your thread as "Solved" so that members will know by looking at the thread title that your problem is solved. Go to the menu immediately above your first post to the thread and click on Thread Tools. From the dropdown menu select "Mark this thread as solved..."

    If a member helped you solve your problem, consider adding to their reputation by clicking on the star icon addreputationiconsmall.jpg below their name.

  9. #9
    Registered User
    Join Date
    07-10-2019
    Location
    Nova Scotia, Canada
    MS-Off Ver
    2013
    Posts
    4

    Re: Hide/Unhide row based on cell value

    Done, great solution from a great team!

+ 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. [SOLVED] Auto hide/unhide based on cell value
    By erice in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 02-09-2018, 04:35 PM
  2. How to Hide-Unhide Row Based on a Cell Value
    By putritersenyum in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 04-18-2015, 06:56 AM
  3. [SOLVED] Hide and unhide text box based on a cell value - 2
    By Rocky2013 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-27-2015, 06:59 PM
  4. [SOLVED] VBA Code to hide and unhide based on a cell value
    By Silver13 in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 03-23-2015, 03:00 PM
  5. [SOLVED] Unhide and Hide row(s) based on value in a cell in the row above
    By jammi1710 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 01-18-2014, 01:27 AM
  6. Hide & Unhide columns based on a cell value
    By DaveNUFC in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 02-21-2010, 09:40 AM
  7. hide/unhide rows based on cell value
    By alexandruc in forum Excel - New Users/Basics
    Replies: 2
    Last Post: 10-12-2009, 06:49 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