一旦我通过“输入/点击”按钮从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
答案 0 :(得分:1)
您需要在Application.EnableEvents = False
的开头添加Sub
,否则每次在工作表中更改值时都会继续运行(例如,当您在{{1}内更改值时}循环)。
<强>代码强>
For i = 16 To 100
编辑1 :您可以用另一种方式编写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
,只有在搜索到的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