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

前端 未结 3 1198
滥情空心
滥情空心 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
    2020-12-22 00:56

    Here to monitor CheckList!A1:H4 and copy CheckList!J3:N5 to Evaluation empty row of Column A entirely:

    Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal target As Range)
        If Sh.Name = "CheckList" Then
              'Monitoring from CheckList!A1:H4, if different change this
    
              If Not Intersect(target, Range("CheckList!A1:H4")) Is Nothing Then
                 Test target 'Here procedure to insert
              End If
        End If
    End Sub
    
    
    Private Sub Test(target As Range)
        Dim LastRow As Long
    
        Dim myCol As Long
        Dim myRow As Long
        myCol = target.Column
    
        If myCol >= 1 And myCol <= 8 Then
        If Range("Evaluation!A1") = "" Then Range("Evaluation!A1") = "History"
        If Range("Evaluation!A2") = "" Then Range("Evaluation!A2") = "Date"
            LastRow = Range("Evaluation!A" & Sheets("Evaluation").Rows.Count).End(xlUp).Row
    
            'In this situation, all J3 to N5 will be copied
            'if different, please modify as actual range
            Dim myRange As Range
            Set myRange = Range("CheckList!J3:N5")
            For a = 1 To myRange.Rows.Count
                LastRow = LastRow + 1
                Range("Evaluation!A" & LastRow).Value = Format(Now, "dd.mm.yyyy hh:mm")
                Range("Evaluation!B" & LastRow & ":F" & LastRow).Value = myRange.Rows(a).Value
            Next a
        End If
    End Sub
    

提交回复
热议问题