VBAmacros和更改date格式为mmm-yy
我有一个Excel的macros(VBA)的问题,是从Excel电子表格中获取一个date,减去一个月,并重新格式化为MMM-YY。 基本上我想采取3/31/2013并将其转换为2月13日
这是我的代码:
Dim ReportDate As Date ReportDate = Worksheets("Current").Cells(2, 16) 'ex. 03-31-2013 prevMonth = Format((Month(ReportDate) - 1) & "/" & Day(ReportDate) & "/" & Year(ReportDate), "mmm") & "-" & Format(ReportDate, "yy") Debug.Print prevMonth
我得到的结果是2/31 / 2013-13
所以我试着改变prevMonthvariables:
prevMonth = Format((Month(ReportDate) - 1) & "/" & Day(ReportDate) & "/" & Year(ReportDate), "mmm-yy")
但是又一次得到了2/31/2013
我试图将prevMonth声明为Integer或Date,但是我得到一个types不匹配的错误。 我只能声明它是一个string,但它仍然没有帮助程序。
在此先感谢您的帮助。
尝试这个
Sub Demo() Dim ReportDate As Date Dim prevMonth As Date ReportDate = Worksheets("Current").Cells(2, 16) 'ex. 03-31-2013 prevMonth = DateAdd("m", -1, ReportDate) Debug.Print Format(prevMonth, "mmm-yy") End Sub