Consulting

Results 1 to 5 of 5

Thread: VBA for all Sheets (Windows login username to cell)

  1. #1

    VBA for all Sheets (Windows login username to cell)

    Hello to all the forum.
    I have the following vba code, which works fine in a sheet. How can I make it work on all the sheets or on the whole workbook, without having to pass the code to all the sheets separately?


    Thanks in advance
    Spiros

    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim cell As Range
    If Not Intersect(Target, Me.Range("C8:E2000")) Is Nothing Then
        For Each cell In Intersect(Target, Me.Range("C8:E2000"))
            If cell.Value <> "" Then
                    Me.Cells(cell.Row, "F").Value = Now()
                    Me.Cells(cell.Row, "G").Value = environ$("UserName")
                   Else
                   Me.Cells(cell.Row, "F").ClearContents
                  Me.Cells(cell.Row, "G").ClearContents
             End If
        Next cell
    End If
    End Sub

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,746
    Location
    In 'ThisWorkbook' module, use the Workbook_SheetChange event handler



    Option Explicit
    
    
    Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
        Dim cell As Range
        
        With Sh
            If Not Intersect(Target, Me.Range("C8:E2000")) Is Nothing Then
                For Each cell In Intersect(Target, Me.Range("C8:E2000"))
                    If cell.Value <> "" Then
                        Me.Cells(cell.Row, "F").Value = Now()
                        Me.Cells(cell.Row, "G").Value = Environ$("UserName")
                        
                    Else
                        Me.Cells(cell.Row, "F").ClearContents
                        Me.Cells(cell.Row, "G").ClearContents
                        
                    End If
                Next cell
            End If
        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

  3. #3
    Thank you so much !!!!

  4. #4
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,746
    Location
    I'd remove the "Me." of "Me.Range" for clarity since the "With Sh" is sufficient
    ---------------------------------------------------------------------------------------------------------------------

    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

  5. #5
    Again Thank you. I appreciate your help !

Posting Permissions

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