Consulting

Page 1 of 2 1 2 LastLast
Results 1 to 20 of 22

Thread: Need help with hiding row under the following circumstances

  1. #1

    Need help with hiding row under the following circumstances

    Under the condition if a cell is NOT read "yes", then I want to hide the entire row that cell is on. For instance, if cell k3 does not read "yes" then I want the whole 3rd row to be completely hidden. I recently learned that conditional formatting cannot perform this task. How can I perform this procedure? Do I have write a macro? I never written a macro before and I haven't found ONE website that tells me exactly how to do this. Supposedly it's real easy, but I need some assistence.

    I need help fast!!

    Thanks in advanced

    also, where do I write the macro. Do I go to tools -> macro -> macros, or do I write the code elsewhere?

  2. #2
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    Hi Shaolin, and Welcome to VBAX!

    I don't have time to write the macro for you right at the moment, but with regards to the "exactly how to" part... Have a read through the complimentary lesson 1 of our Excel VBA Training program! It should answer the basic "where" questions for you.

    I'm also sure that someone will be along to help you shortly.

    Cheers!
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





  3. #3
    VBAX Guru
    Joined
    Mar 2005
    Posts
    3,296
    Location
    The first thing to do for yourself is to record a macro of hiding a row, that will give you the code for hiding a row.

  4. #4
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    And if you want a starter macro for you, just make sure you say the range that you want to check.

    Such as you want to check A1:A10, if any cell value in this range is "yes", then hide the whole row, you could do this a few ways. Let's look at the following ...

    'Ensure variable declaration

    Option Explicit
    'Test routine #1
    'Union (fast)
    
    Sub HideRows_Test1()
    'Declare variables.
        Dim rngCheck As Range, cel As Range, uCel As Range
    'Set range to check for condition ("yes").
        Set rngCheck = Range("A1:A10")
    'Loop through each cell in the set range.
        For Each cel In rngCheck
    'Perform condition check on individual cells.
            If cel.Value = "yes" Then
    'If cells match condition, check if Union exists.
                If uCel Is Nothing Then
    'If it doesn't exist, create it.
                    Set uCel = cel
                Else
    'If it does exist, add to it.
                    Set uCel = Union(uCel, cel)
                End If
    End If
        Next cel
    'Hide all Unioned cells here.
        uCel.EntireRow.Hidden = True
    End Sub
    
    'Test routine #2
    'Standard Loop (faster)
    Sub HideRows_Test2()
    'Declare variables.
        Dim rngCheck As Range, cel As Range
    'Set range to check for condition ("yes").
        Set rngCheck = Range("A1:A10")
    'Loop through each cell in the set range.
        For Each cel In rngCheck
    'Perform If/Then condition check.  If true, perform action.
            If cel.Value = "yes" Then cel.EntireRow.Hidden = True
        Next cel
    End Sub
    'Test routine #3
    'Array check (fastest)

    Sub HideRows_Test3()
    'Declare variables.
        Dim i As Long, celArr()
    'Set range to check for condition ("yes").
        celArr = Range("A1:A10")
    'Loop through each variable in the array.
        For i = LBound(celArr) To UBound(celArr) Step 1
    'Perform If/Then condition check.  If true, perform action.
            If celArr(i, 1) = "yes" Then Range("A" & i).EntireRow.Hidden = True
        Next i
    End Sub

    Hope This Helps

  5. #5
    Thanks for the responses!


    Oh, in addition, the macro that I pasted in the previous post (I'll post it again):

    Sub Hidden()
    ' Hidden Macro
    ' Macro recorded 4/13/2005 by pdp5
    Selection.EntireRow.Hidden = True
    Range("A8").Select
    End Sub
    Is it possible to write the code so that it goes through the whole K column (cell by cell if it has to), and wherever it sees anything other than "Yes" in the K column, it hides the respective rows those cells are on?

    I guess that would be the tricky part of the code. Maybe most of the people here are guru's or something.

  6. #6
    VBAX Guru
    Joined
    Mar 2005
    Posts
    3,296
    Location
    The other questio to ask yourself is how do you activate it by Macro Button, Automaticaly?
    If automaticall when, on start up on cell changes?

  7. #7
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    You could adapt one of the examples above to something like this ...

    Sub HideRows_Test4() 
        Dim rngCheck As Range, cel As Range 
        Set rngCheck = Range("K:K").SpecialCells(xlCellTypeConstants, 23)
        For Each cel In rngCheck 
            If Lcase(cel.Value) <> "yes" Then cel.EntireRow.Hidden = True 
        Next cel 
    End Sub
    Now this does the opposite of that which I posted above, in the fact that if the cell value is NOT equal to any form of "yes" then that row will be hidden. If this is the opposite of that which you would prefer, replace the 'not equal to' signs ( <> ) with an 'equal to' sign ( = ).


    HTH

  8. #8
    Thanks a lot

    One quick question

    If I wanted to place the range starting from K3 to the end of the K column, what do I type in the code?

    Range ("k3:k")??

  9. #9
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    Range("K3", Range("K65536").End(xlUp))
    .. or ..
    Range("K3:K" & Range("K65536").End(xlUp).Row)
    .. or ..
    Range("K3:K65536").SpecialCells(xlCellTypeConstants, 23)

  10. #10
    thanks . . . I'll see if it runs in a few . . .

  11. #11
    Why 23?

    [VBA]Range("K3:K65536").SpecialCells(xlCellTypeConstants, 23) [/VBA]

  12. #12
    BoardCoder
    Licensed Coder
    VBAX Expert mark007's Avatar
    Joined
    May 2004
    Location
    Leeds, UK
    Posts
    622
    Location
    You should use the autofilter to hide the rows - much faster than looping through each one
    "Computers are useless. They can only give you answers." - Pablo Picasso
    Mark Rowlinson FIA | The Code Net

  13. #13
    VBAX Contributor
    Joined
    Dec 2004
    Posts
    122
    Location

    try this

    Sub Hide_rows()
    Range("k1").Select
    Do Until ActiveCell.Value = ""
    If ActiveCell.Value = "no" Then 'this may have to be in upper or lower case
    Selection.EntireRow.Hidden = True
    ActiveCell.Offset(1, 0).Activate
    Else
    ActiveCell.Offset(1, 0).Activate
    End If
    Loop
    End Sub

  14. #14
    does this make sense to any of you?


    If LCase(cel.Value) = "yes" Then cel.EntireRow.RowHeight = 12.75

    What I'm basically trying to do is when the cell value reads "yes" I want the row height to be 12.75 or 17 pixels.

  15. #15
    BoardCoder
    Licensed Coder VBAX Expert mark007's Avatar
    Joined
    May 2004
    Location
    Leeds, UK
    Posts
    622
    Location
    To hide and resize your rows use:

    Sub HideNonYes()
        Application.ScreenUpdating = False
    Dim r As Range
        Dim ws As Worksheet
        Set ws = ActiveSheet
        ws.Rows(1).AutoFilter
        ws.Rows(1).AutoFilter Field:=11, Criteria1:="=yes"
        Set r = Application.Intersect(ws.Cells.SpecialCells(xlCellTypeVisible), ws.UsedRange.Offset(1, 0))
        r.EntireRow.RowHeight = 12.75
        ws.Rows(1).AutoFilter Field:=11, Criteria1:="<>yes"
        Set r = Application.Intersect(ws.Cells.SpecialCells(xlCellTypeVisible), ws.UsedRange.Offset(1, 0))
        ws.AutoFilterMode = False
        r.EntireRow.Hidden = True
    Application.ScreenUpdating = True
    End Sub

    Should run much faster than the loops above.

    Gsouza, you should avoid selecting and activating. Your code can be rewritten as:

    Sub Hide_rows() 
    dim r as range
    dim i as long
    i=1
        set r=activesheet.Range("k1")
        Do Until r.offset(i,0).Value = "" 
            If r.offset(i,0).Value = "no" Then 'this may have to be in upper or lower case
                r.offset(i,0).EntireRow.Hidden = True 
                End If 
    i=i+1
        Loop 
    End Sub
    Hope that helps.

    "Computers are useless. They can only give you answers." - Pablo Picasso
    Mark Rowlinson FIA | The Code Net

  16. #16
    VBAX Contributor
    Joined
    Dec 2004
    Posts
    122
    Location
    Thanks 007 I can just never seem to remember all that short cut stuff. But it sure is alot neater and faster. Thanks for the tip. By the way, Shaolin, for someone that never wrote a macro your catching on fast.

  17. #17
    BoardCoder
    Licensed Coder VBAX Expert mark007's Avatar
    Joined
    May 2004
    Location
    Leeds, UK
    Posts
    622
    Location
    No problem gsouza, it's how we all start!

    "Computers are useless. They can only give you answers." - Pablo Picasso
    Mark Rowlinson FIA | The Code Net

  18. #18
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    23: Because there are four different types of Constants we use with this (SpecialCells) method when referring to a cell with values. Each is associated with a Hardcoded constant number. So instead of writing it out, you can use numbers as expressions for which type of constant you would like to make use of. 23 is just the accumulation (you can add them) of all 4 types of constants.

    A good (comprehensive) list can be found right here. At the bottom there is a link to a zipped file called XL97Constants.zip. This contains a sheet named "MS Excel 97 Constants", from there you can sort either alphabetically or numerically, whichever you prefer.

  19. #19
    BoardCoder
    Licensed Coder VBAX Expert mark007's Avatar
    Joined
    May 2004
    Location
    Leeds, UK
    Posts
    622
    Location
    Alternatively you can look them up in the object browser in the VBIDE. (Hit F2)

    "Computers are useless. They can only give you answers." - Pablo Picasso
    Mark Rowlinson FIA | The Code Net

  20. #20
    Thanks a lot folks. I appreciate it. I was too busy with other stuff at work and didn't have the time to show my appreciation. I'll mos def check those links.

    Anyway, which VBA book do you recommend and which is the best bang for the buck?

Posting Permissions

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