【发布时间】:2020-04-04 21:08:27
【问题描述】:
我正在尝试将数据从 excelsheet 导出到 excel 发票模板。我拥有的 VBA 代码将每一行视为不同的发票,因此为每一行制作不同的工作簿。如果我在 3 行中有 3 个产品的 1 张发票,此代码将每个产品(行)视为单独的发票,这是不正确的。我想修改它,如果发票编号(PiNo)在下一行重复,则意味着下一个产品(行)仅属于上述发票。我是 VBA 新手,因此我从另一个站点获取了代码。
Here is the code:-
Private Sub CommandButton1_Click()
Dim r As Long
Dim path As String
Dim myfilename As String
lastrow = Sheets(“CustomerDetails”).Range(“H” & Rows.Count).End(xlUp).Row
r = 2
For r = 2 To lastrow
ClientName = Sheets("CustomerDetails").Cells(r, 6).Value
Address = Sheets("CustomerDetails").Cells(r, 13).Value
PiNo = Sheets("CustomerDetails").Cells(r, 5).Value
Qty = Sheets("CustomerDetails").Cells(r, 9).Value
Description = Sheets("CustomerDetails").Cells(r, 12).Value
UnitPrice = Sheets("CustomerDetails").Cells(r, 10).Value
Salesperson = Sheets("CustomerDetails").Cells(r, 1).Value
PoNo = Sheets("CustomerDetails").Cells(r, 3).Value
PiDate = Sheets("CustomerDetails").Cells(r, 4).Value
Paymentterms = Sheets("CustomerDetails").Cells(r, 7).Value
PartNo = Sheets("CustomerDetails").Cells(r, 8).Value
Shipdate = Sheets("CustomerDetails").Cells(r, 14).Value
Dispatchthrough = Sheets("CustomerDetails").Cells(r, 15).Value
Modeofpayment = Sheets("CustomerDetails").Cells(r, 16).Value
VAT = Sheets("CustomerDetails").Cells(r, 17).Value
Workbooks.Open ("C:\Users\admin\Desktop\InvoiceTemplate.xlsx")
ActiveWorkbook.Sheets("InvoiceTemplate").Activate
ActiveWorkbook.Sheets("InvoiceTemplate").Range(“Z8”).Value = PiDate
ActiveWorkbook.Sheets("InvoiceTemplate").Range(“AG8”).Value = PiNo
ActiveWorkbook.Sheets("InvoiceTemplate").Range(“AN8”).Value = PoNo
ActiveWorkbook.Sheets("InvoiceTemplate").Range(“B16”).Value = ClientName
ActiveWorkbook.Sheets("InvoiceTemplate").Range(“B17”).Value = Address
ActiveWorkbook.Sheets("InvoiceTemplate").Range(“B21”).Value = Shipdate
ActiveWorkbook.Sheets("InvoiceTemplate").Range(“K21”).Value = Paymentterms
ActiveWorkbook.Sheets("InvoiceTemplate").Range(“T21”).Value = Salesperson
ActiveWorkbook.Sheets("InvoiceTemplate").Range(“AC21”).Value = Dispatchthrough
ActiveWorkbook.Sheets("InvoiceTemplate").Range(“AL21”).Value = Modeofpayment
ActiveWorkbook.Sheets("InvoiceTemplate").Range(“B25”).Value = PartNo
ActiveWorkbook.Sheets("InvoiceTemplate").Range(“J25”).Value = Description
ActiveWorkbook.Sheets("InvoiceTemplate").Range(“Y25”).Value = Qty
ActiveWorkbook.Sheets("InvoiceTemplate").Range(“AF25”).Value = UnitPrice
ActiveWorkbook.Sheets("InvoiceTemplate").Range(“AL39”).Value = VAT
path = "C:\Users\admin\Desktop\Invoices\"
ActiveWorkbook.SaveAs Filename:=path & PiNo & “.xlsx”
myfilename = ActiveWorkbook.FullName
ActiveWorkbook.Close SaveChanges:=True
Next r
End Sub
“H”是产品列,数据从第2行开始。第1行是标题。
感谢任何形式的帮助!
【问题讨论】:
-
不清楚要对重复发票编号的任何行做什么。这些行的内容应该放在哪里?
-
有一个名为 Invoice 的 Excel 工作簿,带有一个模板。我已将该书的单元格与这个启用宏的工作簿链接起来。一旦我运行宏,它将根据每行的数据在指定的模板中创建单独的工作簿。
-
youtu.be/iqOpR5POOKU 请参考这个你会知道这张表是关于什么的,我的问题是什么@TimWilliams
-
我很理解你的问题。我要问的是是否有第二行或第三行等具有相同的发票号码你希望你的代码用它做什么?
-
你的问题在引号里。您的机器上似乎有一个非英语字符集,有时您使用一个字符集中的引号,有时使用另一个字符集中的引号。后者是 VBA 无法理解的双角(2 字节)字符。用 ASCII Chr(34) 引号替换所有双角引号。它们有不同的形状。
ActiveWorkbook.Sheets("InvoiceTemplate").Range(“Z8”).Value = PiDate使用正确的字符。用于工作表名称,但用于范围名称的双字节。