我无法将工作表添加到从函数
返回的工作簿对象中我有以下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
答案 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