I have tried to create a new file and save the new file, however when i run the code, Error 9 pops up.

Sub openwba()

Dim wb As Workbook
Dim NewWB As String
NewWB = "C:\Users\Chengwu\Desktop\Train10_June01.xlsx"

Set wb = Workbooks.Add
wb.SaveAs Workbooks(NewWB)

End Sub

1 Answers

0
Error 1004 On

You could try:

Option Explicit

Sub Macro1()

    Dim wk As Workbook
    Dim NewWB As String

    If Dir("C:\Users\Chengwu\Desktop\Train10_June01.xlsx") = "" Then

        NewWB = "C:\Users\Chengwu\Desktop\Train10_June01.xlsx"
        Set wk = Workbooks.Add
        wk.SaveAs Filename:=NewWB

    End If

End Sub

Note:

  • Take the necessary safeguards in order to avoid error in case of file already exist.