Excel VBA输入框,用于日期和时间,而不仅仅是时间

Excel VBA输入框,用于日期和时间,而不仅仅是时间,excel,vba,datetime,Excel,Vba,Datetime,早些时候我问了一个关于这个格式的问题,我得到了一个非常有用的答案。 我要求在单元格中添加一个值并获得此代码后,放置一个输入框,询问输入时间 选项显式 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 colum

早些时候我问了一个关于这个格式的问题,我得到了一个非常有用的答案。 我要求在单元格中添加一个值并获得此代码后,放置一个输入框,询问输入时间 选项显式

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
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
Private子工作表\u更改(ByVal目标作为范围)
如果Target.Cells.Count>1,则如果更改了多个单元格,则退出子“中止”
'仅当列a中的单元格已更改时才运行代码
如果不相交(Target,Me.Columns(“A”))则什么都不是
'询问时间并在目标单元格右侧写两列
Target.Offset(ColumnCoffset:=2).Value=AskForValidTime
如果结束
端接头
私有函数AskForValidTime()作为字符串
Dim作为布尔值有效
直到有效为止
作为变量的模糊结果
结果=Application.InputBox(“怪物基因是什么?”&vbNewLine和“Gebruik UU:MM”&vbNewLine和“Voorbeeld:09:30”,“Tijdnotatie”)
'测试时间是否为小于24小时小于60分钟的有效时间
Dim SplitTime()作为字符串
拆分时间=拆分(结果“:”)
如果UBound(SplitTime)=1,则
如果Val(SplitTime(0))小于24且Val(SplitTime(1))小于60,则
IsValid=True
AskForValidTime=结果
退出Do
如果结束
如果结束
MsgBox“即使纠正了提示,提示也会在门上显示。Klik op”&vbNewLine和“在vullen中显示提示”,vbOKOnly+vb惊叹号,vbNullString
环
端函数
到目前为止,这段代码工作得很好,现在我正试图让它询问日期和时间,并验证两者。到目前为止,它还没有工作,因为我输入的日期为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 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
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
Private子工作表\u更改(ByVal目标作为范围)
如果Target.Cells.Count>1,则如果更改了多个单元格,则退出子“中止”
'仅当列a中的单元格已更改时才运行代码
如果不相交(Target,Me.Columns(“A”))则什么都不是
'询问时间并在目标单元格右侧写两列
Target.Offset(ColumnCoffset:=2).Value=AskForValidTime
如果结束
端接头
私有函数AskForValidTime()作为字符串
Dim作为布尔值有效
直到有效为止
作为变量的模糊结果
结果=Application.InputBox(“怪物基因是什么?”&vbNewLine和“Gebruik UU:MM”&vbNewLine和“Voorbeeld:09:30”,“Tijdnotatie”)
'测试时间是否为小于24小时小于60分钟的有效时间
Dim SplitDatetime()作为字符串
Dim SplitTime()作为字符串
SplitDatetime=Split(结果)
如果UBound(SplitDatetime)=1,则
SplitTime=Split(SplitDatetime(1),“:”)
如果Val(SplitTime(0))小于24,Val(SplitTime(1))小于60,IsDate(SplitDatetime(0))则
IsValid=True
AskForValidTime=结果
退出Do
如果结束
如果结束
MsgBox“即使纠正了提示,提示也会在门上显示。Klik op”&vbNewLine和“在vullen中显示提示”,vbOKOnly+vb惊叹号,vbNullString
环
端函数

我建议进行以下更改

Option Explicit

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
        Target.Offset(ColumnOffset:=2).NumberFormat = "DD-MM-YYYY hh:mm"
        Target.Offset(ColumnOffset:=2).Value = AskForValidDateTime
    End If
End Sub


Private Function AskForValidDateTime() As Date
    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")
        
        
        Dim SplitDateTime() As String 'split date from time
        SplitDateTime = Split(Result, " ")
        If UBound(SplitDateTime) = 1 Then
            Dim SplitDate() As String
            'note the following code only works for dates entered in the format DD-MM-YYYY
            SplitDate = Split(SplitDateTime(0), "-")
            If UBound(SplitDate) = 2 Then
                Dim SplitTime() As String
                SplitTime = Split(SplitDateTime(1), ":")
                If UBound(SplitTime) = 1 Then
                    If Val(SplitTime(0)) < 24 And Val(SplitTime(1)) < 60 Then
                        IsValid = True
                        'note the following code only works for dates entered in the format DD-MM-YYYY
                        AskForValidDateTime = DateSerial(Val(SplitDate(2)), Val(SplitDate(1)), Val(SplitDate(0))) + TimeSerial(Val(SplitTime(0)), Val(SplitTime(1)), 0)
                        Exit Do
                    End If
                End If
            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
