来自另一个VBA excel上的更新单元格的一张纸上的时间戳

时间:2018-03-06 21:58:30

标签: excel vba excel-vba

我有一张最多31张的工作簿,(名为(01),(02)等,每个都有与下面截图相同的格式 Workbook1

我希望使用以下代码(在此论坛的帮助下:))将时间戳放在镜像文件(WorkBook2)中,该文件将解决工作簿1中B列和C列的差异改变了。

Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Range("B:B, C:C"), Target) Is Nothing Then 'add Columns that will be changed to BA:BA and BB:BB
        'add error control
        On Error GoTo safe_exit
        'don't do anything until you know something has to be done
        Dim r As Range
        Application.EnableEvents = False
        For Each r In Intersect(Range("B:B, C:C"), Target) 'i know this would only work on the same sheet
            r.Offset(0, 1).Value = Now()   'Need to get this section to populate workbook2
        Next r
    End If
safe_exit:
    Application.EnableEvents = True
End Sub

Workbook2

我知道人们已经提出了类似的问题而且我很抱歉,如果这是重复的,但我真的迷失了,试图让它发挥作用。

编辑 - 工作簿两张纸以相同的方式命名,只有一个" t"在ie(01t),(02t)等之后

1 个答案:

答案 0 :(得分:0)

试试这个。代码需要进入工作簿1的ThisWorkbook模块,您需要添加工作簿2的名称。

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)

Dim r As Range

On Error GoTo safe_exit
Application.EnableEvents = False

For Each r In Target
    Select Case r.Column
        Case 2, 3, 5, 6, 8, 9, 11, 12 'b c e f h i k l
            Workbooks("name of workbook2").Sheets(Sh.Name & "t").Range(r.Address).Value = Now()
    End Select
Next r

safe_exit:
Application.EnableEvents = True

End Sub