Consulting

Results 1 to 6 of 6

Thread: Hide row if ?

  1. #1
    VBAX Regular
    Joined
    Mar 2010
    Posts
    49
    Location

    Hide row if ?

    Hi, I have the following code below and it work in one row. I would like to knoe if it 's possibe were I have 2 rows with the following senario:

    Colom: G And H
    If colom G and H is 0 it stay
    If colom G or H is 1 or 0 it stay
    If colom Gor H is N/A or 0 it stay

    But if colom G or H is both 1 it gets hide
    If colom G or H is 1 or N/A it gets hide


    [VBA]
    Sub HURows()
    BeginRow = 6
    EndRow = 25
    ChkCol = 7
    For RowCnt = BeginRow To EndRow
    If Cells(RowCnt, ChkCol).Value <= 1 Then
    Cells(RowCnt, ChkCol).EntireRow.Hidden = True
    Else
    Cells(RowCnt, ChkCol).EntireRow.Hidden = False
    End If
    Next RowCnt
    End Sub
    [/VBA]

    Simon said: If all you want to see is zero's left then this will do:

    VBA:
    Dim i As Long For i = Range("G" & Rows.Count).End(xlUp).Row To 1 Step -1 If Range("G" & i) = 1 Or Range("H" & i) = "N/A" Then Rows(i).Hidden = True End If Next i


    I am asking:
    Hi Simom

    I picked up something: when running the macro it works fine but this is what I get:

    If colom G has a N/A and colom H a 1 it does not hide.
    but it works the other way..

    If colom G has a 1 and colom H a N/A it hides.

    Same is working with 0, if N/A and 0 it work fine and does not hide
    but if it is 0 and N/A it gets hiden, and should not.

    Any idea?
    From unsolved link:
    http://www.vbaexpress.com/forum/showthread.php?t=34481

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    As Simon told you before, this statement

    If colom Gor H is N/A or 0 it stay

    and this one

    If colom G or H is 1 or N/A it gets hide

    are in conflict, you cannoty have both, because it is saying if G or H is N/A hide it, but don't hide it.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    VBAX Regular
    Joined
    Mar 2010
    Posts
    49
    Location
    Sorry for the confusion. This is execly what must happen:

    All these must be hidden If it's in G or H:
    N/A and 1 or
    1 and N/A
    or all 1's

    This must NOT be hidden:
    If there are any zeros in G or H for example,
    N/A and 0
    0 and N/A
    1 and 0
    0 and 1
    or only 0's

    Hope this hepls, sorry for the misunderstanding

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    What about all N/As?
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  5. #5
    VBAX Regular
    Joined
    Mar 2010
    Posts
    49
    Location
    Yes, all N/A must also be hidden

  6. #6
    VBAX Regular
    Joined
    Mar 2010
    Posts
    49
    Location

    90% Solution

    I have found a 90% solution in the KB but need help to hide were G and H is = "N/A"

    [VBA]Sub HideRow()
    Dim HiddenRow&, RowRange As Range, RowRangeValue&
    '*****************************
    '< Set the 1st & last rows to be hidden >
    Const FirstRow As Long = 6
    Const LastRow As Long = 24
    '< Set your columns that contain data >
    Const FirstCol As String = "G"
    Const LastCol As String = "H"
    '*****************************

    ActiveWindow.DisplayZeros = True
    Application.ScreenUpdating = False
    For HiddenRow = FirstRow To LastRow
    '(we're using columns B to G here)
    Set RowRange = Range(FirstCol & HiddenRow & _
    ":" & LastCol & HiddenRow)
    'sums the entries in cells in the RowRange
    RowRangeValue = Application.Sum(RowRange.Value)
    If RowRangeValue <= 0 Then
    'there's something in this row - don't hide
    Rows(HiddenRow).EntireRow.Hidden = False
    Else
    'there's nothing in this row yet - hide it
    Rows(HiddenRow).EntireRow.Hidden = True
    End If
    Next HiddenRow
    Application.ScreenUpdating = True
    End Sub
    [/VBA]

    Please see attached

Posting Permissions

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