0

就目前而言,我已经将它写在代码在原始文件所在的文件夹中创建一个文件的位置。创建文件后,我希望将其 wbBK2 定义为刚刚创建的文件。我想使用相同的代码来创建文件,减去 SaveAs 选项会起作用,但它似乎不喜欢该对象。

有什么原因吗?出于所有预期目的,它应该直接指向文件,但似乎不同意......有人能看到我犯的错误吗?

Option Explicit

Sub OpenOrderReportExport()

    Dim wsJL As Worksheet   'Jobs List
    Dim wsPOT As Worksheet  'PO Tracking
    Dim wsTNO As Worksheet  'Tel-Nexx OOR
    Dim wbBK2 As Workbook   'New Workbook
    Dim wsWS1 As Worksheet  'Sheet1
    Dim wsWS2 As Worksheet  'Sheet2
    Dim wsWS3 As Worksheet  'Sheet3
    Dim CurrentFile As String, NewFileType As String, NewFile As String, Dir As String


    Set wsJL = Sheets("Jobs List")      'Jobs List
    Set wsPOT = Sheets("PO Tracking")   'PO Tracking
    Set wsTNO = Sheets("Tel-Nexx OOR")  'Tel-Nexx OOR
    Set wbBK2 = Workbooks.Add           'New Workbook
    Set wsWS1 = wbBK2.Sheets("Sheet1")  'Sheet1
    Set wsWS2 = wbBK2.Sheets("Sheet2")  'Sheet2
    Set wsWS3 = wbBK2.Sheets("Sheet3")  'Sheet3

        Application.ScreenUpdating = False    ' Prevents screen refreshing.
        CurrentFile = ThisWorkbook.FullName
        NewFileType = "Excel Files 2007 (*.xlsx)"
        Dir = ThisWorkbook.path
        wbBK2.SaveAs Dir & Application.PathSeparator & "Open Order Report -" & Format(Date, "mm-dd-yyyy") & ".xlsx"
        Set wbBK2 = Dir & Application.PathSeparator & "Open Order Report -" & Format(Date, "mm-dd-yyyy") & ".xlsx"

        MsgBox wbBK2
End Sub
4

1 回答 1

2

正如评论中已经暗示的那样,Dir & Application.PathSeparator & "Open Order Report -" & Format(Date, "mm-dd-yyyy") & ".xlsx"不会创建对象。该代码返回一个字符串,它是 VBA 中的普通数据类型。

当您使用set wbBK2 =上面的代码时会发生错误,因为 set 需要一个对象来引用。

顺便说一句,正如 Siddharth Rout 已经指出的那样,您实际上使用此行正确创建了对工作簿的引用,Set wbBK2 = Workbooks.Add因此不需要进一步设置行。

于 2012-09-05T14:31:00.420 回答