Home > Enterprise >  Copy Worksheet if Exist and Create new file if not
Copy Worksheet if Exist and Create new file if not

Time:10-05

hi can you help figure out how to copy worksheet if it existing, and if it is not will automatically create a new workbook then save as blank. please see my code below I try it in if the file is existing copy the file and if not create a new blank file.

Workbooks.Open path1
Sheets.Select

If Sheets("Draft") = "" Then
    Set wb = Workbooks.Add
    ActiveWorkbook.SaveAs saveFolder & "\D201\D201.xlsx", FileFormat:=51
    ActiveWorkbook.Close
Else
    Sheets("Draft").Copy
    ActiveWorkbook.SaveAs saveFolder & "\D201\D201.xlsx", FileFormat:=51
    Workbooks(file1).Close
    ActiveWorkbook.Close
End If

and I've encountered an error it says Subscript out of range

CodePudding user response:

Pretty sure you didn't try real hard there (given debugging the error thrown would have lead you to the obvious error).

  • Related