Macro for creating invoices

Joined
Sep 27, 2017
Messages
3
Hello,

I have a question- I have a table of client data that I need to create unique invoices for. Each row would be one. I have a tab that has the invoice template on it, but have yet to figure out how to get it to create a new template tab with the next rows information. Can someone please help?

Thank you,

Shira
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
I may be misunderstanding your request, but I'll throw out an idea regardless -

You could create an 'Invoice Layout' sheet, which you'll hide from other users and have a macro that'll populate this sheet, which'll request the name of the person you wish to bill. I'll put together a macro for you if this approach is what you're seeking, but figured I'd start here. Once the name is inserted you can have a sheet renamed to the invoice # you have on your 'Client Data' sheet, which all the other data will auto-populate depending on the data within your 'Client Data' sheet using vlookups.
Book1
ABCDE
1Shira Princess Of Power Inc.
2123 MrExcel Court
3Google Chrome, XLS, 00000
4
5Bill To:INVOICE #00000121
6Jill DoeINVOICE DATE07/04/22
7456 MrExcel AveDUE DATE07/19/22
8Google, CA, 123456
9INVOICE TOTAL$ -
10
11QTYDESCRIPTIONUNIT PRICEAMOUNT
12
13
14
15
16
17
18
19
20
21
22
23
24Subtotal0
25Sales Tax7.75%
26
27
28TERMS & CONDITIONS
29
30Payment is due within 15 days
31
32Please make checks payable to:Shira Princess Of Power Inc.
33
Invoice Layout
Cell Formulas
RangeFormula
E5E5=VLOOKUP('Invoice Layout'!$A$6,'Client Data'!A:B,2,FALSE)
E6E6=VLOOKUP('Invoice Layout'!$A$6,'Client Data'!A:G,7,FALSE)
E7E7=E6+15
A7A7=VLOOKUP('Invoice Layout'!A6,'Client Data'!A:C,3,FALSE)
A8A8=CONCATENATE(VLOOKUP($A$6,'Client Data'!A:F,4,0),", ",VLOOKUP($A$6,'Client Data'!A:F,5,0),", ",VLOOKUP($A$6,'Client Data'!A:F,6,0))
E9E9=SUM(E24*E25)+E24
E24E24=SUM(E12:E23)
E25E25=VLOOKUP($A$6,'Client Data'!A:I,9,FALSE)
D32D32=A1


The 'Client Data' sheet could have a vast amount of data, which could be used in your Invoice Layout.
Example:
Book1
ABCDEFGHI
1Client NameInvoiceAddressCityStateZIPDate of ServiceCountyTax Rate
2John Doe00004544123 MrExcel AveGoogleCA12345607/01/22Excel7.75%
3Jill Doe00000121456 MrExcel AveGoogleCA12345607/04/22Excel7.75%
4Ottsel00042069798 MrExcel AveGoogleCA12345607/06/22Excel7.75%
Client Data
Cell Formulas
RangeFormula
I2:I4I2=VLOOKUP(H2,'County Tax Data'!A:B,2,FALSE)

For added fun, you could even create a 'County Tax Data' sheet with all the surrounding areas you provide service(s) to, so your sales tax is auto-populated. Of course, this'll require you to look at those rates within and around your area, but here's a fun example:
Book1
AB
1CountyTax Rate
2Excel7.75%
3OverStack8.00%
4Reddit7.25%
County Tax Data

With a VBA macro, some formatting and time you could have quite the automated system. Of course, this approach uses a vast amount of sheets, but it's an idea. You could always create another macro to clean up these sheets after a certain grace period.
 
Upvote 0
Solution
Found an error. Need to correct it.
 
Last edited:
Upvote 0
Apologies about that. Here you go:

This'll only do 1 line per invoice, as that appears to be the pattern with this example.

You data requires an XLookup mostly compared to my Vlookup examples. You do not have your version of excel listed, but I'm gonna assume here 'bad on my part, I know', and hope you have a version of excel that can handle this lookup.

I formatted your layout as such:
220705_Initial_QuickBooks_Sync_Ottsel_Adjustment.xlsm
CDEFGHIJK
26BILL TONVOICE #202205-007
27Hitachi Energy USA, Inc.DATE5/3/2022
28902 Main Campus DriveDUE DATE7/2/2022
29Raleigh,NC 27607TERMSNet 30
30
31
32
33DATEDESCRIPTIONUNITUSDTOTAL
345/3/2022Studio C (B) Production Support - 2022 May1.0011,000.0011,000.00
35
36
37
3811,000.00
39
40Remit Funds to:BALANCE DUE$11,000.00
41Webster Bank
42Account: Athena Global Advisors, Inc. Routing No: 211370231
43Account No: 9320392767 (USD Account)
44
45
46Payments by Check payable to: Athena Global Advisors, Inc.
4730 S.15th Street
48Suite 600
49Philadelphia, PA 19102 USA
202205-007-5
Cell Formulas
RangeFormula
F26F26=XLOOKUP(Customer_name,'Invoices and Bills Template'!X:X,'Invoices and Bills Template'!E:E,"",0)
F27F27=XLOOKUP(Customer_name,'Invoices and Bills Template'!X:X,'Invoices and Bills Template'!C:C,"",0)
F28F28=XLOOKUP(Customer_name,'Invoices and Bills Template'!X:X,'Invoices and Bills Template'!D:D,"",0)
C28C28=VLOOKUP(Customer_name,'Invoices and Bills Template'!X:Z,3,FALSE)
C29C29=CONCATENATE(VLOOKUP(Customer_name,'Invoices and Bills Template'!X:AD,5,0),",",VLOOKUP(Customer_name,'Invoices and Bills Template'!X:AD,6,0)," ",VLOOKUP(Customer_name,'Invoices and Bills Template'!X:AD,7,0))
C34C34=Date
D34D34=XLOOKUP(Customer_name,'Invoices and Bills Template'!X:X,'Invoices and Bills Template'!K:K,"",0)
I34I34=XLOOKUP(Customer_name,'Invoices and Bills Template'!X:X,'Invoices and Bills Template'!L:L,"",0)
J34J34=XLOOKUP(Customer_name,'Invoices and Bills Template'!X:X,'Invoices and Bills Template'!N:N,"",0)
K34K34=unit*Unit_Price
K38K38=SUM(K34:K37)
J40J40=K38
Named Ranges
NameRefers ToCells
'202205-007-5'!Customer_name='202205-007-5'!$C$27I34:J34, D34, F26:F28, C28:C29
'202205-007-5'!Date='202205-007-5'!$F$27C34
'202205-007-5'!Total='202205-007-5'!$K$34K38
'202205-007-5'!unit='202205-007-5'!$I$34K34
'202205-007-5'!Unit_Price='202205-007-5'!$J$34K34
Cells with Data Validation
CellAllowCriteria
I34Any value

