从函数

时间:2016-04-26 14:29:30

标签: excel vba excel-vba

当我尝试从此函数返回新创建的工作簿对象时,我收到错误。我已经在SO上看过几个关于此的帖子,但大多数人似乎都指出使用Variant作为返回类型,并且我被告知这是一个很大的并不是一次又一次。 (坦率地说,我也不想使用变体数据类型)

错误文本:运行时错误' 91':对象变量或未设置块变量。我在SO上看过其他帖子也有错误,但似乎都没有#91;#39;

Private Function NewWorkbook_Open(ByVal clientName As String, ByVal startDateFromSheet As Date) As Workbook

    'Creates/formats new workbook and saves it to the xdrive without any completed
    Dim newWorkBook As Workbook
    Dim activeWorkbookName As String
    Dim formattedDate

    Workbooks.Add

    formattedDate = Replace(Format(startDateFromSheet, "mm/dd/yy"), "/", ".")

    'Saves workbook with new file name with date attached in saveable format
    ActiveWorkbook.SaveAs Filename:=XLS_CONFIRM_FILE_PATH & "-" & GetOfficialClientName(clientName) & " " & formattedDate & ".xls", FileFormat:=xlNormal


    NewWorkbook_Open = Workbooks(ActiveWorkbook.Name)

新的工作簿对象通过以下行在主例程中设置

Set newExcelConfirmBook = NewWorkbook_Open(.Cells(rowCounter,sellerFirmColumn).Value, startDateFromSheet)

2 个答案:

答案 0 :(得分:2)

如果您在代码末尾尝试这样做,那该怎么样:

set NewWorkbook_Open = Workbooks(ActiveWorkbook.Name)

有效吗?

答案 1 :(得分:1)

如果你不需要,请不要使用'主动...'。使用您正确声明的变量。

applicationDidFinishLaunching