基于InputBox在新工作簿上为工作表名称添加值

时间:2014-04-29 16:09:02

标签: excel vba excel-vba

这是我到目前为止所得到的:

Option Explicit

Sub CreateNewBookWithDaysInMonthSheets()
Dim WS As Worksheet, WB As Workbook
Dim MonthX As Date, Control As Variant, DaysInMonth As Byte, i As Byte

    'create input box
Control = InputBox("Create new FTE workbook for period:", "New FTE", "mm/yyyy")
If IsDate(Control) Then
    'Create sheets based on days in selected month
    MonthX = CDate(Control)
    DaysInMonth = Day(DateSerial(Year(MonthX), Month(MonthX) + 1, 0))
    Application.SheetsInNewWorkbook = DaysInMonth
    Set WB = Workbooks.Add

    i = 1
    For Each WS In WB.Sheets
        WS.Name = i
        i = i + 1

        WB.SaveAs Filename:="FTE_" & Control & ".xlsm"

            'set default workbook count back to 3
            Application.SheetsInNewWorkbook = 3
    Next

Else
MsgBox "No period set"


End If

Set WS = Sheets.Add(Before:=Sheets("1"))
WS.Name = "Total for "


End Sub

现在我的问题是,如何添加" mm / yyyy"在输入框中输入到工作表名称"总计为"?所以这将是例如"总计2015年9月和#34;。

1 个答案:

答案 0 :(得分:0)

尝试:

WS.Name = "Total for " & replace(Control,"/",".")

这会将键入InputBox的值连接到" Total的总和为"串。

注意:您不能拥有多个具有相同名称的工作表,因此您可能希望将i的值添加到此结尾

WS.Name = "Total for " & replace(Control,"/",".") & i