Excel宏有时会中断吗?

时间:2015-07-08 10:46:21

标签: excel vba excel-vba

我已经制作了一个宏来复制excel电子表格但是当它运行一次它不会再次运行?

Sub Add_Week()
    'copy worksheet
    Dim test As Worksheet
    Sheets(1).Copy After:=Sheets(Sheets.Count)
    Set test = ActiveSheet

    'extend date
    jamesdata = Range("D2")
    Firstdate = DateValue(jamesdata)
    seconddate = DateAdd("w", 7, Firstdate)
    Range("D2").Select
    ActiveCell.FormulaR1C1 = seconddate

    'change sheet name
    newname = Range("D2").Text
    test.Name = newname

End Sub

1 个答案:

答案 0 :(得分:1)

尝试使用此代码。

Sub Add_Week()

    Dim newDate As String

    'Copy the last sheet
    Sheets(Sheets.Count).Copy After:=Sheets(Sheets.Count)

    'Extend date
    newDate = DateAdd("w", 7, DateValue(Range("D2")))

    'The main problem is here, because sheet name can't accept "/, \ and single quote"
    'Return value by "DateAdd" method include slash by default.
    'So, I change it to "dd-mmm-yy" format. If you need others, modify format.
    'But it need to valid for sheet name.

    'Change data format to desire format (8-Jul-15)
    newDate = Format(newDate, "dd-mmm-yy")

    'Set date value
    Range("D2") = newDate

    'Change sheet name
    ActiveSheet.Name = newDate

End Sub