简体   繁体   English

如果同一行中的一系列单元格发生任何更改,Excel VBA将更新单元格中的日期

[英]Excel VBA update the date in a cell if anything changes in a range of cells in the same row

I have a code where the date is updated in the cell in column D if any change was made in the cell in the same row in column F: 我有一个代码,如果在F列同一行的单元格中进行了任何更改,日期将在D列的单元格中更新:

Private Sub Worksheet_Change(ByVal Target As Range)
' Code to put the date of the latest update following a change in the corresponding cell in column F
Dim WorkRng As Range
Dim rng As Range
Dim xOffsetColumn As Integer
Set WorkRng = Intersect(Application.ActiveSheet.Range("F:F"), Target)
xOffsetColumn = -2 'The date is put 2 columns to the left of column F
If Not WorkRng Is Nothing Then
    Application.EnableEvents = False
    For Each rng In WorkRng
       If Not VBA.IsEmpty(rng.Value) Then
          rng.Offset(0, xOffsetColumn).Value = Now
          rng.Offset(0, xOffsetColumn).NumberFormat = "dd/mm/yyyy"
       Else
          rng.Offset(0, xOffsetColumn).ClearContents
       End If
   Next
   Application.EnableEvents = True
End If
End Sub

Now I need to adapt this code so that the date is updated in the cell in column D if any change was made in the cells in the same row in columns F to K. 现在,我需要调整此代码,以便在F到K列的同一行中的单元格中进行任何更改的情况下,更新D列中的单元格的日期。

I have very little VBA knowledge and would be grateful for any help in adapting the code. 我对VBA的了解很少,对于在改编代码方面提供的任何帮助将不胜感激。

This appears to work: 这似乎起作用:

Private Sub Worksheet_Change(ByVal Target As Range)
' Code to put the date of the latest update following a change in the corresponding cell in column F
    Dim WorkRng As Range, roww As Long
    Dim rng As Range
    Set WorkRng = Intersect(Range("F:K"), Target)
    If Not WorkRng Is Nothing Then
        Application.EnableEvents = False
            For Each rng In WorkRng
                roww = rng.Row
                If Not rng.Value = "" Then
                    Cells(roww, "D").Value = Now
                    Cells(roww, "D").NumberFormat = "dd/mm/yyyy"
                Else
                    Cells(roww, "D").ClearContents
                End If
            Next
        Application.EnableEvents = True
    End If
End Sub

We just do not use OFFSET() 我们只是不使用OFFSET()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当第一个单元格上的值发生变化时,Excel VBA 代码复制一行中的一系列单元格并粘贴到不同的工作表但相同的行/范围 - Excel VBA code to copy a range of cells in a row and paste in a different sheet but same row/range, when the value on 1st cell changes Excel VBA 忽略单元格范围中的隐藏行和批量更新(仅针对 Excel 中的筛选单元格更新) - Excel VBA Ignore Hidden Row and Mass Update from Cells Range (Update Only for Filtered Cell in Excel) Excel VBA 更新范围内的单元格 - Excel VBA Update Cells In Range 如果范围中的单元格更新,则使用日期更新 Excel 单元格 - Update excel cell with date if a cell in a range is update 查找单元格范围(Excel VBA)中是否存在日期 - Find if date is present in Range of cells (Excel VBA) VBA Excel单元格中的单独日期范围 - VBA Excel separate range of date in cells 如果VBA for Excel中单元格区域中的所有可见单元格都为空白,如何隐藏行? - How to hide row if all visible cells in cell range are blank in VBA for Excel? 使用Range(Cells(),Cells())Excel VBA设置具有单元格内容的变量 - Setting up a variable with Cell Content with Range(Cells(),Cells()) Excel VBA EXCEL VBA:如果cell.value =“ WORD”在范围内,则如何处理下一个单元格(同一行)的值 - EXCEL VBA: How to manupulate next cell's (same row) value if cell.value=“WORD” in a range Excel VBA:更改为同一单元格上的所有工作表 - Excel VBA: Changes to all sheets on same cell
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM