日期格式 - 美国希望德国是默认Excel

时间:2018-01-26 10:49:22

标签: excel excel-vba date vba

我需要开发一个供全世界使用的模板,日期格式需要验证并显示为US-Format。问题是,我在德国工作,我试过这个:

Date Format

但是我希望它被验证为带有MM / DD / YYYY的美国格式,但是当它以该格式输入时,我得到无效日期,但我需要验证MM / DD / YYYY的值是否有人有个主意吗?

1 个答案:

答案 0 :(得分:1)

您可以在VBA中循环使用数据验证,并相应地格式化它们,然后您也可以提示用户是否输入了错误的日期,如下所示:

Sub foo()
Dim ws As Worksheet: Set ws = Sheets("Sheet1")
'declare and set your worksheet, amend as required
LastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
'get the last row with data on Column A

For i = 1 To LastRow
    On Error Resume Next
    If Not IsDate(ws.Cells(i, 1)) Then MsgBox "Incorrect date on row " & i
    'if not date then prompt user
    ws.Cells(i, 1).NumberFormat = "@"
    'format as text

    ws.Cells(i, 1) = Format(Month(ws.Cells(i, 1)), "00") & "/" & Format(Day(ws.Cells(i, 1)), "00") & "/" & Format(Year(ws.Cells(i, 1)), "0000")
    'format as US Date
Next i
End Sub