添加工作表和日期时出错

时间:2018-06-02 16:27:53

标签: vba excel-vba excel

我在工作簿中添加了一个名为" xxx"今天约会,例如。 xxx 6.2 使用vba但不允许。我收到错误400

这是我的代码

  Dim SheetName As String
  Dim dt As Date
  dt = Data
  SheetName = "xxx "
  'Dim WS As Worksheet
  Sheets.Add(, ActiveSheet).Name = SheetName & dt

1 个答案:

答案 0 :(得分:0)

请尝试以下代码:

Sheets.Add(, ActiveSheet).Name = SheetName & dt

在语法上是不正确的,因此你的错误。

Option Explicit

Sub test()

    Dim SheetName As String
    Dim dt As Date
    'dt = "2018-06-02" '<== asssign date value to variable
    dt = Now    '<== Today's date
    SheetName = "xxx "
    Dim WS As Worksheet
    Set WS = Sheets.Add '<== use Set for new worksheet object
    WS.Name = SheetName & Format$(dt, "yyyy-mm-dd") '<==cannot have "/" in name so use format to ensure outputs in legal way

End Sub