将日期设置为美国日期格式(不考虑区域设置)

使用 Visual Basic for Applications (VBA) 将日期连接到 SQL 字符串中时,必须使用标准的美国日期格式,而不考虑在 控制面板“区域设置”工具中设置的区域。 本主题包含一个将任何日期转换为美国日期格式的自定义函数。

Function MakeUSDate(DateIn As Variant) As String 
 
    ' Do nothing if the value is not a date. 
    If Not IsDate(DateIn) Then Exit Function 

    ' Format the date value as a U.S. date formatted string expression.
    MakeUSDate = "#" & Month(DateIn) & "/" & Day(DateIn) & "/" & Year(DateIn) & "#"

End Function

支持和反馈

有关于 Office VBA 或本文档的疑问或反馈? 请参阅 Office VBA 支持和反馈,获取有关如何接收支持和提供反馈的指南。