简体   繁体   中英

Timestamp on one sheet from updated cell on another VBA excel

I have a workbook with upto 31 sheets,(named (01),(02) etc, Each with the same format as per the screenshot below 工作簿1

I wish to use the following code which I have found(with help from this forum :) ) to place a timestamp in a mirror file (WorkBook2) that will work out the difference from when column B and Column C in workbook 1 is changed.

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

工作簿2

I know people have asked similar questions and im sorry if this is a duplicate but I really am lost on trying to get this to work.

Edit - Workbook two sheets are named in the same way with just a "t" after ie (01t), (02t) etc

Try this. The code needs to go in the ThisWorkbook module of workbook 1 and you need to add the name of workbook 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM