我正在使用Excel 2007并尝试创建一个代码,该代码将按当前日期的日期命名活动工作簿的特定工作表。到目前为止,我设法创建一个包含当月所有日期的数组,但是我将这个数组的元素应用为工作表名称时遇到了问题。代码行: ActiveWorkbook.Sheets(N).Name.Add = MyArray(N) 不起作用。任何改变代码的帮助都会非常感激,因为我是VBA的新手:
Sub CreateSheetsWithNamesAsDatesOfCurrentMonth()
Dim x1 As Date
Dim x2 As Integer
Dim BOM As Date
Dim x4 As Date
Dim x5 As Integer
Dim EOM As Date
Dim N As Integer
Dim DaysBetween As Integer
Dim MyArray() As Variant
Dim i As Long
x1 = DateSerial(Year(Now), Month(Now), Day(Now) + 1)
x2 = Day(DateSerial(Year(Now), Month(Now), Day(Now)))
BOM = x1 - x2
x4 = DateSerial(Year(Now), Month(Now) + 1, Day(Now))
x5 = Day(DateSerial(Year(Now), Month(Now) + 1, Day(Now)))
EOM = x4 - x5
DaysBetween = EOM - BOM
For N = 0 To DaysBetween
ReDim MyArray(N To DaysBetween)
MyArray(N) = DateAdd("d", N, BOM)
Next N
For N = 0 To LBound(MyArray)
ActiveWorkbook.Sheets(N).Name.Add = MyArray(N)
Next N
End Sub
答案 0 :(得分:1)
您的作业有误,您使用Add
Sheets
加入Name
集合:
ActiveWorkbook.Sheets.Add Name:=MyArray(N)
注意:数组中的值可能需要转换为String
类型,并确保删除/
之类的非法字符:
ActiveWorkbook.Sheets.Add Name:=Replace(Cstr(MyArray(N)), "/", vbNullString)