As it stands now I have it written up where the code creates a file in the folder that the original file exists in. Once the file is created, I want to have it where wbBK2 is defined as the file that was just created. I figured using the same code to create the file, minus the SaveAs option would work, but it seems that it doesn’t like the object.
Is there a reason why? For all intended purposes it should be pointing directly at the file, but it seems to not agree… can someone see the mistake I’ve made?
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
As already hinted at in the comments
Dir & Application.PathSeparator & "Open Order Report -" & Format(Date, "mm-dd-yyyy") & ".xlsx"does not create an object. That code returns a string, which is an ordinary data type in VBA.The error occurs when you use
set wbBK2 =the code above, because set requires an object to reference.As an aside, as Siddharth Rout already pointed out, you actually correctly created the reference to the work book with this line
Set wbBK2 = Workbooks.Addso a further set line is unnecessary.