英文:
I’d like to time stamp and add application username multiple different ranges on the same worksheet when any changes are made to rows in each range
问题
我表格的示例
我使用了这个事件宏来针对单个表格,但我想知道是否有一种方式可以使用类似的宏,但它适用于所有表格。例如,如果我在表格1(V3:AG34)中进行更改,我希望列AH中的每一行都有一个用户名/时间戳,该时间戳会在表格内发生更改的每一行。如果我对表格5(BP3:BU160)进行更改,我希望列BV中的每一行都有一个用户名/时间戳,该时间戳会在表格内发生更改的每一行。这是否可能,并且是否可以编写以在这些表格中添加行时更新?
工作表中的一些表格是使用Xlookup填充的。
Private Sub Worksheet_Change(ByVal Target As Range)
Dim r As Range, Intersection As Range, cell As Range
Dim s As String
Set r = Range("B3:CA1003")
Set Intersection = Intersect(r, Target)
s = vbCrLf & Environ("USERNAME") & vbCrLf & Application.UserName
If Intersection Is Nothing Then Exit Sub
Application.EnableEvents = False
For Each cell In Intersection
Range("A" & cell.Row).Value = Date & " " & Time & s
Next cell
Application.EnableEvents = True
End Sub
任何帮助将不胜感激。
提前感谢!
我已经在互联网上搜索了任何关于这个问题的答案,但我无法将它转换为同一工作表上的多个范围。
英文:
Example of one of my tables
I used this event macro for a single Table, but I would like to see if there is a way to use a similar macro but it applies to all tables. For example, if I make a change in table1 (V3:AG34) I would like column AH to have a username/timestamp in each row that has a change within that table. If I make a change to Table5 (BP3:BU160) I would like column BV to have a username/timestamp in each row that has a change in within that table. Is this possible, and can be written to update if I add rows to those tables?
Some of the tables in the worksheet are being populated using Xlookup.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim r As Range, Intersection As Range, cell As Range
Dim s As String
Set r = Range("B3:CA1003")
Set Intersection = Intersect(r, Target)
s = vbCrLf & Environ("USERNAME") & vbCrLf & Application.UserName
If Intersection Is Nothing Then Exit Sub
Application.EnableEvents = False
For Each cell In Intersection
Range("A" & cell.Row).Value = Date & " " & Time & s
Next cell
Application.EnableEvents = True
End Sub
Any help would be greatly appreciated.
Thanks in advance!
I’ve searched the internet for any sort of semblance of an answer to this and I’ve not been able to convert this to multiple ranges on the same worksheet.
答案1
得分: 0
私有子程序工作表更改(多个Excel表格ListObjects)
- 请注意,假定您已向每个表格添加了一个“标记”列。
Private Sub Worksheet_Change(ByVal Target As Range)
Dim lo As ListObject, irg As Range, drg As Range, urg As Range
For Each lo In Me.ListObjects
With lo.DataBodyRange
Set irg = Intersect(.Resize(, .Columns.Count - 1), Target)
If Not irg Is Nothing Then
Set drg = Intersect(irg.EntireRow, .Columns(.Columns.Count))
If urg Is Nothing Then
Set urg = drg
Else
Set urg = Union(urg, drg)
End If
Set irg = Nothing ' reset for the next iteration
End If
End With
Next lo
If urg Is Nothing Then Exit Sub
Dim Stamp As String: Stamp = Now & vbLf & Environ("USERNAME") & vbLf & Application.UserName
Application.EnableEvents = False
urg.Value = Stamp
Application.EnableEvents = True
End Sub
初始帖子
工作表更改:时间戳和用户
Private Sub Worksheet_Change(ByVal Target As Range)
Const FIRST_ROW_RANGE As String = "B3:CA3"
Const STAMP_COLUMN As String = "A"
Dim trg As Range
With Me.Range(FIRST_ROW_RANGE)
Set trg = .Resize(Me.Rows.Count - .Row + 1)
End With
Dim irg As Range: Set irg = Intersect(trg, Target)
If irg Is Nothing Then Exit Sub
Dim drg As Range
Set drg = Intersect(irg.EntireRow, Me.Columns(STAMP_COLUMN))
Dim Stamp As String: Stamp = Now & vbCrLf & Environ("USERNAME") & vbCrLf & Application.UserName
Application.EnableEvents = False
drg.Value = Stamp
Application.EnableEvents = True
End Sub
英文:
A Worksheet Change: Multiple Excel Tables (ListObjects)
- Note that it is assumed that you have added a 'stamp' column to each table.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim lo As ListObject, irg As Range, drg As Range, urg As Range
For Each lo In Me.ListObjects
With lo.DataBodyRange
Set irg = Intersect(.Resize(, .Columns.Count - 1), Target)
If Not irg Is Nothing Then
Set drg = Intersect(irg.EntireRow, .Columns(.Columns.Count))
If urg Is Nothing Then
Set urg = drg
Else
Set urg = Union(urg, drg)
End If
Set irg = Nothing ' reset for the next iteration
End If
End With
Next lo
If urg Is Nothing Then Exit Sub
Dim Stamp As String: Stamp = Now _
& vbLf & Environ("USERNAME") & vbLf & Application.UserName
Application.EnableEvents = False
urg.Value = Stamp
Application.EnableEvents = True
End Sub
Initial Post
A Worksheet Change: Time Stamp and User
<!-- language: lang-vb -->
Private Sub Worksheet_Change(ByVal Target As Range)
Const FIRST_ROW_RANGE As String = "B3:CA3"
Const STAMP_COLUMN As String = "A"
Dim trg As Range
With Me.Range(FIRST_ROW_RANGE)
Set trg = .Resize(Me.Rows.Count - .Row + 1)
End With
Dim irg As Range: Set irg = Intersect(trg, Target)
If irg Is Nothing Then Exit Sub
Dim drg As Range
Set drg = Intersect(irg.EntireRow, Me.Columns(STAMP_COLUMN))
Dim Stamp As String: Stamp = Now _
& vbCrLf & Environ("USERNAME") & vbCrLf & Application.UserName
Application.EnableEvents = False
drg.Value = Stamp
Application.EnableEvents = True
End Sub
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论