简体   繁体   中英

Run time error 9 subcript out of range and invoice automation

I am trying to run some VBA code to generate an automated invoice, but I am receiving the following error:

Error 9 subscript out of range

for this code.

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

Any idea what could be causing this?

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

This is the function I have saved in my personal macro workbook:

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

It is something that I use daily which is why it's there, and you might want to do the same if it's causing you problems

so get rid of that line completely, copy the above code to your module, then whatever you were using lastrow for, update it to include the worksheet.

You would call this function like this (assuming that you don't have a declaration for your workbook):

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

Edit: I had to completely rewrite your code, so please make sure everything works

Code:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM