October 5, 2015

Transfer Data from Invoice to Worksheet with VBA

Let’s watch the Excel training video before we start understanding the VBA macro code to transfer the data.

Transfer Data from Invoice to Worksheet

How to transfer data from invoice to an Excel worksheet automatically with VBA. Earlier we had learnt how to transfer data from a worksheet to an invoice template. Often we need to create a report from an invoice or purchase order and wish to automate the process completely. 

The macro VBA code does the following:

  • It automatically increases the invoice number by 1 when we open the workbook
  • The invoice data is automatically displayed
  • The payment due date is automatically calculated
  • Important data like invoice number, invoice data, item description, item value and invoice total is transferred to another sheet to create a report. The item value and invoice total is calculated automatically
  • If the user enters no data like quantity or details of the item then the macro exits without saving the data or file
  • At the end of the invoice entry the file is saved in two formats automatically – .xlsx and .pdf

The complete macro code:

Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim ws1 As Worksheet
Set ws1 = Worksheets(“Invoice”)
Dim ws2 As Worksheet
Set ws2 = Worksheets(“Sheet1”)
Dim count As Long
count = 0

Application.DisplayAlerts = False

For i = 15 To 29
If ws1.Cells(i, 2) = “” Then
count = count + 1
End If
Next i
If count = 15 Then
ActiveWorkbook.Close savechanges = False
Exit Sub
End If

For i = 15 To 29
erow = ws2.Cells(Rows.count, 1).End(xlUp).Offset(1, 0).Row
If ws1.Cells(i, 2) <> “” Then
ws2.Cells(erow, 1) = ws1.Range(“InvoiceNumberDisplay”)
ws2.Cells(erow, 2) = ws1.Cells(3, 2)
ws2.Cells(erow, 2) = Format(ws2.Cells(erow, 2), “mm-dd-yy”)
ws2.Cells(erow, 3) = ws1.Cells(i, 3)
ws2.Cells(erow, 4) = ws1.Cells(i, 5)
End If
Next i
ws2.Cells(erow – 1, 5) = ws1.Range(“InvoiceTotal”)

Dim Path As String
Path = “c:\myinvoices\”
ActiveWorkbook.ActiveSheet.SaveAs Filename:=Path & Range(“B2″) & ” – ” & Range(“C2”) & “.xlsx”, FileFormat:=51
ActiveWorkbook.ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=Path & Range(“B2″) & ” – ” & Range(“C2”) & “.pdf”, OpenAfterPublish:=False

Application.DisplayAlerts = True
End Sub

Private Sub Workbook_Open()
Range(“InvoiceNumberDisplay”) = Range(“InvoiceNumberDisplay”) + 1
Range(“C3”) = Date
Range(“C4”) = DateAdd(“m”, 1, Range(“B3”))
Range(“B15:D29”) = “”
End Sub

10 thoughts on “Transfer Data from Invoice to Worksheet with VBA

  1. Sir what if i created invoice from userform and have to transfer data for report with multiple items. I tried with one item its done. but for multiple item in the list. can we do it? do we have youtube video for this? or can be done?

  2. How did you user formula =iferror([@[UNIT PRICE]*[@QUANTITY],””), as it either saying me invalid or data cant be user start with latter or _, I create name for range cell with UNIT_PRICE and then QTY… but still gives the same error. please advise


  3. Sir,If copy of your VBA code & paste it in my excel sheet,it display error. So send me how I enter VBA code correctly. Thank you

  4. Hai Dinest Kumar

    I copy the template from invoice excel and copyexactly you code to VBA but failed to run . What should I do .

  5. Hi Sir,
    I’d watched your video of copy invoice data.
    I need one help on same topic.
    I have my own invoice format but I can’t apply your steps for that.
    So can you pls help out on this

  6. Good Day! I would like to ask how do you transfer data from invoice userform into records with multiple item? Example, in userform:
    Invoice No. 1 Date 4-20-2019
    Items Amount
    Dress $500
    Shirt $200
    Total: $700

    in records:
    Invoice No. Date Items Amount Total
    1 4-20-2019 Dress $500
    1 4-20-2019 Shirt $200 $700

    what will be the VBA code of that?
    Thanks a lot!

  7. 1.When the Excel file is closed, we see the following:
    When open the save Invoice xlsx, it is revealed that it saved the whole file whereas we need to save only the Invoice but PDF is OK.
    2.When we open the main file, the Invoice No. is increased by 1 but when we close the file without any data the Invoice No. should not be increased.

    Please solve the matter.

    Thanks and regards

Comments are closed.