2013-07-05 2 views
2

템플릿 및 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 

답변

0

시도 :

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 
+0

는 원래 코드를 잘못 무엇인지 알려 주시기 바랍니다 수 있습니까? –

+0

예! 잘 작동합니다. 고마워요! –

+0

@ NandiniJoshi 환영합니다 :) – Santosh

관련 문제