Use this code. I avoided a msgbox for you to type each and every customer, as I found this to be tedious with what you have, so a popup box will have you select the cell of the customer you wish to create an invoice for. I left out hiding the 'Template', but the code is there if you wish to re-enable this feature for your team/employees. I needed a PDF macro a while back, so I modified it slightly for you. Should work wonders.
Also, I couldn't think of a way to avoid duplicate sheet names with the same invoice# as the name, so I added a "-" & sheetcount, so it'll always be different. This method may not work for you, which is that's the case I apologies, as my VBA knowledge is still very novice.
VBA Code:
Sub CreateInvoice()

    Dim rng As Range
    Set rng = Application.InputBox("Click which customer to generate an Invoice for:", "Athena Global Advisors, Inc", Type:=8)
    Dim answer As Integer
   
    Application.DisplayAlerts = False
        'insert an apostrophe in front of the code below or delete it IF you do not wish to hide it
        'Sheets("Template").Visible = True
       
    If rng <> "" Then
        Sheets("Template").Copy After:=Sheets("Invoices and Bills Template")
        On Error Resume Next
        ActiveSheet.Range("C27") = rng
        ActiveSheet.Name = Range("F26") & "-" & ThisWorkbook.Sheets.Count
        ActiveSheet.Cells(27, 3).Select
    End If
   
    MsgBox "An Invoice for " & rng & " was generated.", vbOKOnly, "Athena Global Advisors, Inc"
        'perform the same action here if you do not need to hide this sheet
        'Sheets("Template").Visible = False
    Application.DisplayAlerts = True
   
    answer = MsgBox("Do you wish to save Invoice# " & Range("f26") & "?", _
        vbYesNo + vbQuestion, "Athena Global Advisors, Inc")
    If answer = vbYes Then
        PDFActiveSheet
    If answer = vbNo Then
        Exit Sub
    End If
    End If
   
End Sub

Private Sub PDFActiveSheet()

    Dim wsA As Worksheet
    Dim wbA As Workbook
    Dim strTime As String
    Dim strName As String
    Dim strPath As String
    Dim strFile As String
    Dim strPathFile As String
    Dim myFile As Variant
    On Error GoTo errHandler

    Set wbA = ActiveWorkbook
    Set wsA = ActiveSheet
    strTime = Format(Now(), ActiveSheet.Range("F26") & "_" & ActiveSheet.Range("C27"))

    'get active workbook folder, if saved
    strPath = wbA.Path
        If strPath = "" Then
        strPath = Application.DefaultFilePath
        End If
    strPath = strPath & "\"

    'replace spaces and periods in sheet name
    strName = Replace(wsA.Name, " ", "")
    strName = Replace(strName, ".", "_")

    'create default name for savng file
    strFile = ActiveSheet.Range("F26") & "_" & ActiveSheet.Range("C27") & ".pdf"
    strPathFile = strPath & strFile

    'use can enter name and
    ' select folder for file
    myFile = Application.GetSaveAsFilename _
        (InitialFileName:=strPathFile, _
            FileFilter:="PDF Files (*.pdf), *.pdf", _
            Title:="Select Folder and FileName to save")

    'export to PDF if a folder was selected
    If myFile <> "False" Then
        wsA.ExportAsFixedFormat _
            Type:=xlTypePDF, _
            Filename:=myFile, _
            Quality:=xlQualityStandard, _
            IncludeDocProperties:=True, _
            IgnorePrintAreas:=False, _
            OpenAfterPublish:=False
        'confirmation message with file info
        MsgBox "PDF file has been created: " & vbCrLf & myFile, , "Athena Global Advisors, Inc"
    End If

exitHandler:
        Exit Sub
errHandler:
        MsgBox "Could not create PDF file"
        Resume exitHandler
End Sub

Also, if trying to input all this yourself seems time consuming you can always use this link to access my version via Drop Box.
220705_Initial_QuickBooks_Sync_Ottsel_Adjustment.xlsm

NOTE: nothing may appear in a preview, but click "Download" and you should have access to the file.
 
Upvote 0
Yes, that would be amazing, thank you! I am attaching my file here so you can get a detailed look. I need each line to populate a separate template and then I need to save each template as a PDF. A macro would be very helpful here because it is a very manual process. I uploaded it to google docs. 220705_Initial_QuickBooks_Sync copy.xlsm
Hope the above works. Again, sorry for not fully proof-checking everything the first time around. Can't believe I overlooked inserting the customer selection.
 
Upvote 0

Forum statistics

Threads
1,214,830
Messages
6,121,831
Members
449,051
Latest member
excelquestion515

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top