【问题标题】:Data control from textbox and inverted day/month values来自文本框和倒置日/月值的数据控制
【发布时间】:2012-02-04 12:31:59
【问题描述】:

我需要检查在文本框中输入的日期是否有效。它必须是单个文本框,因此没有解决方法。 现在,我有这个代码:

Private Sub cmdOK_Click()
Dim dataAnalisi As Date
If IsDate(txtDataAnalisi.Value) Then
dataAnalisi = txtDataAnalisi.Value
    Dim giornoAnalisi, meseAnalisi As Integer
    giornoAnalisi = Format(dataAnalisi, "dd")
    meseAnalisi = Format(dataAnalisi, "mm")
    If giornoAnalisi <= 31 And meseAnalisi <= 12 Then
        Call arrayList(dataAnalisi)
        Unload Me
    Else
        GoTo DateError
    End If
Else
DateError:
    MsgBox "Inserire una data formattata correttamente!", vbCritical, "Errore nell'inserimento!"
    txtDataAnalisi.SetFocus
End If
End Sub

对不起,如果它有意大利语文本。该函数运行良好,唯一的问题是,如果我输入例如 11/14/12(其中日期为 dd/mm/yy 并且 14 是错误类型),它会反转日期和月份值。相反,我希望 sub 告诉用户再次检查他的输入!你能帮助我吗?谢谢!

【问题讨论】:

  • 什么是调用arrayList 调用?你能提供一个工作表的例子吗?
  • 即使它必须是单个文本框,您也可以使用弹出的日期选择器来绝对控制结果

标签: forms excel vba date textbox


【解决方案1】:

每个月左右都有这个问题的变化。我相信 Excel 会将有效的美国日期视为美国日期。我多年来一直这么认为,但其他人不同意。

我使用下面的函数来检查我认为 Excel 会误解的格式并将它们转换为明确的格式。

我用英文缩写表示月份。我相信法语是几个月内唯一不允许使用三个字符缩写的语言,所以也许你有自己的一套。您必须根据您的要求调整该部分的例程。

希望这会有所帮助。

Function MyDateValue(ByVal DateIn As String, ByRef DateOut As Date) As Boolean

  ' DateIn is a value to be checked as a valid date.
  ' If it is a valid date, DateOut is set to its value and the function
  ' returns True.

  ' Excel misinterprets dates such as "4/14/11" as 14 April 2011.  This routine
  ' checks for such dates and, if necessary, changes them to an unambiguous
  ' format before calling IsDate and DateValue.

  Dim DatePart() As String
  Dim MonthNum As Long

  Const MonthAbbr As String = "janfebmaraprmayjunjulaugsepoctnovdec"

  ' Replace popular delimiters with Microsoft standard
  DateIn = Replace(DateIn, "-", "/")
  DateIn = Replace(DateIn, "\", "/")

  DatePart = Split(DateIn, "/")
  If UBound(DatePart) = 2 Then
    ' DateStg is three values separated by delimiters
    ' Check middle part
    If IsNumeric(DatePart(1)) Then
      MonthNum = Val(DatePart(1))
      If MonthNum >= 1 And MonthNum <= 12 Then
        ' Middle part could be numeric month
        ' Convert to format Excel does not misinterpret
         'Debug.Assert False
        DatePart(1) = Mid(MonthAbbr, ((MonthNum - 1) * 3) + 1, 3)
        DateIn = Join(DatePart, "-")
        If IsDate(DateIn) Then
          DateOut = DateValue(DateIn)
          MyDateValue = True
          Exit Function
        End If
      Else
        ' Middle part cannot be a month
        'Debug.Assert False
        MyDateValue = False
        Exit Function
      End If
    Else
      'Debug.Assert False
      ' The middle part is not a number.  It could be a month abbreviation
      MonthNum = InStr(1, MonthAbbr, LCase(DatePart(1)))
      If MonthNum = 0 Then
        ' The middle portion is neither a month number nor a month abbreviation
        Debug.Assert False
        MyDateValue = False
      Else
        ' The middle portion is a month abbreviation.
        ' Excel will handle date correctly
        'Debug.Assert False
        MonthNum = (MonthNum - 1) / 3 + 1
        DateIn = Join(DatePart, "-")
        If IsDate(DateIn) Then
        'Debug.Assert False
          DateOut = DateValue(DateIn)
          MyDateValue = True
          Exit Function
        End If
      End If
    End If
  Else
    '    Debug.Assert False
    ' Use IsDate for other formats
    If IsDate(DateIn) Then
       ' Debug.Assert False
      DateOut = DateValue(DateIn)
      MyDateValue = True
      Exit Function
    Else
      '  Debug.Assert False
      MyDateValue = False
    End If
  End If

End Function

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-05
    • 1970-01-01
    • 2019-04-25
    • 2021-11-16
    • 1970-01-01
    • 2020-08-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多