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

前端 未结 3 1188
滥情空心
滥情空心 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条回答
  • 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
    
    0 讨论(0)
  • 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

    0 讨论(0)
  • 2020-12-22 00:58

    Here the code you need

    Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal target As Range)
        If Sh.Name = "checklist" Then
              If Not Intersect(target, Range("BH400:BL500")) Is Nothing Then
                 Cells(target.Row, 1) = Format(Now, "DD/MM/YYYY  hh:mm")
                 Test target
              End If
        End If
    End Sub
    
    Private Sub Test(target As Range)
        Dim LastRow As Long
    
        LastRow = Range("evaluation!A" & Sheets("evaluation").Rows.Count).End(xlUp).Row
    
        If Range("evaluation!A1").Value <> "" Then
           LastRow = LastRow + 1
        End If
        Range("evaluation!A" +LastRow).Value = "=NOW()"
        Range("evaluation!B" +LastRow).Value = Range("CheckList!B" & Target.row)
        Range("evaluation!C" +LastRow).Value= "1"
        Range("evaluation!D" +LastRow).Value= Range("CheckList!D" & Target.row)
    End Sub
    

    Update as your google sheet

    Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal target As Range)
        If Sh.Name = "CheckList" Then
              'Monitoring from A3:E100, if different change this
              If Not Intersect(target, Range("A3:E100")) Is Nothing Then
                 'if any monitoring here, please you add here
                 Test target 'Here procedure to insert
              End If
        End If
    End Sub
    
    
    Private Sub Test(target As Range)
        Dim LastRow As Long
    
        LastRow = Range("Evaluation!A" & Sheets("Evaluation").Rows.Count).End(xlUp).Row
    
        If Range("Evaluation!A1").Value <> "" Then
           LastRow = LastRow + 1
        End If
        'every change A3:E in checklist will insert row to this evaluation
        'but if different please you decide here
        Range("Evaluation!A" & LastRow).Value = Format(Now, "dd.mm.yyyy hh:mm") 'you can change this
        Range("Evaluation!B" & LastRow & ":F" & LastRow).Value = Range("CheckList!A" & target.Row & ":E" & target.Row).Value
    End Sub
    

    Next Update

    Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal target As Range)
        If Sh.Name = "CheckList" Then
              'Monitoring from A3:E100, if different change this
              If Not Intersect(target, Range("A3:E100")) Is Nothing Then
                 'if any monitoring here, please you add here
                 Test target 'Here procedure to insert
              End If
              If Not Intersect(target, Range("G3:K100")) Is Nothing Then
                 'if any monitoring here, please you add here
                 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
        myCol = target.Column
    
        If myCol >= 1 And myCol <= 5 Then
            LastRow = Range("Evaluation!A" & Sheets("Evaluation").Rows.Count).End(xlUp).Row
    
            If Range("Evaluation!A1").Value <> "" Then
               LastRow = LastRow + 1
            End If
            'every change A3:E in checklist will insert row to this evaluation
            'but if different please you decide here
            Range("Evaluation!A" & LastRow).Value = Format(Now, "dd.mm.yyyy hh:mm") 'you can change this
            Range("Evaluation!B" & LastRow & ":F" & LastRow).Value = Range("CheckList!A" & target.Row & ":E" & target.Row).Value
        End If
        If myCol >= 7 And myCol <= 11 Then
            LastRow = Range("Evaluation!H" & Sheets("Evaluation").Rows.Count).End(xlUp).Row
    
            If Range("Evaluation!H1").Value <> "" Then
               LastRow = LastRow + 1
            End If
            'every change A3:E in checklist will insert row to this evaluation
            'but if different please you decide here
            Range("Evaluation!H" & LastRow).Value = Format(Now, "dd.mm.yyyy hh:mm") 'you can change this
            Range("Evaluation!I" & LastRow & ":M" & LastRow).Value = Range("CheckList!G" & target.Row & ":K" & target.Row).Value
        End If
    End Sub
    
    0 讨论(0)
提交回复
热议问题