格式在Excel

时间:2017-03-29 01:15:38

标签: excel excel-vba vba

我在用户表单中动态添加TextBox,并希望使它们成为日期格式以确保正确输入日期。我无法找到任何例子。

以下是用户表单激活的代码:

Private Sub TextBox3_Exit(ByVal Cancel As MSForms.ReturnBoolean)

fltDays = TextBox3.Value If TextBox3.Value = 0 Then Exit Sub

For i = 1 To fltDays

n = i - 1

Dim TextBox As Control

Set theLbl = FloatDayFrm.Controls.add("Forms.Label.1", "lbl_" & i, True)
    With theLbl
      .Caption = "Day " & i
      .Left = 20
      .Width = 60
      .Top = n * 24 + 100
      .Font.Size = 10
    End With


Set TextBox = FloatDayFrm.Controls.add("Forms.TextBox.1", "TextBox_" & n, True)
    With TextBox
        .Top = 100 + (n * 24)
        .Left = 90
        .Height = 18
        .Width = 50
        .Name = "txtBox" & i
        .Font.Size = 10
        .TabIndex = n + 4
        .TabStop = True
    End With Next i

FloatDayFrm.Height = 150 + fltDays * 24 With btnOK .Top = 102 + fltDays * 24 .TabStop = True .TabIndex = n + 5 End With

With btnCancel .Top = 102 + fltDays * 24 '.TabStop = True .TabIndex = n + 6 End With

End Sub

这是我的Command按钮代码:

Private Sub btnOK_Click()

n = TextBox3.Value

For j = 1 To n

Set varFloatDay = FloatDayFrm.Controls("txtBox" & j)

Select Case varFloatDay
    Case ""
        MsgBox "Day " & j & " can't be blank", vbOKOnly, "Incorrect Value"
        Exit Sub
    Case Is > TextBox2.Value
         MsgBox "Date is after end date", vbOKOnly, "Incorrect Value"
        Exit Sub
    Case Is < TextBox1.Value
         MsgBox "Date is BEFORE end date", vbOKOnly, "Incorrect Value"
        Exit Sub
End Select

Next j

End Sub

任何帮助都将不胜感激。

2 个答案:

答案 0 :(得分:1)

文本框中的任何输入都是文本字符串。如果您希望它是一个日期,您可以使用IsDate(TextBox1.Value)来确定VBA是否能够将字符串转换为日期(这是一个Double类型的数字)。但是,VBA不会100%正确执行此测试。例如,如果您的区域设置将日期分隔符作为句点,则可能无法将3/2/17识别为日期。如果您的区域设置为mm.dd.yy,它可能会将3.2.17转换为3月2日。在自己的PC上工作时,您可以控制区域设置。但是,如果您的项目将被释放,那么最好使用日历控件来获得正确的日期。

答案 1 :(得分:1)

您必须将文本转换为日期格式。您可以使用多种方法。

  1. 在文本框旁边添加标签,以显示用户必须指定日期的格式。按照格式解析用户指定的文本。执行验证和转换,如下面的代码所示。
  2. 使用日历控件代替文本框作为用户输入。
  3. 包含年,月,日的单独文本框或单元格。执行验证和转换,如下面的代码所示。
  4. 如果您确定日期符合区域设置的指定格式。执行验证和转换,如下面的代码所示。
  5. 尝试以下

    Private Sub TestDate()
        Dim yr As Integer
        Dim mnth As Integer
        Dim day As Integer
        Dim dt As Date
        Dim strDate As String
    
        '''''3rd approach''''''
        yr = ActiveSheet.Range("A1")
        mnth = ActiveSheet.Range("B1")
        day = ActiveSheet.Range("C1")
        If IsNumeric(yr) And IsNumeric(mnth) And IsNumeric(day) Then
            If yr < 0 Or mnth < 0 Or day < 0 Then
                MsgBox "Year, Month and Day must be greater than 0."
                Exit Sub
            End If
        Else
            MsgBox "Year, Month and Day must be an integer."
            Exit Sub
        End If
        'convert to Date
        dt = DateSerial(yr, mnth, day)
    
        '''''4th approach''''''
        'Display a date according to your system's short date format
        'i.e. regional settings in control panel
        strDate = Format(ActiveSheet.Range("D1"), "Short Date")
        If Not IsDate(strDate) Then
            MsgBox "Incorrect Date Format"
            Exit Sub
        End If
        dt = CDate(strDate)
    End Sub