我解決了一個相關的問題。我的工作手冊僅供英國使用。它有一張表格用於輸入在各個場所收集的現金細節。用戶有兩個單元格字段來標識每個場地;通常是位置和日期,但有時「日期」字段將包含擴展位置名稱。 日期應爲輸入爲dd/mm/yy,但幾乎任何可識別的東西都可接受,但 mm/dd/yy除外。 細節存儲在內存中,然後複製到格式化的工作表進行打印。我驗證了內存中的存儲空間。但在工作簿使用了幾個月之後,我發現如果用戶在格式爲dd/mm/[yy] yy(例如05/11/17)的單元格中輸入了有效日期,則其解釋爲mm/dd/[yy] yy也會給出一個有效的日期,那麼該日期會模糊地打印爲11-Mar,而不是11月5日。
一些代碼片段:
'Data structure:
Public Type BkItem 'An item of income, for banking.
ItemName As String 'The first field, just a text name.
ItemDate As Date 'The second field, interpreted as a date.
ItemDateNumber As Long 'The date as internally stored as an integer.
ItemDateString As String 'Re-formatted string, e.g. "05-Nov-17".
' ...
End Type 'BkItem.
'Input validation:
BankData = Range(.Cells(BankFirstRow, BankFirstCol), _
.Cells(BankLastItemLastRow, BankLastCol))
With BankItem(BankTotalItems)
.ItemName = IName
.ItemDateString = BankData(<row>, <col>)
.ItemDateNumber = DateToLong(.ItemDateString)
End With
'Utility routine. "Paper" is a 2-dimensional array of all the data to be printed
'on one or more pages; "Dest" is a global range.:
Sub OutputDataToSheet(ByVal Size As Long, ByRef CurrentSheet As String, _
ByRef Paper() As Variant)
Worksheets(CurrentSheet).Activate
Set Dest = Worksheets(CurrentSheet).Range((Cells(1, 1)), _
(Cells(Size, LastCol)))
Dest.Value = Paper 'Copy data to final sheet for printing.
End Sub 'OutputDataToSheet.
'As we build the array "Paper", it helps to format those cells on the final
'printout worksheet which are going to contain dates.
.Range(Cells(CurRow, L15c01), Cells(CurRow, L15c01)).NumberFormat = "dd-Mmm-yyyy"
'For the item date.
.Range(Cells(CurRow, L15c01), Cells(CurRow, L15c01)).HorizontalAlignment = xlCenter
If IsDate(BankItem(item).ItemDateString) Then
Paper(<row>, <col>) = BankItem(item).ItemDateNumber
'Date as a number, so OutputDataToSheet preserves UK date format.
Else
Paper(<row>, <col>) = BankItem(item).ItemDateString
'Extension of name.
End If 'IsDate(.ItemDateString).
您沒有太多的選擇。如果你想控制輸出,你需要更多的代碼 - 這真的不是那麼辛苦! – Rory
[Excel VBA - 將文本轉換爲日期?](https://stackoverflow.com/questions/20375233/excel-vba-convert-text-to-date)可能重複 – eirikdaude