将发票输入复制到单独的 sheet,将发票另存为 pdf 并重置发票 sheet
Copy invoice input to separate sheet, save the invoice as pdf & reset invoice sheet
我正在寻找 VBA 代码,它可以通过按钮将发票数据复制到单独的工作sheet(例如 "Save Invoice") ,一个将文件另存为 .pdf 的按钮,最后是一个带有宏的按钮,用于重置发票 sheet ("Reset invoice")。我尝试了一些 VBA 解决方案,但我似乎无法找到可行的解决方案。
- 每个新保存的输入都应保存在上一个保存的输出下方的空行中
- 应复制发票中的所有 inputs/rows如果它们包含数据
- 带有"Save as pdf"
的按钮
- 一个按钮到"clear the sheet"
代码:
Sub InvoiceToRecords()
LastRecordsRow = Worksheets("Invoice Data").UsedRange.Rows.Count
'determines the # of rows used
NewRecordsRow = LastRecordsRow + 1
'Row for pasting latest invoice will be 1 row below the end of the last invoice
Sheets("Invoice").Activate
Range("Invoice").Copy Sheets("Invoice Data").Range("D" & Rows.Count).End(xlUp).Offset(1, 0)
Range("Customer").Copy Sheets("Invoice Data").Range("A" & Rows.Count).End(xlUp).Offset(1, 0)
Range("Invoice Number").Copy Sheets("Invoice Data").Range("B" & Rows.Count).End(xlUp).Offset(1, 0)
Range("Invoice Date").Copy Sheets("Invoice Data").Range("C" & Rows.Count).End(xlUp).Offset(1, 0)
End Sub
Highlighted code
Error message
Syntax error
要复制次要数据的次数与发票明细行数一样多,您可以按以下方式进行,只需将您的代码替换为此代码即可:
Sub InvoiceToRecords()
Dim ws As Worksheet: Set ws = Worksheets("Invoice")
Dim wsData As Worksheet: Set wsData = Worksheets("Invoice Data")
'declare and set the worksheets, amend as required
Dim i As Long, dataRows As Long
dataRows = ws.Range("Invoice").Columns(1).SpecialCells(xlCellTypeConstants, 23).Count
'count the number of Invoice lines with data (non-empty)
ws.Range("Invoice").Copy wsData.Range("D" & Rows.Count).End(xlUp).Offset(1, 0)
'copy invoice lines to Invoice Data
For i = 1 To dataRows 'loop from 1 to however many lines your named range "Invoice" has
ws.Range("Customer").Copy wsData.Range("A" & Rows.Count).End(xlUp).Offset(1, 0)
ws.Range("Invoice Number").Copy wsData.Range("B" & Rows.Count).End(xlUp).Offset(1, 0)
ws.Range("Invoice Date").Copy wsData.Range("C" & Rows.Count).End(xlUp).Offset(1, 0)
Next i
End Sub
要将您的工作表保存为 PDF,请执行以下操作,我会使用某种变量来生成 PDF 文件名,这样您就不会一直覆盖同一个文件,可能是公司和发票编号的组合,或者即使是时间戳也可以:
Sub foo()
Dim ws As Worksheet: Set ws = Sheets("Sheet1")
Filen = "C:\Users\Lorenz\Desktop\NewPdf.pdf"
'amend filename & path to save above
ws.ExportAsFixedFormat Type:=xlTypePDF, _
Filename:=Filen, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=False
End Sub
更新:
现在把它们放在一起:
Sub InvoiceToRecords()
Dim ws As Worksheet: Set ws = Worksheets("Invoice")
Dim wsData As Worksheet: Set wsData = Worksheets("Invoice Data")
'declare and set the worksheets, amend as required
Dim i As Long, dataRows As Long
'TRANSFER data to Invoice Data
dataRows = ws.Range("Invoice").Columns(1).SpecialCells(xlCellTypeConstants, 23).Count
'count the number of Invoice lines with data (non-empty)
ws.Range("Invoice").Copy wsData.Range("D" & Rows.Count).End(xlUp).Offset(1, 0)
'copy invoice lines to Invoice Data
For i = 1 To dataRows 'loop from 1 to however many lines your named range "Invoice" has
ws.Range("Customer").Copy
wsData.Range("A" & Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
ws.Range("Invoice Number").Copy
wsData.Range("B" & Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
ws.Range("Invoice Date").Copy
wsData.Range("C" & Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
Next i
'SAVE Invoice as PDF
FilenameValue = ws.Range("Customer") & "_Invoice" & ws.Range("Invoice Number")
FilenameValue = Replace(FilenameValue, " ", "") 'remove spaces
FilenameValue = Replace(FilenameValue, ".", "_") 'replace dots with underscore
Filen = "C:\Users\Lorenz\Desktop\" & FilenameValue & ".pdf"
'amend filename & path to save above
ws.ExportAsFixedFormat Type:=xlTypePDF, _
Filename:=Filen, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=False
'CLEAR ranges ready for next invoice
ws.Range("Invoice").ClearContents
ws.Range("Customer").ClearContents
ws.Range("Invoice Number").ClearContents
ws.Range("Invoice Date").ClearContents
End Sub
我正在寻找 VBA 代码,它可以通过按钮将发票数据复制到单独的工作sheet(例如 "Save Invoice") ,一个将文件另存为 .pdf 的按钮,最后是一个带有宏的按钮,用于重置发票 sheet ("Reset invoice")。我尝试了一些 VBA 解决方案,但我似乎无法找到可行的解决方案。
- 每个新保存的输入都应保存在上一个保存的输出下方的空行中
- 应复制发票中的所有 inputs/rows如果它们包含数据
- 带有"Save as pdf" 的按钮
- 一个按钮到"clear the sheet"
代码:
Sub InvoiceToRecords()
LastRecordsRow = Worksheets("Invoice Data").UsedRange.Rows.Count
'determines the # of rows used
NewRecordsRow = LastRecordsRow + 1
'Row for pasting latest invoice will be 1 row below the end of the last invoice
Sheets("Invoice").Activate
Range("Invoice").Copy Sheets("Invoice Data").Range("D" & Rows.Count).End(xlUp).Offset(1, 0)
Range("Customer").Copy Sheets("Invoice Data").Range("A" & Rows.Count).End(xlUp).Offset(1, 0)
Range("Invoice Number").Copy Sheets("Invoice Data").Range("B" & Rows.Count).End(xlUp).Offset(1, 0)
Range("Invoice Date").Copy Sheets("Invoice Data").Range("C" & Rows.Count).End(xlUp).Offset(1, 0)
End Sub
Highlighted code
Error message Syntax error
要复制次要数据的次数与发票明细行数一样多,您可以按以下方式进行,只需将您的代码替换为此代码即可:
Sub InvoiceToRecords()
Dim ws As Worksheet: Set ws = Worksheets("Invoice")
Dim wsData As Worksheet: Set wsData = Worksheets("Invoice Data")
'declare and set the worksheets, amend as required
Dim i As Long, dataRows As Long
dataRows = ws.Range("Invoice").Columns(1).SpecialCells(xlCellTypeConstants, 23).Count
'count the number of Invoice lines with data (non-empty)
ws.Range("Invoice").Copy wsData.Range("D" & Rows.Count).End(xlUp).Offset(1, 0)
'copy invoice lines to Invoice Data
For i = 1 To dataRows 'loop from 1 to however many lines your named range "Invoice" has
ws.Range("Customer").Copy wsData.Range("A" & Rows.Count).End(xlUp).Offset(1, 0)
ws.Range("Invoice Number").Copy wsData.Range("B" & Rows.Count).End(xlUp).Offset(1, 0)
ws.Range("Invoice Date").Copy wsData.Range("C" & Rows.Count).End(xlUp).Offset(1, 0)
Next i
End Sub
要将您的工作表保存为 PDF,请执行以下操作,我会使用某种变量来生成 PDF 文件名,这样您就不会一直覆盖同一个文件,可能是公司和发票编号的组合,或者即使是时间戳也可以:
Sub foo()
Dim ws As Worksheet: Set ws = Sheets("Sheet1")
Filen = "C:\Users\Lorenz\Desktop\NewPdf.pdf"
'amend filename & path to save above
ws.ExportAsFixedFormat Type:=xlTypePDF, _
Filename:=Filen, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=False
End Sub
更新:
现在把它们放在一起:
Sub InvoiceToRecords()
Dim ws As Worksheet: Set ws = Worksheets("Invoice")
Dim wsData As Worksheet: Set wsData = Worksheets("Invoice Data")
'declare and set the worksheets, amend as required
Dim i As Long, dataRows As Long
'TRANSFER data to Invoice Data
dataRows = ws.Range("Invoice").Columns(1).SpecialCells(xlCellTypeConstants, 23).Count
'count the number of Invoice lines with data (non-empty)
ws.Range("Invoice").Copy wsData.Range("D" & Rows.Count).End(xlUp).Offset(1, 0)
'copy invoice lines to Invoice Data
For i = 1 To dataRows 'loop from 1 to however many lines your named range "Invoice" has
ws.Range("Customer").Copy
wsData.Range("A" & Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
ws.Range("Invoice Number").Copy
wsData.Range("B" & Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
ws.Range("Invoice Date").Copy
wsData.Range("C" & Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
Next i
'SAVE Invoice as PDF
FilenameValue = ws.Range("Customer") & "_Invoice" & ws.Range("Invoice Number")
FilenameValue = Replace(FilenameValue, " ", "") 'remove spaces
FilenameValue = Replace(FilenameValue, ".", "_") 'replace dots with underscore
Filen = "C:\Users\Lorenz\Desktop\" & FilenameValue & ".pdf"
'amend filename & path to save above
ws.ExportAsFixedFormat Type:=xlTypePDF, _
Filename:=Filen, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=False
'CLEAR ranges ready for next invoice
ws.Range("Invoice").ClearContents
ws.Range("Customer").ClearContents
ws.Range("Invoice Number").ClearContents
ws.Range("Invoice Date").ClearContents
End Sub