【问题标题】:Excel VBA Input Date and then Output Date in UK FormatExcel VBA 输入日期,然后以英国格式输出日期
【发布时间】:2015-04-08 10:09:31
【问题描述】:

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

但是,当我要求输入日期时 - 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

【问题讨论】:

  • 不清楚您的问题到底是什么。什么“惯用伎俩”?请显示一些示例输入、所需输出、当前输出,并指出当前输出与所需输出有何不同。 A Minimal, Complete, and Verifiable example 总是有帮助,截图也是如此。
  • “通常的技巧”是在存储日期时将所有日期更改为美国日期。我希望它在输入框中输入英国日期,并且输出与英国日期相同。

标签: vba date excel


【解决方案1】:

这对我有用:

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

【讨论】:

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