2013-04-30 68 views
3

編輯:經過一些閱讀,我遇到了一個我在下面發佈的答案。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日

這是非常奇怪的。這就像單元格第一次填充它將其切換到美國的日期格式,但之後的任何事情都可以。我將嘗試在同事的機器上和我的家用電腦上測試,看看結果如何。

+0

No-repro。 VBA在隱式轉換中使用系統設置。請檢查應用於單元格的顯示格式。 – GSerg 2013-05-01 11:01:48

+0

@GSerg:應用於單元格的格式是Date> type * 14/03/2001,並且語言環境設置爲英語(U.K。)。我也試過14/03/2001,但沒有區別。試着在不同的機器上測試,看看我是否得到不同的結果。 – bawpie 2013-05-01 11:38:25

回答

2

好了,文章在這裏:http://allenbrowne.com/ser-36.html 我做了一些調整,對代碼:

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 'amendment 
    newVal = Format(Target.Value, "\ dd\/mm\/yyyy\") 

    Application.Undo 
    'oldVal = Target.Value 'amendment 
    oldVal = Format(Target.Value, "\ dd\/mm\/yyyy\") 
    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: 應用程序。enableEvents方法=真 結束子

最重要的一點似乎是格式:

oldVal = Format(Target.Value, "\ dd\/mm\/yyyy\") 

Allen的建議代碼爲:

Format$(varDate, "\#mm\/dd\/yyyy\#") 

我改成了DD/MM和替換#隨一個空間,嘿presto - 它似乎工作!儘管這是一個非常令人沮喪的問題,我現在必須確保在其他用戶機器上有效!感謝@ sous2817和@GSerg的意見/建議。

0

這似乎爲我工作......除非我想念理解你的問題....

newVal = Format(Sheet1.Range("A1"), "dd/mm/yyyy;@") 

代碼的全部:看完這個

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 
+0

謝謝@ sous2817,但使用它,如果我輸入一個日期到B2它只是消失完成(Excel 2007)?我已經更新了我的問題,試圖讓我的想法更清晰一些。 – bawpie 2013-05-01 09:33:21

+0

@bawpie:我編輯了我的帖子以包含整個方法。在我的測試表上,它的工作原理與您的想法完全相同。在B列中輸入一個日期:C,你會得到一個以逗號分隔的日期列表,這些列表中的日期已格式化爲更「UK」的格式。 – sous2817 2013-05-01 11:03:05

+0

這是非常奇怪的。我建立了一個全新的工作簿,在2013年1月1日至2013年12月31日的k列中列出日期列表,並將其命名爲「列表」。我已經進入數據驗證並將其應用到列B,告訴它允許列表,並將源設置爲=列表。單元格的格式是日期,* 14/03/2001,英國語言環境,但仍然從列表中選擇01/07/2013或自由輸入時,它將轉換爲07/01/2013。我將在今晚的家用電腦上試試這個功能,看它是否能夠複製。 (這是使用您的發佈代碼)。感謝你的協助! – bawpie 2013-05-01 11:26:31