2014-12-22 87 views
1

我想从Excel中的客户数据列表创建自动发票。我有宏和一些VBA的经验,但没有深入过。我复制了我想要的基本模板here。我复制了大部分结构并改变了我需要的东西,但它不适合我。我试图独立完成并研究其他方法,但恐怕我现在已经不知所措了。这是我的代码,我得到'运行时错误1004'。如果有人能告诉我需要修复它,我将不胜感激。试图创建自动发票Excel VBA

代码:

Private Sub CommandButton1_Click() 

Dim customer As String 
Dim customerid As String 
Dim invoicenumber As Long 
Dim r As Integer 
Dim mydate As String 
Dim path As String 
Dim myfilename As String 
Dim providercount As Integer 
Dim basefee As Integer 
Dim faxlines As Integer 
Dim faxpages As Integer 
Dim faxbundles As Integer 
Dim invoicedate As String 
Dim lastrow As Long 

'This row is causing the error 
lastrow = Sheets("Greenway").Range(“A” & Rows.Count).End(xlUp).Row 


r = 8 


For r = 8 To lastrow 


If Cells(r, 14).Value = “done” Then GoTo nextrow 


customer = ThisWorkbook.Sheets(“Greenway”).Cells(r, 3).Value 
customerid = ThisWorkbook.Sheets(“Greenway”).Cells(r, 2).Value 
providercount = ThisWorkbook.Sheets(“Greenway”).Cells(r, 5).Value 
basefee = ThisWorkbook.Sheets(“Greenway”).Cells(r, 6).Value 
faxlines = ThisWorkbook.Sheets(“Greenway”).Cells(r, 7).Value 
faxpages = ThisWorkbook.Sheets(“Greenway”).Cells(r, 10).Value 
faxbundles = ThisWorkbook.Sheets(“Greenway”).Cells(r, 11).Value 
invoicenumber = ThisWorkbook.Sheets(“Greenway”).Cells(r, 15).Value 
invoicedate = ThisWorkbook.Sheets(“Greenway”).Cells(r, 16).Value 

Cells(r, 14).Value = “done” 
Application.DisplayAlerts = False 
Workbooks.Open ("C:\Users\Andrew\Dropbox (Updox)\All Company\DEPT-Finance\Billing\Greenway\Invoices\2015Invoices\Template.xlsx") 
ActiveWorkbook.Sheets(“invoice”).Activate 

ActiveWorkbook.Sheets("Invoice").Range("E7").Value = customername 
ActiveWorkbook.Sheets("Invoice").Range("E5").Value = invoicenumber 
ActiveWorkbook.Sheets("Invoice").Range("A16").Value = providercount 
ActiveWorkbook.Sheets("Invoice").Range("A17").Value = faxlines 
ActiveWorkbook.Sheets("Invoice").Range("A18").Value = faxpages 
ActiveWorkbook.Sheets("Invoice").Range("A19").Value = faxbundles 
ActiveWorkbook.Sheets("Invoice").Range("E8").Value = customerid 
ActiveWorkbook.Sheets("Invoice").Range("D16").Value = basefee 
ActiveWorkbook.Sheets(“invoice”).Range("E4").Value = invoicedate 

path = "C:\Users\Andrew\Dropbox (Updox)\All Company\DEPT-Finance\Billing\Greenway\Invoices\2015Invoices\" 
mydate = Date 
mydate = Format(mydate, “mm.yy”) 

ActiveWorkbook.SaveAs Filename:=path & “_” & customername & “_” & mydate & “.xlsx” 
myfilename = ActiveWorkbook.FullName 
SetAttr myfilename, vbReadOnly 
Application.DisplayAlerts = True 
ActiveWorkbook.PrintOut copies:=1 

ActiveWorkbook.Close SaveChanges:=False 

nextrow: 

Next r 


End Sub  
+0

什么行会抛出错误? – Degustaf

+0

This one:lastrow = Sheets(“Greenway”)。Range(“A”&Rows.Count).End(xlUp).Row – asutter

+0

http://stackoverflow.com/questions/11169445/error-finding-last-used -cell-in-vba/11169920#11169920可能有兴趣。 – pnuts

回答

0

也许你应该试试这个,找到柱A中的最后一行,并解决 '运行时错误1004':

With Sheets("Greenway") 
    lastrow = .Range("A" & .Rows.Count).End(xlUp).Row 
End With 

或:

lastrow = Sheets("Greenway").Range("A" & Sheets("Greenway").Rows.Count).End(xlUp).Row 

而不是:

lastrow = Sheets("Greenway").Range(“A” & Rows.Count).End(xlUp).Row 

请注意Rows之前的点(.)分隔符,以指示您希望获取属性行的表格(“Greenway”)对象。


对于你所得到现在 '运行时错误9' 的错误,请尝试:

customer = Sheets("Greenway").Cells(r, 3).Value 

代替:

customer = ThisWorkbook.Sheets(“Greenway”).Cells(r, 3).Value 

希望它是非常有用的!

+0

谢谢你解决了这个问题!但是现在我在这条线上得到'运行时错误9':customer = Sheets(“Greenway”)。Cells(r,3)。值 – asutter

+0

@asutter:我更新了答案以解决运行时错误9.如果它的工作原理,你应该适用于其他变量customerid,providercount,basefee等相同的解决方案 – Academia

+0

真棒一切工作,现在感谢! – asutter