【问题标题】:Generate multiple invoices based on a template and worksheet of data根据模板和数据工作表生成多张发票
【发布时间】:2013-07-05 10:24:16
【问题描述】:

我正在尝试根据模板和 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

【问题讨论】:

    标签: vba excel for-loop


    【解决方案1】:

    试试下面的代码:

    Sub AddNew()
        Dim str1, str2, str3 As String
        Dim numrows As Integer
        Dim i As Integer
        Dim NewBook As Workbook, oWkb As Workbook
    
        With ThisWorkbook.Sheets("Rawdata")
           numrows = .Range("A" & .Rows.Count).End(xlUp).Row - 2
           i = 3
    
            While numrows > 0
                str1 = .Cells(i, 16).Value
                str2 = .Cells(i, 1).Value
    
                'cannot save filename with backslash
                str3 = Replace(.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
    
                ThisWorkbook.Sheets("Invoice").Cells.Copy
                Set oWkb = Workbooks.Open("D:\Nandini\Invoice generation automation\" & str1 & " " & Format(str2, "mmm") & "-" & Format(str2, "YYYY") & " " & str3 & ".xlsx")
    
                oWkb.ActiveSheet.Range("A1").PasteSpecial
                numrows = numrows - 1
    
                i = i + 1
            Wend
    
         End With
    End Sub
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-05-01
      • 1970-01-01
      • 2017-05-14
      • 1970-01-01
      • 1970-01-01
      • 2020-02-14
      • 2011-03-08
      相关资源
      最近更新 更多