Excel VBA将(不变)时间戳添加到命名单元格

时间:2018-05-08 03:05:45

标签: excel vba excel-vba

我想自动为NAMED CELLS (Named_Cell_1,Named_Cell_2,...)或特定的NAMED RANGE单元格(Named_Range_Cells_1,Named_Range_Cells_2,...)添加非更新时间戳。 ..)随机放置在工作表中,当输入直接相邻列中与指定单元格或命名范围直接相邻的单元格中的值时。

我无法使用未命名的单元格或单元格范围(例如:A1:A4)来确定需要输入日期戳的单元格,因为单元格是动态工作表的一部分,不断更改行和列,因此,需要显示日期戳的单元格必须是所有命名的单元格或范围。

大约有5到6个命名单元格需要加盖日期戳,所以我不介意对每个单元格进行硬VBA编码。

我意识到我们需要VBA代码,我们不能使用Now()或Today()。

您的帮助和时间将不胜感激。我发现了一些代码片段,它们可以完成这项工作但是过于笼统,并且不使用单元名称或范围名称。

非常感谢任何帮助。

= = = = = = = = = =

这是第一个片段。

Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Column = 3 Then
        Application.EnableEvents = False
        Cells(Target.Row, 4) = Date + Time
        Application.EnableEvents = True
    End If
End Sub

这是第二个。

Private Sub Worksheet_Change(ByVal Target As Range)

If Target.Cells.Count > 1 Then Exit Sub
If Not Intersect(Target, Range("A1:A10")) Is Nothing Then
    With Target(1, 2)
    .Value = Date
    .Entire Column.AutoFit
    End With
End If
End Sub

1 个答案:

答案 0 :(得分:0)

一种方法:在Worksheet_Change测试中,如果相邻的单元格有一个名称,如果它确实测试了名称的名称,看它是否是这里的时间标记"名称。然后放置时间戳。

要清楚,它是包含名为的时间戳的单元格。

像这样,将时间戳设置为已更改数据的

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim cl As Range, nm As Name

    For Each cl In Target.Cells
        If cl.Column > 1 Then
            Set nm = Nothing
            On Error Resume Next
            Set nm = cl.Offset(0, -1).Name
            On Error GoTo 0
            If Not nm Is Nothing Then
                ' Keep only one of these three lines
                If nm.Name Like "*Named_Cell_*" Then ' Book or Sheet scoped names
                'If nm.Name Like "Named_Cell_*" Then ' Book scoped names
                'If nm.Name Like "*!Named_Cell_*" Then ' Sheet scoped names

                    Application.EnableEvents = False
                    nm.RefersToRange = Date + Time
                    Application.EnableEvents = True
                End If
            End If
        End If
    Next
End Sub

像这样,将时间戳设置为已更改数据的右侧

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim cl As Range, nm As Name

    For Each cl In Target.Cells
        If cl.Column < Me.Columns.Count  Then
            Set nm = Nothing
            On Error Resume Next
            Set nm = cl.Offset(0, 1).Name
            On Error GoTo 0
            If Not nm Is Nothing Then
                ' Keep only one of these three lines
                If nm.Name Like "*Named_Cell_*" Then ' Book or Sheet scoped names
                'If nm.Name Like "Named_Cell_*" Then ' Book scoped names
                'If nm.Name Like "*!Named_Cell_*" Then ' Sheet scoped names

                    Application.EnableEvents = False
                    nm.RefersToRange = Date + Time
                    Application.EnableEvents = True
                End If
            End If
        End If
    Next
End Sub

这将处理WorkBook和WorkSheet范围的名称。其他选项包括注释掉的行

确保名称模式对于这些&#34;时间戳来说是唯一的&#34;名。

您可以根据需要添加任意数量的命名单元格,只需在名称后面添加一个后缀即可。我个人使用工作表范围的名称&#34; TimeStampGoesHere_1&#34;,&#34; TimeStampGoesHere_2&#34;等(您可以在每张纸上重新开始编号为1)并更改为If

If nm.Name Like "*!TimeStampGoesHere_*" Then

关于名称范围的说明。

名称可以作用于工作簿或单个工作表。这在名称管理器中可见。应该清楚If语句如何工作

enter image description here

要了解VBA如何报告这些命名范围的名称,请运行此

Sub NameScope()
    Dim nm As Name

    Set nm = Sheet1.Range("A1").Name
    Debug.Print "Workbook Scoped Name", nm.Name

    Set nm = Sheet1.Range("A2").Name
    Debug.Print "Worksheet Scoped Name", nm.Name
End Sub

显示

  

工作簿范围名称样本1
  工作表范围名称表1!Sample2