如何将输入框输入限制为仅限日期

时间:2015-12-02 17:24:24

标签: excel vba excel-vba

我想在下面的代码中实现一个控件,只允许使用mm / dd / yyyy格式的日期,并防止在输入框中输入空格。我尝试过添加一些代码,但一直遇到错误。第一个用于检查它是否为空白的控件,但第二个if Not似乎只是被跳过。

public class Level extends GameState {

public static int width, height;
Pallette tilePallette;

public static Map<Integer, Tile> tileMap = new HashMap<Integer, Tile>();

public static int[] tiles;

2 个答案:

答案 0 :(得分:3)

你的If语句对此有点偏僻。这是一个代码,它将删除GoTo的使用(这是最佳做法),并且仍然正确循环,直到您获得所需的格式。

Sub tt()
Dim ws1     As Worksheet
Set ws1 = Sheets("Report Generation")

With ws1
    Dim rptdate As Variant
    rptdate = InputBox("Please Enter the Report Date *Must be EOM format mm/dd/yyyy*", "Enter Date")

    Do While rptdate = "" Or Not IsDate(rptdate)
        MsgBox ("You did not enter a date in the correct format!"), vbCritical, Error
        rptdate = InputBox("Please Enter the Report Date *Must be EOM format mm/dd/yyyy*", "Enter Date")
    Loop

    .Range("B8").Value = rptdate
End With
End Sub

答案 1 :(得分:0)

以下准确地做了我想要的。谢谢findwindow。

'Updates the report date located in report generation tab cell B8
Dim ws1 As Worksheet
Set ws1 = Sheets("Report Generation")
ReTry:
With ws1
Dim rptdate As Variant
    rptdate = InputBox("Please Enter the Report Date *Must be EOM format mm/dd/yyyy*", "Enter Date")
    If rptdate = "" Then
    MsgBox ("You did not enter a date!"), vbCritical, Error
    GoTo ReTry
    ElseIf Not IsDate(rptdate) Then
    MsgBox ("You did not enter the correct format!"), vbCritical, Error
    GoTo ReTry
    Else
    Range("B8").Value = rptdate
    End If
End With