我制作了一个简单的宏来将数据从一个标签移动到另一个标签,并添加日期以简化我们构建日记的过程。
然而,当我要求输入日期时 - 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
答案 0 :(得分: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