【发布时间】:2020-12-01 15:20:36
【问题描述】:
我早些时候就这种格式提出了一个问题,我得到了一个非常有帮助的答案。 我要求放置一个输入框,在将值添加到单元格并获取此代码后询问时间 显式选项
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Cells.Count > 1 Then Exit Sub 'abort if more than one cell was changed
'only run the code if a cell in column A was changed
If Not Intersect(Target, Me.Columns("A")) Is Nothing Then
'ask for time and write it 2 columns right of the target cell
Target.Offset(ColumnOffset:=2).Value = AskForValidTime
End If
End Sub
Private Function AskForValidTime() As String
Dim IsValid As Boolean
Do Until IsValid
Dim Result As Variant
Result = Application.InputBox("Wat is de tijd dat het monster genomen is?" & vbNewLine & "Gebruik UU:MM" & vbNewLine & "Voorbeeld: 09:30", "Tijdnotatie")
'test if time is a valid time with less than 24 hours and less than 60 minutes
Dim SplitTime() As String
SplitTime = Split(Result, ":")
If UBound(SplitTime) = 1 Then
If Val(SplitTime(0)) < 24 And Val(SplitTime(1)) < 60 Then
IsValid = True
AskForValidTime = Result
Exit Do
End If
End If
MsgBox "Een correcte tijdsnotatie is nodig om door te gaan. Klik op" & vbNewLine & "<Ok> om de tijd opnieuw in te vullen", vbOKOnly + vbExclamation, vbNullString
Loop
End Function
到目前为止,这段代码运行良好,现在我试图让它询问日期和时间并验证两者。到目前为止,它还没有解决,因为我输入为 DD-MM 的日期从输入框中输出为 MM-DD。这是我对上面代码的改编,希望有人能帮助我。
选项显式
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Cells.Count > 1 Then Exit Sub 'abort if more than one cell was changed
'only run the code if a cell in column A was changed
If Not Intersect(Target, Me.Columns("A")) Is Nothing Then
'ask for time and write it 2 columns right of the target cell
Target.Offset(ColumnOffset:=2).Value = AskForValidTime
End If
End Sub
Private Function AskForValidTime() As String
Dim IsValid As Boolean
Do Until IsValid
Dim Result As Variant
Result = Application.InputBox("Wat is de tijd dat het monster genomen is?" & vbNewLine & "Gebruik UU:MM" & vbNewLine & "Voorbeeld: 09:30", "Tijdnotatie")
'test if time is a valid time with less than 24 hours and less than 60 minutes
Dim SplitDatetime() As String
Dim SplitTime() As String
SplitDatetime = Split(Result)
If UBound(SplitDatetime) = 1 Then
SplitTime = Split(SplitDatetime(1), ":")
If Val(SplitTime(0)) < 24 And Val(SplitTime(1)) < 60 And IsDate(SplitDatetime(0)) Then
IsValid = True
AskForValidTime = Result
Exit Do
End If
End If
MsgBox "Een correcte tijdsnotatie is nodig om door te gaan. Klik op" & vbNewLine & "<Ok> om de tijd opnieuw in te vullen", vbOKOnly + vbExclamation, vbNullString
Loop
End Function
【问题讨论】:
-
“自从我输入为 DD-MM” 你到底输入了什么?你能添加一个例子吗(见minimal reproducible example)?问题是
IsDate只能检查真实的日期时间。但是日期必须有年份!如果没有年份,则不是有效日期。这是因为例如 2 月可以有 28 天或 29 天(取决于年份),因此没有年份的 "date" 在技术上无法验证。 -
例如,如果我使用 1-12-2020 7:30,则输出为 12-1-2020 7:30。我想知道如何解决这个问题
-
您在输入日期之前是否正确指定了单元格的日期格式?例如
Range("A1").NumberFormat = "DD-MM-YYYY hh:mm"?