繁体   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