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

huangapple go评论111阅读模式
英文:

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填充的。

  1. Private Sub Worksheet_Change(ByVal Target As Range)
  2. Dim r As Range, Intersection As Range, cell As Range
  3. Dim s As String
  4. Set r = Range("B3:CA1003")
  5. Set Intersection = Intersect(r, Target)
  6. s = vbCrLf & Environ("USERNAME") & vbCrLf & Application.UserName
  7. If Intersection Is Nothing Then Exit Sub
  8. Application.EnableEvents = False
  9. For Each cell In Intersection
  10. Range("A" & cell.Row).Value = Date & " " & Time & s
  11. Next cell
  12. Application.EnableEvents = True
  13. 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.

  1. Private Sub Worksheet_Change(ByVal Target As Range)
  2. Dim r As Range, Intersection As Range, cell As Range
  3. Dim s As String
  4. Set r = Range("B3:CA1003")
  5. Set Intersection = Intersect(r, Target)
  6. s = vbCrLf & Environ("USERNAME") & vbCrLf & Application.UserName
  7. If Intersection Is Nothing Then Exit Sub
  8. Application.EnableEvents = False
  9. For Each cell In Intersection
  10. Range("A" & cell.Row).Value = Date & " " & Time & s
  11. Next cell
  12. Application.EnableEvents = True
  13. 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)

  • 请注意,假定您已向每个表格添加了一个“标记”列。

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. Private Sub Worksheet_Change(ByVal Target As Range)
  2. Dim lo As ListObject, irg As Range, drg As Range, urg As Range
  3. For Each lo In Me.ListObjects
  4. With lo.DataBodyRange
  5. Set irg = Intersect(.Resize(, .Columns.Count - 1), Target)
  6. If Not irg Is Nothing Then
  7. Set drg = Intersect(irg.EntireRow, .Columns(.Columns.Count))
  8. If urg Is Nothing Then
  9. Set urg = drg
  10. Else
  11. Set urg = Union(urg, drg)
  12. End If
  13. Set irg = Nothing ' reset for the next iteration
  14. End If
  15. End With
  16. Next lo
  17. If urg Is Nothing Then Exit Sub
  18. Dim Stamp As String: Stamp = Now & vbLf & Environ("USERNAME") & vbLf & Application.UserName
  19. Application.EnableEvents = False
  20. urg.Value = Stamp
  21. Application.EnableEvents = True
  22. End Sub

初始帖子

工作表更改:时间戳和用户

  1. Private Sub Worksheet_Change(ByVal Target As Range)
  2. Const FIRST_ROW_RANGE As String = "B3:CA3"
  3. Const STAMP_COLUMN As String = "A"
  4. Dim trg As Range
  5. With Me.Range(FIRST_ROW_RANGE)
  6. Set trg = .Resize(Me.Rows.Count - .Row + 1)
  7. End With
  8. Dim irg As Range: Set irg = Intersect(trg, Target)
  9. If irg Is Nothing Then Exit Sub
  10. Dim drg As Range
  11. Set drg = Intersect(irg.EntireRow, Me.Columns(STAMP_COLUMN))
  12. Dim Stamp As String: Stamp = Now & vbCrLf & Environ("USERNAME") & vbCrLf & Application.UserName
  13. Application.EnableEvents = False
  14. drg.Value = Stamp
  15. Application.EnableEvents = True
  16. End Sub
英文:

A Worksheet Change: Multiple Excel Tables (ListObjects)

  • Note that it is assumed that you have added a 'stamp' column to each table.

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. Private Sub Worksheet_Change(ByVal Target As Range)
  2. Dim lo As ListObject, irg As Range, drg As Range, urg As Range
  3. For Each lo In Me.ListObjects
  4. With lo.DataBodyRange
  5. Set irg = Intersect(.Resize(, .Columns.Count - 1), Target)
  6. If Not irg Is Nothing Then
  7. Set drg = Intersect(irg.EntireRow, .Columns(.Columns.Count))
  8. If urg Is Nothing Then
  9. Set urg = drg
  10. Else
  11. Set urg = Union(urg, drg)
  12. End If
  13. Set irg = Nothing ' reset for the next iteration
  14. End If
  15. End With
  16. Next lo
  17. If urg Is Nothing Then Exit Sub
  18. Dim Stamp As String: Stamp = Now _
  19. & vbLf & Environ("USERNAME") & vbLf & Application.UserName
  20. Application.EnableEvents = False
  21. urg.Value = Stamp
  22. Application.EnableEvents = True
  23. End Sub

Initial Post

A Worksheet Change: Time Stamp and User

<!-- language: lang-vb -->

  1. Private Sub Worksheet_Change(ByVal Target As Range)
  2. Const FIRST_ROW_RANGE As String = &quot;B3:CA3&quot;
  3. Const STAMP_COLUMN As String = &quot;A&quot;
  4. Dim trg As Range
  5. With Me.Range(FIRST_ROW_RANGE)
  6. Set trg = .Resize(Me.Rows.Count - .Row + 1)
  7. End With
  8. Dim irg As Range: Set irg = Intersect(trg, Target)
  9. If irg Is Nothing Then Exit Sub
  10. Dim drg As Range
  11. Set drg = Intersect(irg.EntireRow, Me.Columns(STAMP_COLUMN))
  12. Dim Stamp As String: Stamp = Now _
  13. &amp; vbCrLf &amp; Environ(&quot;USERNAME&quot;) &amp; vbCrLf &amp; Application.UserName
  14. Application.EnableEvents = False
  15. drg.Value = Stamp
  16. Application.EnableEvents = True
  17. End Sub

huangapple
  • 本文由 发表于 2023年3月21日 03:06:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/75794331.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定