Consulting

Results 1 to 9 of 9

Thread: Autohiding Empty Rows. VBA code

  1. #1
    VBAX Regular pegbol's Avatar
    Joined
    Feb 2005
    Posts
    45
    Location

    Autohiding Empty Rows. VBA code

    .
    .
    Hello Masters,

    I have 2 sheets - A Master Data - and a secondary worksheet which I use for do display certain cells / rows from the master.

    I am trying to figure a way of autohiding rows within the secondary sheet that don't contain data.

    So, say for example: I have the following formula in the secondary sheet (range B5:B305)
    =IF(ISERROR(INDEX('Master'!F:F,MATCH(A5,'Master'!A:A,0))),"",INDEX('Master'!F:F,MATCH(A5,'Master'!A:A,0)))
    If the formula returns a blank value, I need the entire row to be hidden automatically.


    Your valuable help will be highly appreciated.

    best regards,
    Pedro
    .
    .

  2. #2
    Administrator
    Chat VP
    VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    Hi Pedro,

    See if this does what you want

    Option Explicit
     
    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim N&
    Application.ScreenUpdating = False
    For N = 5 To 305
    If Range("B" & N).Value = 0 Then
    Rows(N).EntireRow.Hidden = True
    Else
    Rows(N).EntireRow.Hidden = False
    End If
    Next N
    Application.ScreenUpdating = True
    End Sub
    HTH,
    John
    You know you're really in trouble when the light at the end of the tunnel turns out to be the headlight of a train hurtling towards you

    The major part of getting the right answer lies in asking the right question...


    Made your code more readable, use VBA tags (this automatically inserts [vba] at the start of your code, and [/vba ] at the end of your code) | Help those helping you by marking your thread solved when it is.

  3. #3
    VBAX Tutor
    Joined
    May 2004
    Location
    Germany, Dresden
    Posts
    217
    Location
    Maybe you could simply use the AutoFilterFeature and set it to display all non-empty cells?

  4. #4
    VBAX Regular pegbol's Avatar
    Joined
    Feb 2005
    Posts
    45
    Location
    .
    .
    John, thanks so much for your kind solution.

    I implemented the code, but can't make it work.

    Please, would you (or anyone else) help me to resolve the problem ?.

    I enclose my file with the precise data.

    kindest regards,
    Pedro
    .
    .

  5. #5
    Administrator
    Chat VP VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    Hi Pedro,

    Try this instead

    Option Explicit
    
    Private Sub Worksheet_Calculate()
        Dim N&
        Application.ScreenUpdating = False
        For N = 5 To 305
            If Range("B" & N).Text = Empty Then
                Rows(N).EntireRow.Hidden = True
            Else
                Rows(N).EntireRow.Hidden = False
            End If
        Next N
        Application.ScreenUpdating = True
    End Sub
    Regards,
    John
    You know you're really in trouble when the light at the end of the tunnel turns out to be the headlight of a train hurtling towards you

    The major part of getting the right answer lies in asking the right question...


    Made your code more readable, use VBA tags (this automatically inserts [vba] at the start of your code, and [/vba ] at the end of your code) | Help those helping you by marking your thread solved when it is.

  6. #6
    VBAX Regular pegbol's Avatar
    Joined
    Feb 2005
    Posts
    45
    Location
    .
    .
    John,

    Thanks so much!!!!!.

    Yes!!!. Now, the code hides all empty rows.

    One last question:
    When I add new registers in the Master sheet, the code takes long time to update my secondary sheet.
    Can be possible make it faster ?.
    If not, it is OK. I could deal with that.

    I appreciate your valuable assistance.

    best regards,
    Pedro
    .
    .

  7. #7
    Administrator
    Chat VP VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    Hi Pedro,

    You could change
    
    Private Sub Worksheet_Calculate()
    to
    Private Sub Worksheet_Activate()
    it then just runs the once each time the second sheet is activated

    Regards,
    John
    You know you're really in trouble when the light at the end of the tunnel turns out to be the headlight of a train hurtling towards you

    The major part of getting the right answer lies in asking the right question...


    Made your code more readable, use VBA tags (this automatically inserts [vba] at the start of your code, and [/vba ] at the end of your code) | Help those helping you by marking your thread solved when it is.

  8. #8
    VBAX Regular pegbol's Avatar
    Joined
    Feb 2005
    Posts
    45
    Location
    .
    .
    Again, thanks so much Master for all your kind attention!!!.


    best regards,
    Pedro

    .
    .

  9. #9
    Administrator
    Chat VP VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    glad to be able to help
    You know you're really in trouble when the light at the end of the tunnel turns out to be the headlight of a train hurtling towards you

    The major part of getting the right answer lies in asking the right question...


    Made your code more readable, use VBA tags (this automatically inserts [vba] at the start of your code, and [/vba ] at the end of your code) | Help those helping you by marking your thread solved when it is.

Posting Permissions

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