Vba操纵返回的工作簿对象

时间:2016-06-11 12:20:13

标签: excel vba function excel-vba object

我无法将工作表添加到从函数

返回的工作簿对象中

我有以下vba代码:

    For i = 0 To Uniq_M_Unit.Count
        'Chenge data type of collection item to string
         wrkb_nameas = CStr(Uniq_M_Unit(i))
         'Call function that creates the workbooks passing the name of the workbook as parameter
         Set wrbook = AddNewWorkbook(wrkb_nameas)
        'Add new worksheet to cuurrent opened workbook
        wrbook.Sheets.Add.Name = "XXXX"

    Next

功能:

    Public Function AddNewWorkbook(Bar As String) As Workbook
       Set MyFunction = Workbooks.Add
       MyFunction.SaveAs Filename:="C:\Users\gematzab\Desktop\" & Bar & ".xlsx"
    End Function

1 个答案:

答案 0 :(得分:2)

将您的代码更改为以下内容:

 Public Function AddNewWorkbook(Bar As String) As Workbook
       Set AddNewWorkbook= Workbooks.Add
       AddNewWorkbook.SaveAs Filename:="C:\Users\gematzab\Desktop\" & Bar & ".xlsx"
End Function