【发布时间】:2018-06-22 08:56:33
【问题描述】:
我有一个 Excel 表,如果在 A 列的单元格中输入了一个值,那么 B 列中它旁边的单元格会自动生成日期和时间。我遇到的问题是我想检查A列中的值是否为空白,即“”,那么日期和时间也会被清除。我已经想出了如何添加日期和时间,而不是如何添加对单元格值的检查以查看它是否为空白。 下面的代码和一个例子;
示例;
A4 a change is made, the current date and time is entered into B4
A8 a change is made, the current date and time is entered into B8
A4 the user clears the cell (presses delete on their keyboard), B4 is cleared too.
A4 the user enters "hello world", the current date and time is entered again
代码;
Private Sub Worksheet_Change(ByVal Target As Range)
Dim xCellColumn As Integer
Dim xTimeColumn As Integer
Dim xRow, xCol As Integer
xCellColumn = 2
xTimeColumn = 5
xRow = Target.Row
xCol = Target.Column
If Target.Text <> "" Then
If xCol = xCellColumn Then
Cells(xRow, xTimeColumn) = Now()
End If
End If
End Sub
谢谢
【问题讨论】: