Creating a data history with Excel VBA using LastRow, Time Stamp and Workbook.sheetchange

前端 未结 3 1206
滥情空心
滥情空心 2020-12-22 00:41

I have programmed a manual macro in Excel VBA that displays a table to show the history of certain data in a sheet called \"evaluation\". The data i reference to is in the t

3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-22 00:57

    You must have general module (not object module), if no, insert new module, and put this:

    Public myLastRow As Long
    Public myTarget As Long
    
    Public Function CheckMe(target As Long)
        CheckMe = ""
        Range("Evaluation!A:F").UnMerge
        LastRow = Range("Evaluation!A" & Sheets("Evaluation").Rows.Count).End(xlUp).Row
        If Range("Evaluation!A1").Value <> "" Then
           LastRow = LastRow + 1
        End If
        myLastRow = LastRow
        myTarget = target
    End Function
    

    Call the function in cell G3 by formula:

    =LEFT(A3&B3&C3&D3&E3&F3&CheckMe(ROW(A3)),0)
    

    Copy Cell G3 to G4:G1000 (or as your last possible row)

    Last, in ThisWorkBook Module as we use before, clear all code, and add this code:

    Private Sub Workbook_SheetCalculate(ByVal Sh As Object)
        If myTarget < 3 Then Exit Sub
        Range("Evaluation!A:F").UnMerge
    
        Range("Evaluation!A" & myLastRow).Value = Format(Now, "dd.mm.yyyy hh:mm") 'you can change this
        Range("Evaluation!B" & myLastRow & ":F" & myLastRow).Value = Range("Checklist!A" & myTarget & ":E" & myTarget).Value
        myLastRow = 0
        myTarget = 0
    End Sub
    

    And do test

提交回复
热议问题