VBA日期保持为美国人

时间:2016-11-16 09:31:59

标签: vba date

我对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月16日”,而不是“12月16日”

2 个答案:

答案 0 :(得分:0)

根据我的测试,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)

答案 1 :(得分:0)

这个宏要求在考勤记录的头部打印日期,在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