选项显式
私有子工作表_更改(ByVal目标作为范围)
如果Target.Cells.Count>1,则如果更改了多个单元格,则退出子“中止”
'仅当列a中的单元格已更改时才运行代码
如果不相交(Target,Me.Columns(“A”))则什么都不是
Target.Offset(columnPoffset:=2).NumberFormat=“DD-MM-YYYY hh:MM”
Target.Offset(ColumnCoffset:=2).Value=AskForValidDateTime
如果结束
端接头
私有函数AskForValidDateTime()作为日期
Dim作为布尔值有效
直到有效为止
作为变量的模糊结果
结果=Application.InputBox(“怪物基因是什么?”&vbNewLine和“Gebruik UU:MM”&vbNewLine和“Voorbeeld:09:30”,“Tijdnotatie”)
Dim SplitDateTime()作为字符串“从时间分割日期”
SplitDateTime=Split(结果为“”)
如果UBound(SplitDateTime)=1,则
Dim SplitDate()作为字符串
'注意:以下代码仅适用于以DD-MM-YYYY格式输入的日期
SplitDate=Split(SplitDateTime(0),“-”)
如果UBound(SplitDate)=2,则
Dim SplitTime()作为字符串
SplitTime=Split(SplitDateTime(1),“:”)
如果UBound(SplitTime)=1,则
如果Val(SplitTime(0))小于24且Val(SplitTime(1))小于60,则
IsValid=True
'注意:以下代码仅适用于以DD-MM-YYYY格式输入的日期
AskForValidDateTime=DateSerial(Val(SplitDate(2)),Val(SplitDate(1)),Val(SplitDate(0)))+TimeSerial(Val(SplitTime(0)),Val(SplitTime(1)),0)
退出Do
如果结束
如果结束
如果结束
如果结束
MsgBox“即使纠正了提示,提示也会在门上显示。Klik op”&vbNewLine和“在vullen中显示提示”,vbOKOnly+vb惊叹号,vbNullString
环
端函数

我建议进行以下更改

Option Explicit

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
        Target.Offset(ColumnOffset:=2).NumberFormat = "DD-MM-YYYY hh:mm"
        Target.Offset(ColumnOffset:=2).Value = AskForValidDateTime
    End If
End Sub


Private Function AskForValidDateTime() As Date
    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")
        
        
        Dim SplitDateTime() As String 'split date from time
        SplitDateTime = Split(Result, " ")
        If UBound(SplitDateTime) = 1 Then
            Dim SplitDate() As String
            'note the following code only works for dates entered in the format DD-MM-YYYY
            SplitDate = Split(SplitDateTime(0), "-")
            If UBound(SplitDate) = 2 Then
                Dim SplitTime() As String
                SplitTime = Split(SplitDateTime(1), ":")
                If UBound(SplitTime) = 1 Then
                    If Val(SplitTime(0)) < 24 And Val(SplitTime(1)) < 60 Then
                        IsValid = True
                        'note the following code only works for dates entered in the format DD-MM-YYYY
                        AskForValidDateTime = DateSerial(Val(SplitDate(2)), Val(SplitDate(1)), Val(SplitDate(0))) + TimeSerial(Val(SplitTime(0)), Val(SplitTime(1)), 0)
                        Exit Do
                    End If
                End If
            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
选项显式
私有子工作表_更改(ByVal目标作为范围)
如果Target.Cells.Count>1,则如果更改了多个单元格,则退出子“中止”
'仅当列a中的单元格已更改时才运行代码
如果不相交(Target,Me.Columns(“A”))则什么都不是
Target.Offset(columnPoffset:=2).NumberFormat=“DD-MM-YYYY hh:MM”
Target.Offset(ColumnCoffset:=2).Value=AskForValidDateTime
如果结束
端接头
私有函数AskForValidDateTime()作为日期
Dim作为布尔值有效
直到有效为止
作为变量的模糊结果
结果=Application.InputBox(“怪物基因是什么?”&vbNewLine和“Gebruik UU:MM”&vbNewLine和“Voorbeeld:09:30”,“Tijdnotatie”)
Dim SplitDateTime()作为字符串“从时间分割日期”
拆分日期时间