格式功能在 excel vba 中不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25933116/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 04:35:47  来源:igfitidea点击:

Format function not working in excel vba

excel-vbavbaexcel

提问by user3345390

I am new to vba. i am trying to convert current date format to mm/dd/yyyy. I used format function to achieve it. Still i am getting as dd-mm-yyyy format. Dim current as Date

我是 vba 新手。我正在尝试将当前日期格式转换为 mm/dd/yyyy。我使用格式功能来实现它。我仍然是 dd-mm-yyyy 格式。将电流调暗为日期

currentDate = Format(Now, "MM/dd/yyyy")

Thanks in advance.

提前致谢。

采纳答案by Gary's Student

This is because Format()returns a String:

这是因为Format()返回一个字符串:

Sub NeedADate()
    Dim s As String
    s = Format(Now(), "mm/dd/yyyy")
    MsgBox s
End Sub

回答by robbertonline

First paste the desired date in a cell, then format the cell layout.

首先将所需日期粘贴到单元格中,然后格式化单元格布局。

Cells(1,1).Value = Now()
Cells(1,1).Numberformat = "mm/dd/yyyy"