テンプレートと Excel シートのデータに基づいて複数の請求書を生成し、保存しようとしています。次のコードで 1004 エラーがスローされます - アプリケーション定義またはオブジェクト定義のエラーです。助けてください。私はvbaが初めてです。
Sub AddNew()
Dim str1, str2, str3 As String
Dim numrows As Integer
Dim i As Integer
numrows = ActiveWorkbook.Sheets("Rawdata").Range("A" & Rows.Count).End(xlUp).Row - 2
MsgBox numrows
i = 3
While numrows > 0
str1 = ActiveWorkbook.Sheets("Rawdata").Cells(i, 16).Value
MsgBox (str1)
str2 = ActiveWorkbook.Sheets("Rawdata").Cells(i, 1).Value
'cannot save filename with backslash
str3 = Replace(ActiveWorkbook.Sheets("Rawdata").Cells(i, 2).Value, "/", "-")
Set NewBook = Workbooks.Add
With NewBook
.Title = "All Invoice"
.Subject = "Invoice"
.SaveAs Filename:="D:\Nandini\Invoice generation automation\" & str1 & " " & Format(str2, "mmm") & "-" & Format(str2, "YYYY") & " " & str3 & ".xlsx"
.Close SaveChanges:=True
End With
ActiveWorkbook.Sheets("Invoice").Select
Cells.Select
Selection.Copy
Workbooks.Open ("D:\Nandini\Invoice generation automation\" & str1 & " " & Format(str2,
"mmm") & "-" & Format(str2, "YYYY") & " " & str3 & ".xlsx")
activeworksheet.Paste
numrows = numrows - 1
i = i + 1
Wend
End Sub