簡體   English   中英

運行時錯誤9超出腳本范圍和發票自動化

[英]Run time error 9 subcript out of range and invoice automation

我正在嘗試運行一些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

這是我保存在個人宏工作簿中的功能:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM