Consulting

Results 1 to 4 of 4

Thread: Hide rows based on username

  1. #1
    VBAX Newbie
    Joined
    Nov 2018
    Posts
    2
    Location

    Hide rows based on username

    Dear community,

    could you please help me with the code I wrote. I would like to hide rows depending which user opens the file:

    Sub Macro1()
    '
    ' Macro1 Macro
    '
    LastRow = 1000
    For j = 1 To LastRow
    If (Range("A" & j) = Environ("Username")) <> True Then Rows("A" & j).EntireRow.Hidden = True
    
    Next j
    
    End Sub
    What I'm doing wrong?

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,711
    Location
    A little simpler, and a lot more general

    Try this


    Option Explicit
    
    Sub HideRows()
        Dim rCell As Range, rData As Range
        Dim sUsername As String
        
        Set rData = Nothing
        On Error Resume Next
        Set rData = ActiveSheet.Columns(1).SpecialCells(xlCellTypeConstants, xlTextValues)
        On Error GoTo 0
        
        If rData Is Nothing Then Exit Sub
        
         sUsername = UCase(Environ("Username"))
        
        For Each rCell In rData.Cells
            rCell.EntireRow.Hidden = (UCase(rCell.Value) = sUsername)
        Next
    End Sub
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  3. #3
    VBAX Newbie
    Joined
    Nov 2018
    Posts
    2
    Location
    Great! Thanks Paul!

    How would I reverse the exercise. So rows are hidden and I would like to show only those with the right username?

    Thanks.

  4. #4
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,711
    Location
    Probably something like this

    It's not 100% bullet-proof, like an empty sheet will probably make it fail, so you might need to add some error handling


    Option Explicit
    
    Sub ShowRows()
        Dim rCell As Range, rData As Range
        Dim sUsername As String
        
        
        With ActiveSheet
            'show all
            .Rows.Hidden = False
        
            'hide empty rows at the bottton
            Set rCell = .Cells(.Rows.Count, 1).End(xlUp).Offset(1, 0)
            Range(rCell, rCell.End(xlDown)).EntireRow.Hidden = True
        
            'get any text cells
            Set rData = Nothing
            On Error Resume Next
            Set rData = ActiveSheet.Columns(1).SpecialCells(xlCellTypeVisible)
            On Error GoTo 0
        
            If rData Is Nothing Then Exit Sub
        
             sUsername = UCase(Environ("Username"))
            
            For Each rCell In rData.Cells
                rCell.EntireRow.Hidden = (UCase(rCell.Value) <> sUsername)
            Next
        
        End With
        
    End Sub
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

Posting Permissions

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