編輯:經過一些閱讀,我遇到了一個我在下面發佈的答案。VBA強制字符串變量爲英文日期格式
好的,所以我一直在尋找一塊VBA,可通過www.contextures.com。這段代碼使用戶能夠將多個條目添加到由逗號分隔的數據驗證列表中。該列表允許用戶從日期列表中進行選擇,並且還可以選擇一小部分文本條目。它的工作方式是用戶可以自由輸入日期,或使用選擇列表選擇日期或文本輸入。有時兩個日期可能需要進入單元格,因此下面的VBA允許用戶選擇/鍵入一個日期,按回車鍵,然後在同一單元格中鍵入另一個日期。只要他們一進入,前一個日期就會出現在單元格中,新的日期之後用逗號分隔。到現在爲止還挺好。
這是VBA的原始片段。現在
Option Explicit
' Developed by Contextures Inc.
' www.contextures.com
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rngDV As Range
Dim oldVal As String
Dim newVal As String
If Target.Count > 1 Then GoTo exitHandler
On Error Resume Next
Set rngDV = Cells.SpecialCells(xlCellTypeAllValidation)
On Error GoTo exitHandler
If rngDV Is Nothing Then GoTo exitHandler
If Intersect(Target, rngDV) Is Nothing Then
'do nothing
Else
Application.EnableEvents = False
newVal = Target.Value
Application.Undo
oldVal = Target.Value
Target.Value = newVal
If Target.Column = 3 Then
If oldVal = "" Then
'do nothing
Else
If newVal = "" Then
'do nothing
Else
Target.Value = oldVal _
& ", " & newVal
End If
End If
End If
End If
exitHandler:
Application.EnableEvents = True
End Sub
,我遇到的問題然而,該VBA工作,因爲我英國的,每當我進入英國的日期進入細胞的VBA適用於(如取01/06/2013年即dd/mm/yyyy),VBA將進行干預並將日期返還爲2013年6月1日(年/月/年)。我的控制面板設置都設置爲uk格式,所以我認爲這與VBA存儲方式有關,然後返回日期。我已經嘗試修改dims oldval和newval(到日期),但是VBA不允許我像以前一樣在一個單元中有多個條目(我猜vba不喜歡連接日期?)。
我已經修改了VBA到周圍(下面)將日期格式化爲非標準dd.mm.yyyy(dd/mm/yyyy不起作用)的黑客工作,但這需要我告訴用戶以這種格式輸入日期,我寧願不要這樣做(我寧願允許更多標準01/01/2013或01-01-2013兩者都被Excel識別爲日期):
Option Explicit
' Developed by Contextures Inc.
' www.contextures.com
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rngDV As Range
Dim oldVal As String
Dim newVal As String
If Target.Count > 1 Then GoTo exitHandler
On Error Resume Next
Set rngDV = Cells.SpecialCells(xlCellTypeAllValidation)
On Error GoTo exitHandler
If rngDV Is Nothing Then GoTo exitHandler
If Intersect(Target, rngDV) Is Nothing Then
'do nothing
Else
Application.EnableEvents = False
newVal = Format(Target.Value, "dd.mm.yyyy") 'amendment
Application.Undo
oldVal = Format(Target.Value, "dd.mm.yyyy") 'amendment
Target.Value = newVal
If Target.Column > 1 Then 'amendment
If oldVal = "" Then
'do nothing
Else
If newVal = "" Then
'do nothing
Else
Target.Value = oldVal _
& ", " & newVal
End If
End If
End If
End If
exitHandler:
Application.EnableEvents = True
End Sub
無論如何,任何意見/指針將不勝感激!我使用Excel 2007中,但電子表格需要與2003年
編輯兼容:數據的樣子是這樣的問題在這裏:Excel 2003 - How to count dates in row, including multiple dates in cell
編輯:我還要提到的是,如果我輸入01/07/2013到單元格的兩倍,它出來是這樣的:
2013年7月1日,2013年1月7日
這是非常奇怪的。這就像單元格第一次填充它將其切換到美國的日期格式,但之後的任何事情都可以。我將嘗試在同事的機器上和我的家用電腦上測試,看看結果如何。
No-repro。 VBA在隱式轉換中使用系統設置。請檢查應用於單元格的顯示格式。 – GSerg 2013-05-01 11:01:48
@GSerg:應用於單元格的格式是Date> type * 14/03/2001,並且語言環境設置爲英語(U.K。)。我也試過14/03/2001,但沒有區別。試着在不同的機器上測試,看看我是否得到不同的結果。 – bawpie 2013-05-01 11:38:25