运行时错误9子标记超出范围和发票自动化

时间:2018-01-19 07:39:52

标签: excel vba excel-vba

我正在尝试运行一些VBA代码来生成自动发票,但我收到以下错误:

  

错误9下标超出范围

代码。

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

知道可能导致这种情况的原因吗?

Private Sub CommandButton1_Click()
Dim customername As String
Dim customeraddress As String
Dim invoicenumber As Long
Dim r As Long
Dim mydate As String
Dim path As String
Dim myfilename As String
lastrow = Sheets(“CustomerDetails”).Range(“A” & Rows.Count).End(xlUp).Row
r = 2
For r = 2 To lastrow
If Cells(r, 17).Value = “done” Then GoTo nextrow

customername = Sheets(“CustomerDetails”).Cells(r, 1).Value
customeraddress = Sheets(“CustomerDetails”).Cells(r, 2).Value
invoicenumber = Sheets(“CustomerDetails”).Cells(r, 6).Value
quantity = Sheets(“CustomerDetails”).Cells(r, 18).Value
Description = Sheets(“CustomerDetails”).Cells(r, 19).Value
UnitPrice = Sheets(“CustomerDetails”).Cells(r, 20).Value
SalesTaxRate = Sheets(“CustomerDetails”).Cells(r, 16).Value

Cells(r, 17).Value = “done”
Application.DisplayAlerts = False
Workbooks.Open (“C \ invoices \ BasicInvoice.xlsx”)
ActiveWorkbook.Sheets(“BasicInvoice”).Activate
ActiveWorkbook.Sheets(“BasicInvoice”).Range(“I8”).Value = invoicenumber
ActiveWorkbook.Sheets(“BasicInvoice”).Range(“C8”).Value = customername
ActiveWorkbook.Sheets(“BasicInvoice”).Range(“C9”).Value = customeraddress
ActiveWorkbook.Sheets(“BasicInvoice”).Range(“B21”).Value = quantity
ActiveWorkbook.Sheets(“BasicInvoice”).Range(“C21”).Value = Description
ActiveWorkbook.Sheets(“BasicInvoice”).Range(“H21”).Value = UnitPrice
ActiveWorkbook.Sheets(“BasicInvoice”).Range(“D18”).Value = SalesTaxRate

path = “C \ invoices \ ”
mydate = Date
mydate = Format(mydate, “mm_dd_yyyy”)
 ActiveWorkbook.SaveAs Filename:=path & invoicenumber & “ - ” & 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

1 个答案:

答案 0 :(得分:0)

这是我在个人宏工作簿中保存的功能:

Function LastRow(ByVal ws As Worksheet, Optional ByVal col As Variant = 1) As Long
    With ws
        LastRow = .Cells(.Rows.Count, col).End(xlUp).Row
    End With
End Function

这是我每天使用的东西,这就是为什么它在那里,你可能想要做同样的事情,如果它导致你的问题

所以完全摆脱那条线,将上面的代码复制到你的模块,然后将你使用lastrow的任何内容复制,更新它以包含工作表。

您可以这样调用此函数(假设您没有为您的工作簿声明):

... = LastRow(Worksheets("CustomerDetails"))

编辑:我必须完全重写你的代码,所以请确保一切正常

<强>代码:

Option Explicit

Function LastRow(ByVal ws As Worksheet, Optional ByVal col As Variant = 1) As Long
    With ws
        LastRow = .Cells(.Rows.Count, col).End(xlUp).Row
    End With
End Function

Private Sub CommandButton1_Click()

    Const path$ = "C:\invoices\"

    Dim customername As String, customeraddress As String, invoicenumber As Long
    Dim r As Long, mydate As String, myfilename As String, wbInv As Workbook
    Dim quantity, Description, UnitPrice, SalesTaxRate
    Dim tempWB As Workbook
    Dim wsCD As Worksheet

    Set wsCD = ThisWorkbook.Worksheets("CustomerDetails")

    For r = 2 To LastRow(wsCD)

        If Not Cells(r, 17).Value = "done" Then

            With wsCD

                customername = .Cells(r, 1).Value
                customeraddress = .Cells(r, 2).Value
                invoicenumber = .Cells(r, 6).Value
                quantity = .Cells(r, 18).Value
                Description = .Cells(r, 19).Value
                UnitPrice = .Cells(r, 20).Value
                SalesTaxRate = .Cells(r, 16).Value
                .Cells(r, 17).Value = "done"

            End With

            Application.DisplayAlerts = False
            Set wbInv = Workbooks.Open("C:\invoices\BasicInvoice.xlsx")

            With wbInv.Worksheets("BasicInvoice")
                .Range("I8").Value = invoicenumber
                .Range("C8").Value = customername
                .Range("C9").Value = customeraddress
                .Range("B21").Value = quantity
                .Range("C21").Value = Description
                .Range("H21").Value = UnitPrice
                .Range("D18").Value = SalesTaxRate
            End With

            mydate = Format(Date, "mm_dd_yyyy")
            wbInv.SaveAs Filename:=path & invoicenumber & " - " & customername & _
                    " - " & mydate & ".xlsx"
            myfilename = ActiveWorkbook.FullName
            SetAttr myfilename, vbReadOnly
            Application.DisplayAlerts = True
            'ActiveWorkbook.PrintOut copies:=1
            wbInv.Close SaveChanges:=False
            Set wbInv = Nothing
            Set tempWB = Nothing

        End If

    Next r

End Sub