PDA

View Full Version : [SOLVED:] VBA for all Sheets (Windows login username to cell)



spmatrix
11-06-2023, 09:19 AM
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

Paul_Hossler
11-06-2023, 10:59 AM
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

spmatrix
11-06-2023, 11:22 AM
Thank you so much !!!!

Paul_Hossler
11-06-2023, 01:41 PM
I'd remove the "Me." of "Me.Range" for clarity since the "With Sh" is sufficient

spmatrix
11-06-2023, 11:16 PM
Again Thank you. I appreciate your help !