我正在尝试制作一个excel文档,每当在相邻行中输入内容时,该文档都会自动填充A列中的日期。我在网上找到了类似的答案:
Private Sub Worksheet_Change(ByVal Target As Excel.Range)
'Updated by Extendoffice 2017/10/12
Dim xRg As Range, xCell As Range
On Error Resume Next
If (Target.Count = 1) Then
If (Not Application.Intersect(Target, Me.Range("B:B")) Is Nothing) Then _
Target.Offset(0, -1) = Date
Application.EnableEvents = False
Set xRg = Application.Intersect(Target.Dependents, Me.Range("B:B"))
If (Not xRg Is Nothing) Then
For Each xCell In xRg
xCell.Offset(0, -1) = Date
Next
End If
Application.EnableEvents = True
End If
End Sub
这会使相邻列自动填充日期。评论部分给出了另一个答案,这正是我所追求的功能。由于注释部分的格式不正确,其缩进不存在:
Private Sub Worksheet_Change(ByVal Target As Excel.Range)
'Updated by Extendoffice 20190924
Dim xRg As Range, xCell As Range
Dim xInt As Integer
On Error Resume Next
If (Target.Count = 1) Then
If (Not Application.Intersect(Target, Me.Range("B:N")) Is Nothing) Then
Application.EnableEvents = False
xInt = Target.Row
Me.Range("A" & xInt).Value = Date
Application.EnableEvents = True
End If
End If
End Sub
应该如何正确编写?
答案 0 :(得分:0)
缩进的数目表示相关代码行的嵌套深度。每增加一层嵌套,都应使用一个额外的制表符/ 4个空格。
几个网站会自动为您进行格式化,例如:
Private Sub Worksheet_Change(ByVal Target As Excel.Range)
'Updated by Extendoffice 20190924
Dim xRg As Range, xCell As Range
Dim xInt As Integer
On Error Resume Next
If (Target.Count = 1) Then
If (Not Application.Intersect(Target, Me.Range("B:N")) Is Nothing) Then
Application.EnableEvents = False
xInt = Target.Row
Me.Range("A" & xInt).Value = Date
Application.EnableEvents = True
End If
End If
End Sub