【问题标题】:VBA Date stays as AmericanVBA 日期保持为美国
【发布时间】:2017-03-30 10:23:26
【问题描述】:

我对 VBA 比较陌生,我需要一些关于我一直在编写的代码的帮助。目前,它看起来像这样:

Sub RoundedRectangle1_Click()
Selection.NumberFormat = "dd mmm yy"
Range("H2").ClearContents
 Dim Date1 As ValueChange
Range("H2").Value = InputBox("Enter the first date (Monday) of the week you wish to view, in the format DD/MM")
End Sub

如您所见,我有一个弹出框供用户手动输入日期,但由于某种原因,一旦输入,它会一直以美国格式提供答案,例如,如果我输入 04 /12,这将显示为“12 Apr 16”,而不是“04 Dec 16”

【问题讨论】:

  • 为什么不使用date picker?
  • 写 04/12/16 会正确显示吗?
  • @Vityata 不幸的是这不起作用。
  • @SBF 我无法下载东西,因为这是一个工作项目,这将违反政策

标签: vba date


【解决方案1】:

根据我的测试,InputBox 返回一个string。我要做的是编写以下函数(只是演示,此代码中没有错误处理):

Private Function ParseDate(sInput As String) As Date
    Dim sTmp() As String
    sTmp = Split(sInput, "/")

    ParseDate = DateTime.DateSerial(2016, sTmp(1), sTmp(0))
End Function

然后简单地这样称呼它:

 Dim sResult As String
 sResult = InputBox("Enter the first date (Monday) of the week you wish to view, in the format DD/MM")
 Range("H2").Value = ParseDate(sResult)

【讨论】:

  • 可能值得将 DateSerial 中的 2016 替换为 Year(Date()) 以保留当前年份。
  • 当然,但这就是为什么我说这只是一个演示:)
  • 不幸的是,@DarrenBartrup-Cook 也没有任何运气 - 证明这是一项令人沮丧的任务..
【解决方案2】:

此宏要求在考勤登记表的开头打印日期,适用于 21 世纪的 dd/mm/yy 或 dd/mm。可以很容易地适应包括 20 美分

Sub Print_Register()
'
' Print_Register Macro
Dim MeetingDate, Answer

    Sheets("Register").Select
    Range("A1").Select
GetDate:
    MeetingDate = DateValue(InputBox("Enter the date of the meeting." & Chr(13) & _
    "Note Format" & Chr(13) & "Format DD/MM/YY or DD/MM", "Meeting Date", , 10000, 10000))
    If MeetingDate = "" Then GoTo TheEnd
    If MeetingDate < 36526 Then MeetingDate = MeetingDate + 36525 'If no yy add year 2000
    Range("Current_Meeting_Date") = MeetingDate
    Answer = MsgBox("Date OK?", 3)
    If Answer = 2 Then GoTo TheEnd
    If Answer = 7 Then GoTo GetDate
    ExecuteExcel4Macro "PRINT(1,,,1,,,,,,,,2,,,TRUE,,FALSE)"
TheEnd:
End Sub

【讨论】:

    猜你喜欢
    • 2017-06-28
    • 1970-01-01
    • 1970-01-01
    • 2014-06-28
    • 1970-01-01
    • 2021-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多