Recording a Data Entry Time: modify my macro to make it valid for multiple columns

StackOverflow https://stackoverflow.com/questions/22990248

  •  01-07-2023
  •  | 
  •  

Question

I found a macro online that automatically inputs the date in column B (row X), if the content of column A (same row) is altered.

I would like to modify it so that it works on multiple rows - so that in addition to monitoring changes in row A, it will do the same for columns C, E, G, and input dates in columns D, F, H (respectively) if the former's content is changed.

Here's the macro I found, from: http://excel.tips.net/T003116_Recording_a_Data_Entry_Time.html


Private Sub Worksheet_Change(ByVal Target As Excel.Range)
    Dim rCell As Range
    Dim rChange As Range

    On Error GoTo ErrHandler
    Set rChange = Intersect(Target, Range("A:A"))
    If Not rChange Is Nothing Then
        Application.EnableEvents = False
        For Each rCell In rChange
            If rCell > "" Then
                With rCell.Offset(0, 1)
                    .Value = Now
                    .NumberFormat = "hh:mm:ss"
                End With
            Else
                rCell.Offset(0, 1).Clear
            End If
        Next
    End If

ExitHandler:
    Set rCell = Nothing
    Set rChange = Nothing
    Application.EnableEvents = True
    Exit Sub
ErrHandler:
    MsgBox Err.Description
    Resume ExitHandler
End Sub

I'm not too familiar with all the different VBA elements, so forgive the elementary question. Hope you can take the time to help, thanks.

Était-ce utile?

La solution

Replace:

Set rChange = Intersect(Target, Range("A:A"))

with:

Set rChange = Intersect(Target, Range("A:A, C:C, E:E, G:G"))
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top