我解决了一个相关的问题。我的工作手册仅供英国使用。它有一张表格用于输入在各个场所收集的现金细节。用户有两个单元格字段来标识每个场地;通常是位置和日期,但有时“日期”字段将包含扩展位置名称。 日期应为输入为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