输入用户表单数据后自动输入日期
Automatic Date Entered after userform data entered
在我通过 "enter/click" 按钮输入来自 User_Form 的数据后,有没有一种方法可以使日期或时间自动化?我试过这段代码,但它不断重启我的 excel 工作簿。最重要的是,我在 Private Sub Worksheet_Change(ByVal Target As Range)
中还有大量其他代码
会不会超载了?
所以我在想,如果我可以用我的 Userform_click() 编写代码,我会过得更好吗?
Private Sub Worksheet_Change(ByVal Target As Range)
Dim i As Integer
For i = 16 To 100
If Cells(i, 3).Value <> "" Then
Cells(i, 1).Value = Date & " "
Cells(i, 1).NumberFormat = "mm/dd/yy"
End If
Next
End Sub
您需要在 Sub
的开头添加 Application.EnableEvents = False
,否则每次在工作表中更改值时它都会保留 运行(就像您更改它时一样)在你的 For i = 16 To 100
循环中)。
代码
Private Sub Worksheet_Change(ByVal Target As Range)
Dim i As Integer
Application.EnableEvents = False
For i = 16 To 100
If Cells(i, 3).Value <> "" Then
Cells(i, 1).Value = Date & " "
Cells(i, 1).NumberFormat = "mm/dd/yy"
End If
Next i
Application.EnableEvents = True '<-- restore to original setting
End Sub
编辑 1:您可以用另一种方式编写 Sub
,只有在搜索到的 Range("C16:C100")
中更改单元格时才会输入它.仅当修改后的单元格在该范围内时,才检查每个单元格是否 Value <> ""
.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim C As Range
Application.EnableEvents = False
If Not Intersect(Range("C16:C100"), Target) Is Nothing Then
For Each C In Intersect(Range("C16:C100"), Target)
If C.Value <> "" Then
C.Offset(, -2).Value = Date & " "
C.Offset(, -2).NumberFormat = "mm/dd/yy"
End If
Next C
End If
Application.EnableEvents = True
End Sub
在我通过 "enter/click" 按钮输入来自 User_Form 的数据后,有没有一种方法可以使日期或时间自动化?我试过这段代码,但它不断重启我的 excel 工作簿。最重要的是,我在 Private Sub Worksheet_Change(ByVal Target As Range)
会不会超载了?
所以我在想,如果我可以用我的 Userform_click() 编写代码,我会过得更好吗?
Private Sub Worksheet_Change(ByVal Target As Range)
Dim i As Integer
For i = 16 To 100
If Cells(i, 3).Value <> "" Then
Cells(i, 1).Value = Date & " "
Cells(i, 1).NumberFormat = "mm/dd/yy"
End If
Next
End Sub
您需要在 Sub
的开头添加 Application.EnableEvents = False
,否则每次在工作表中更改值时它都会保留 运行(就像您更改它时一样)在你的 For i = 16 To 100
循环中)。
代码
Private Sub Worksheet_Change(ByVal Target As Range)
Dim i As Integer
Application.EnableEvents = False
For i = 16 To 100
If Cells(i, 3).Value <> "" Then
Cells(i, 1).Value = Date & " "
Cells(i, 1).NumberFormat = "mm/dd/yy"
End If
Next i
Application.EnableEvents = True '<-- restore to original setting
End Sub
编辑 1:您可以用另一种方式编写 Sub
,只有在搜索到的 Range("C16:C100")
中更改单元格时才会输入它.仅当修改后的单元格在该范围内时,才检查每个单元格是否 Value <> ""
.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim C As Range
Application.EnableEvents = False
If Not Intersect(Range("C16:C100"), Target) Is Nothing Then
For Each C In Intersect(Range("C16:C100"), Target)
If C.Value <> "" Then
C.Offset(, -2).Value = Date & " "
C.Offset(, -2).NumberFormat = "mm/dd/yy"
End If
Next C
End If
Application.EnableEvents = True
End Sub