Excel VBA输入日期,然后以英国格式输出日期



我制作了一个简单的宏,用于将数据从一个选项卡移动到另一个选项卡,并添加日期,以简化构建日志的过程。

然而,当我要求输入日期时,VBA会把你的日期变成美国日期。我需要的是用户输入一个英国日期,输出的是相同的英国日期——然而,如果我输入一个美国日期,它就会显示为美国日期。

我能想到的唯一方法是以美国格式输入日期,并使用Format(str, "dd/mm/yyyy")以英国格式输出日期,但这并不理想。

一个更简单的方法将是一个很大的帮助,因为宏是为团队而不仅仅是我自己。

Dim wb As Workbook, wk1 As Worksheet, wk2 As Worksheet, rng As Range, str As String
Sub Journal()
Set wb = ThisWorkbook
Set wk1 = ActiveSheet
Set wk2 = wb.Sheets("CBCS Journal")
str = InputBox(Prompt:="What date would you like to post this journal for?", _
    Title:="Enter the posting date", Default:=Format(Now, "dd/mm/yyyy"))
For i = 1 To wk1.Range("A" & Rows.Count).End(xlUp).Row - 9
wk2.Range("B" & i + 1).Value = str
End Sub

这对我有效:

Dim str As String
Dim splitStr() As String
Dim strFormat As String
Dim d As Date
'Your preferred date format
strFormat = "dd/mm/yyyy"
'Get string representing date
str = InputBox(Prompt:="What date would you like to post this journal for?", _
               Title:="Enter the posting date", Default:=Format(Now, strFormat))
'Parse the date string
splitStr = Split(str, "/")
d = DateSerial(CInt(splitStr(2)), CInt(splitStr(1)), CInt(splitStr(0)))
'Display it in the proper format
With Range("A1")
    .Value = d
    .NumberFormat = strFormat
End With

最新更新