+ Reply to Thread
Results 1 to 12 of 12

Excel 2007 : VB code to hide rows that are empty or contain "-"

Hybrid View

  1. #1
    Registered User
    Join Date
    07-26-2010
    Location
    Canada
    MS-Off Ver
    Excel 2003
    Posts
    11

    VB code to hide rows that are empty or contain "-"

    Hi folks,

    Need some help with VB function to hide rows based on a value in a column defined.
    The function should evaluate on the first column and it will loop through all rows in the template range. If the cell evaluate to 0, then it suppress the row, otherwise, it leave it.

    I need the function to be generic and work on any worksheet.

    Any help is much appreciated to get me started.

    Thanks in advance!!

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: VB code to hide rows that are empty or contain "-"

    It helps to know how the values in that column get there, by manual insertion or by formula. But a generic way to examine all of column A on a given sheet would be:

    Option Explicit
    
    Sub HideRows()
    Dim RNG     As Range
    Dim HideRNG As Range
    Dim Cel     As Range
    
    Set RNG = Range("A1:A" & Cells.SpecialCells(xlLastCell).Row)
    
    For Each Cel In RNG
        If Cel.Value = "" Or Cel.Value = "-" Then
            If HideRNG Is Nothing Then
                Set HideRNG = Cel
            Else
                Set HideRNG = Union(HideRNG, Cel)
            End If
        End If
    Next Cel
    
    If Not HideRNG Is Nothing Then
        HideRNG.EntireRow.Hidden = True
        Set HideRNG = Nothing
    End If
    
    Set RNG = Nothing
    End Sub
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,998

    Re: VB code to hide rows that are empty or contain "-"

    Is there a reason for not just using an autofilter?
    Everyone who confuses correlation and causation ends up dead.

  4. #4
    Registered User
    Join Date
    07-26-2010
    Location
    Canada
    MS-Off Ver
    Excel 2003
    Posts
    11

    Re: VB code to hide rows that are empty or contain "-"

    Thanks for the feed back folks. I'm new with VB. I'm using the following code (as it's the only one i seem to understand) but I get an error when I try to compile it. By the way the object is sheet1.

    Run-Time error '424, Object required
    Function hidrows()
    Template.ActiveSheet.Protection.Enabled = False
    Template.ActiveSheet.Calculate
    
    For intRow = 1 To Template.ActiveSheet.UsedRange.Rows.Count
        If Template.Cells(intRow, 1).Value = "X" Or Template.Cells(intRow, 1).Value = "x" Then
            For intCol = 1 To Template.ActiveSheet.UsedRange.Columns.Count
                If Template.Rows(intRow).EntireRow.Hidden = False Then
                    Template.Rows(intRow).EntireRow.Hidden = True
                End If
            Next
        End If
    Next
    
    Template.ActiveSheet.Protection.Enabled = True
    
    End Function
    Last edited by romperstomper; 09-22-2010 at 06:29 PM. Reason: add code tags

  5. #5
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,998

    Re: VB code to hide rows that are empty or contain "-"

    What is Template supposed to be?

    Also, you must use code tags when posting code on the forum. I have added them for you on this occasion, but you must use them yourself in future, please.

  6. #6
    Registered User
    Join Date
    07-26-2010
    Location
    Canada
    MS-Off Ver
    Excel 2003
    Posts
    11

    Re: VB code to hide rows that are empty or contain "-"

    okay i removed the Template and not got a different error. Error 438, Object doesn't support this property or method. added quoted text around my code, hope that is what you meant by code tags.

    Function hidrows()
    ActiveSheet.Protection.Enabled = False
    ActiveSheet.Calculate
    
    For intRow = 1 To ActiveSheet.UsedRange.Rows.Count
        If Cells(intRow, 1).Value = "X" Or Cells(intRow, 1).Value = "x" Then
            For intCol = 1 To ActiveSheet.UsedRange.Columns.Count
                If Sheet1.Rows(intRow).EntireRow.Hidden = False Then
                    Sheet1.Rows(intRow).EntireRow.Hidden = True
                End If
            Next
        End If
    Next
    
    ActiveSheet.Protection.Enabled = True
    
    End Function
    Last edited by ironmonkey888; 09-23-2010 at 12:04 AM.

  7. #7
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,998

    Re: VB code to hide rows that are empty or contain "-"

    Highlight your code and click the # at the top of your post window. For more information about these and other tags, see here

    You can also just type them in:
    [code] your code goes here [/code]

  8. #8
    Registered User
    Join Date
    07-26-2010
    Location
    Canada
    MS-Off Ver
    Excel 2003
    Posts
    11

    Re: VB code to hide rows that are empty or contain "-"

    moved to excel programming page ....

  9. #9
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,998

    Re: VB code to hide rows that are empty or contain "-"

    Your unprotect and protect part is incorrect - it should be:
    ActiveSheet.UnProtect
    and:
    ActiveSheet.Protect

  10. #10
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,482

    Re: VB code to hide rows that are empty or contain "-"

    Why are you looping through the columns when you hide the entirerow?
    Cheers
    Andy
    www.andypope.info

  11. #11
    Registered User
    Join Date
    07-26-2010
    Location
    Canada
    MS-Off Ver
    Excel 2003
    Posts
    11

    Re: VB code to hide rows that are empty or contain "-"

    Tried your suggestion but i still get the same error.

    I only want to loop through the first column as there will be a formula that will return x if there is not data. I see what your mean. how do i modify my code to only loop through column 1?

    thx
    Last edited by ironmonkey888; 09-23-2010 at 10:20 AM.

  12. #12
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,998

    Re: VB code to hide rows that are empty or contain "-"

    What did you actually try?

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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