把今天的excel表头格式化为“2016年3月3日”

一些谷歌search引导我这篇文章说,这是不可能的,没有一个macros,但它是2013年出版Excel的,也许他们是错的。 任何人都知道我没有macros可以做到这一点? 或者如果我必须做一个macros,是否有办法做到这一点,像在头部有一个自定义variables像&[todays_date_formatted]或我可以用VBA定义的东西?

没有VBA 使用公式把它放在一个单元格中:

=TEXT(NOW(),"mmmm d, yyyy") 

它解决了:

 March 4, 2016 

如果你想在VBA中做到这一点:

 'Place this function in a Standard Module Public Function todays_date_formatted() as String todays_date_formatted = Format(Now(), "mmmm d, yyyy") End Function 

然后使用以下公式从其他VBA或工作表中调用它:

 =todays_date_formatted() 

但是,在上述所有内容中,date都会转换为string,因此您无法直接使用date算术中的值, 并且结果可能因语言和地区而异。

正如其他人所build议的,我build议在其中使用=Now()的单元格中使用内置的数字格式。 如果您没有可用的内置数字格式,只需创build一个自定义格式,然后使用以下格式string:

 mmmm d, yyyy 

编辑 – 使其与页眉一起工作

这将更新名为Sheet1的工作表的标题,但可以将其更改为任何特定工作表的代号。

您可能需要使用包含此函数的公式来保留单元格(或定义的名称),以便计算时可以更新表头。

 Public Function todays_date_formatted() As String Dim sFormattedDate As String sFormattedDate = Format(Now(), "mmmm d, yyyy") 'Change 'Sheet1' to the codename of the sheet header you want to update Sheet1.PageSetup.CenterHeader = sFormattedDate todays_date_formatted = sFormattedDate End Function 

一张图片胜过千言万语: 在这里输入图像说明