我有一个do until循环,它使用布尔值和输入框。试图从输入循环中获取一个str变量

时间:2016-11-18 01:36:54

标签: excel vba excel-vba

在Visual Basics中,我有一个直到循环,以确保以正确的方式输入日期。如何在输入框中输入用户输入并将其另存为日期变量?

Private Sub cmdbtn3_Click()

'create a booleanvariable
Dim blDone As Boolean

'ask for there name
strName = InputBox("Please Enter Your Name", "Step 1")

'create a do while not because we want a repeat until blDone is True
Do While Not blDone

    'set the value of the input box with the product list equal to the boolean
    'ask for the date of the start, have a do loop until date1
    blDone = InputBox("Please Enter the Estimated Starting Date of CU" & vbNewLine & "Example 05/24/2014") Like "##/##/####"


    'Create a msg that explains what the input must be
    If Not blDone Then MsgBox "the input didn't match the pattern '01/23/2014' where:" _
                              & vbNewLine & vbTab & "'01' must bethe month" _
                              & vbNewLine & vbTab & "'23' must be the date" _
                              & vbNewLine & vbTab & "'2014' must be the year"

'I need to save the value of the date as

Loop

End Sub

1 个答案:

答案 0 :(得分:2)

存储输入,然后对其进行比较。如果dateInput设置为Date类型,那么您的检查会失败,因为01/01/2000已更改为1/1/2000。我通过在Variant中捕获它来检查它,然后转换它并在最后存储在dateOutput中来解决这个问题。

Private Sub cmdbtn3_Click()

'create a booleanvariable
Dim blDone As Boolean, dateInput As Variant, dateOutput As Date

'ask for there name
strName = InputBox("Please Enter Your Name", "Step 1")

'create a do while not because we want a repeat until blDone is True
Do While Not blDone

    'set the value of the input box with the product list equal to the boolean
    'ask for the date of the start, have a do loop until date1
    dateInput = InputBox("Please Enter the Estimated Starting Date of CU" & vbNewLine & "Example 05/24/2014")

    blDone = dateInput Like "##/##/####"

    'Create a msg that explains what the input must be
    If Not blDone Then MsgBox "the input didn't match the pattern '01/23/2014' where:" _
                              & vbNewLine & vbTab & "'01' must bethe month" _
                              & vbNewLine & vbTab & "'23' must be the date" _
                              & vbNewLine & vbTab & "'2014' must be the year"

'I need to save the value of the date as

Loop

dateOutput = CDate(dateInput)

End Sub