VBA to generate invoices from data base

joyrichter

New Member
Joined
Jun 17, 2023
Messages
31
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
Hi
Im getting stuck on a code, I have gotten the code to generate an invoice from a database worksheet, I want to copy the code for the complete sheet, to generate different invoices and to rename the generated invoices.
I will appreciate any tips and help.
Thank you
Joy
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Hi @joyrichter

I coded the Macro from scratch, because your code had some errors/typos and added some "error" checking to it:
  • if the first cell (A2) contains no data, the macro will stop with a message
  • if a processed row contains a blank cell, the macro will stop with a message
  • if all rows have consistent data, the macro will start with a message
Here's the final result:

The table containing data:
MrExcel_joyrichter_invoice.xlsm
ABCDEFGHIJKLMNOP
1NameAdressCityProvincePostal CodeCountryVAT numberInvoice dateInvoice numberProduct codeDescriptionRateQtyDelivery distanceProduct Manager:Contact Number:
2Bela Bela Supermarket12 Church streetBela BelaLimpopo1001South Africa11223368112023/02/04ONC100CABanBanana89.131156Ms Tau084 321 1221
3Magalies Tea Garden3 Baber streetHartbeespoortNorth West6000South Africa11223368992023/02/04ONC101CATirTiramisu175.7679Ms Tau084 321 1221
4Klapoog's Castle89 Seahorse avenueLangebaanWestern Cape3001South Africa11223368662023/02/04ONC102CABanBanana89.1341471Ms Tau084 321 1221
5Klapoog's Castle89 Seahorse avenueLangebaanWestern Cape3001South Africa11223368662023/02/04ONC103CAPanPancakes78.2511471Ms Tau084 321 1221
6Mermaid Bakery123 Strand streetCape TownWestern Cape3000South Africa11223368552023/02/06ONC104CACrmCaramel16381420Ms Tau084 321 1221
7Jawsmunger5 Beach roadGqeberhaEastern Cape2000South Africa11223368442023/02/06ONC105CAFruFruit173.981068Ms Tau084 321 1221
8Mermaid Bakery123 Strand streetCape TownWestern Cape3000South Africa11223368552023/02/07ONC106CACrmCaramel16361420Ms Tau084 321 1221
9Waterfall Eatery34 Mossie avenueMbombelaMpumulanga5000South Africa11223368882023/02/07ONC107CACrtCarrot1639329Ms Tau084 321 1221
10Guppies56 Beach roadDurbanKwazulu Natal7000South Africa11223369102023/02/08ONC108CAChoChocolate1631573Ms Tau084 321 1221
11Roos Hotel31 Daisy avenueBloemfonteinFree State4000South Africa11223368772023/02/09ONC109CAPanPancakes78.259421Ms Tau084 321 1221
12Capricorn Inn51 Unicorn drivePolokwaneLimpopo1002South Africa11223368222023/02/09ONC110CAStrStrawberry1636309Ms Tau084 321 1221
13Klapoog's Castle89 Seahorse avenueLangebaanWestern Cape3001South Africa11223368662023/02/10ONC111CATirTiramisu175.791471Ms Tau084 321 1221
14Magalies Tea Garden3 Baber streetHartbeespoortNorth West6000South Africa11223368992023/02/11ONC112CABanBanana89.131079Ms Tau084 321 1221
15Guppies56 Beach roadDurbanKwazulu Natal7000South Africa11223369102023/02/13ONC113CAChoChocolate1633573Ms Tau084 321 1221
16Mermaid Bakery123 Strand streetCape TownWestern Cape3000South Africa11223368552023/02/14ONC114CARedRed velvet184.131420Ms Tau084 321 1221
17Klapoog's Castle89 Seahorse avenueLangebaanWestern Cape3001South Africa11223368662023/02/14ONC115CAOraOrange16361471Ms Tau084 321 1221
18Klapoog's Castle89 Seahorse avenueLangebaanWestern Cape3001South Africa11223368662023/02/16ONC116CAPanPancakes78.2571471Ms Tau084 321 1221
19Guppies56 Beach roadDurbanKwazulu Natal7000South Africa11223369102023/02/17ONC117CAStrStrawberry1636573Ms Tau084 321 1221
20UNISA student caféPreller streetPretoriaGauteng0001South Africa11223368332023/02/18ONC118CABanBanana89.13758Ms Tau084 321 1221
21Waterfall Eatery34 Mossie avenueMbombelaMpumulanga5000South Africa11223368882023/02/18ONC119CAChoChocolate1636329Ms Tau084 321 1221
22Roos Hotel31 Daisy avenueBloemfonteinFree State4000South Africa11223368772023/02/19ONC120CAOraOrange1637421Ms Tau084 321 1221
23Mermaid Bakery123 Strand streetCape TownWestern Cape3000South Africa11223368552023/02/20ONC121CATirTiramisu175.791420Ms Tau084 321 1221
24Magalies Tea Garden3 Baber streetHartbeespoortNorth West6000South Africa11223368992023/02/21ONC122CATirTiramisu175.7979Ms Tau084 321 1221
25UNISA student caféPreller streetPretoriaGauteng0001South Africa11223368332023/02/21ONC123CACrmCaramel163258Ms Tau084 321 1221
26Lulu's Bakery321 Justice streetPretoriaGauteng0001South Africa11223367892023/02/22ONC124CAChoChocolate163658Ms Tau084 321 1221
27Jawsmunger5 Beach roadGqeberhaEastern Cape2000South Africa11223368442023/02/25ONC125CACrtCarrot16341068Ms Tau084 321 1221
28Capricorn Inn51 Unicorn drivePolokwaneLimpopo1002South Africa11223368222023/02/26ONC126CAChoChocolate1638309Ms Tau084 321 1221
29UNISA student caféPreller streetPretoriaGauteng0001South Africa11223368332023/02/27ONC127CACrtCarrot163558Ms Tau084 321 1221
30Mermaid Bakery123 Strand streetCape TownWestern Cape3000South Africa11223368552023/02/27ONC128CARedRed velvet184.161420Ms Tau084 321 1221
31Lulu's Bakery321 Justice streetPretoriaGauteng0001South Africa11223367892023/02/28ONC129CAStrStrawberry163158Ms Tau084 321 1221
32Sophia's Tea Garden45 Tau avenueJohannesburgGauteng0002South Africa11223368002023/02/28ONC130CALemLemon Meringue173.9719Ms Tau084 321 1221
Recorded_Sales


the template table:
MrExcel_joyrichter_invoice.xlsm
ABCDEFGHIJKL
11
22Ikhishi Likagogo (Pty) LtdIsando
33086 121 4311Gauteng
4434
55South Africa
6VAT number: 99123456781
7
8Customer Name:Invoice date:Tax invoice number:Amount due (ZAR)
90
10
11Due dute:
12
13
14
15
16
17
18CodeDescriptionRateQty/DistTotal
19Product Rate / Quantity0
20Delivery Rate / Distance0
21Subtotal0
22VAT (15%)0
23Total0
24
25For any queries please contact the product manager. Details are provided below:
26Product Manager:
27Contact Number:
28
29Delivery:
30Delivery is charged at R2.07 (VAT inclusive) for delivery in South Africa.
Invoice_Template
Cell Formulas
RangeFormula
J9J9=L23
L19:L20L19=I19*J19
L21,L23L21=L19+L20
L22L22=L21*0.15



the VBA code:
VBA Code:
'Invoice Generator
Sub To_Generate_Invoices()

'Constant declaration
Const deliveryRate = 1.8

'Variable declaration:
Dim RecordedSalesWS As Worksheet
Dim InvoiceTemplateWS As Worksheet
Dim currentWorkingWS As Worksheet
Dim customerName As String
Dim customerAddress As String
Dim customerCity As String
Dim customerProvince As String
Dim customerPostalCode As Single
Dim customerCountry As String
Dim customerVATnumber As String
Dim invoiceDate As Date
Dim TaxinvoiceNumber As String
Dim productCode As String
Dim productDescription As String
Dim productRate As Single
Dim productQuantity As Integer
Dim deliveryDistance As Integer
Dim productionManager As String
Dim contactNumber As String
Dim a As Long
Dim rng_dest As Range
Dim rng As Range
Dim i As Long
Dim j As Long
Dim n As Long
Dim Counter As String
Dim Val As Integer
Dim numberOfDatasets As Long
Dim numberOfInconsistentRows As Long
Dim dataInconsistency As Boolean

'Set workbook variables to worksheets
Set RecordedSalesWS = ThisWorkbook.Sheets("Recorded_Sales")
Set InvoiceTemplateWS = ThisWorkbook.Sheets("Invoice_Template")

Set rng_dest = ThisWorkbook.Sheets("Recorded_Sales").Range("A:P")
numberOfDatasets = 0
numberOfInconsistentRows = 0
dataInconsistency = False

'search for last row with data and check consistency (blank cells)
i = 2
numberOfInconsistentRows = 0
Do While rng_dest.Cells(i, 1) <> ""
   'loop through columns A to P (column 1 - 16), look for and count blank cells
   For j = 1 To 16
      If rng_dest.Cells(i, j).Value = "" Then numberOfInconsistentRows = numberOfInconsistentRows + 1
   Next j
   If numberOfInconsistentRows > 0 Then
      dataInconsistency = True
   End If
   numberOfInconsistentRows = 0
   i = i + 1
   numberOfDatasets = numberOfDatasets + 1
Loop
If numberOfDatasets = 0 Then
   MsgBox "No data detected in first row. Macro will stop."
   Exit Sub
End If
If dataInconsistency = True Then
   MsgBox "Inconsistent data. Please check for blank cells. Macro will stop"
   Exit Sub
End If
MsgBox numberOfDatasets & " Datasets found." & vbCrLf & "Generating invoices."

Application.ScreenUpdating = False
'write data to invoice template
For i = 1 To numberOfDatasets
   customerName = RecordedSalesWS.Range("A" & i + 1).Value
   customerAddress = RecordedSalesWS.Range("B" & i + 1).Value
   customerCity = RecordedSalesWS.Range("C" & i + 1).Value
   customerProvince = RecordedSalesWS.Range("D" & i + 1).Value
   customerPostalCode = RecordedSalesWS.Range("E" & i + 1).Value
   customerCountry = RecordedSalesWS.Range("F" & i + 1).Value
   customerVATnumber = RecordedSalesWS.Range("G" & i + 1).Value
   invoiceDate = RecordedSalesWS.Range("H" & i + 1).Value
   TaxinvoiceNumber = RecordedSalesWS.Range("I" & i + 1).Value
   productCode = RecordedSalesWS.Range("J" & i + 1).Value
   productDescription = RecordedSalesWS.Range("K" & i + 1).Value
   productRate = RecordedSalesWS.Range("L" & i + 1).Value
   productQuantity = RecordedSalesWS.Range("M" & i + 1).Value
   deliveryDistance = RecordedSalesWS.Range("N" & i + 1).Value
   productionManager = RecordedSalesWS.Range("O" & i + 1).Value
   contactNumber = RecordedSalesWS.Range("P" & i + 1).Value

   InvoiceTemplateWS.Copy After:=Sheets(Sheets.Count)
   On Error Resume Next
   ActiveSheet.Name = TaxinvoiceNumber
   On Error GoTo 0
  
   Set currentWorkingWS = ThisWorkbook.Sheets(TaxinvoiceNumber)
   With currentWorkingWS
      .Range("A9").Value = customerName
      .Range("A10").Value = customerAddress
      .Range("A11").Value = customerCity
      .Range("A12").Value = customerProvince
      .Range("A13").Value = customerPostalCode
      .Range("A13").NumberFormat = "0000"
      .Range("A14").Value = customerCountry
      .Range("A15").Value = "VAT Number:" & customerVATnumber
      .Range("C9").Value = invoiceDate
      .Range("C12") = invoiceDate + 7
      .Range("E9").Value = TaxinvoiceNumber
      .Range("A19").Value = productCode
      .Range("B19").Value = productDescription
      .Range("I19").Value = productRate
      .Range("J19").Value = productQuantity
      .Range("I20").Value = deliveryRate
      .Range("J20").Value = deliveryDistance
      .Range("A20").Value = "Delivery:"
      .Range("B20").Value = deliveryDistance
      .Range("C20").Value = "km - Isando to " & customerCity
      .Range("C26").Value = productionManager
      .Range("C27").Value = contactNumber
   End With
Next i

Application.ScreenUpdating = True
End Sub
Thank you very very much,
I will study and compater the code and try to understand where my errors were..
 
Upvote 1
Activity4_8 (3) - Test.xlsm
ABCDEFGHIJKLMNOP
1NameAdressCityProvincePostal CodeCountryVAT numberInvoice dateInvoice numberProduct codeDescriptionRateQtyDelivery distanceProduct Manager:Contact Number:
2Bela Bela Supermarket12 Church streetBela BelaLimpopo1001South Africa11223368112023/02/04ONC100CABanBanana89.131156Ms Tau084 321 1221
3Magalies Tea Garden3 Baber streetHartbeespoortNorth West6000South Africa11223368992023/02/04ONC101CATirTiramisu175.65679Ms Tau084 321 1221
4Klapoog's Castle89 Seahorse avenueLangebaanWestern Cape3001South Africa11223368662023/02/04ONC102CABanBanana89.1341471Ms Tau084 321 1221
5Klapoog's Castle89 Seahorse avenueLangebaanWestern Cape3001South Africa11223368662023/02/04ONC103CAPanPancakes78.2511471Ms Tau084 321 1221
6Mermaid Bakery123 Strand streetCape TownWestern Cape3000South Africa11223368552023/02/06ONC104CACrmCaramel163.0481420Ms Tau084 321 1221
7Jawsmunger5 Beach roadGqeberhaEastern Cape2000South Africa11223368442023/02/06ONC105CAFruFruit173.8981068Ms Tau084 321 1221
8Mermaid Bakery123 Strand streetCape TownWestern Cape3000South Africa11223368552023/02/07ONC106CACrmCaramel163.0461420Ms Tau084 321 1221
9Waterfall Eatery34 Mossie avenueMbombelaMpumulanga5000South Africa11223368882023/02/07ONC107CACrtCarrot163.049329Ms Tau084 321 1221
10Guppies56 Beach roadDurbanKwazulu Natal7000South Africa11223369102023/02/08ONC108CAChoChocolate163.041573Ms Tau084 321 1221
11Roos Hotel31 Daisy avenueBloemfonteinFree State4000South Africa11223368772023/02/09ONC109CAPanPancakes78.259421Ms Tau084 321 1221
12Capricorn Inn51 Unicorn drivePolokwaneLimpopo1002South Africa11223368222023/02/09ONC110CAStrStrawberry163.046309Ms Tau084 321 1221
13Klapoog's Castle89 Seahorse avenueLangebaanWestern Cape3001South Africa11223368662023/02/10ONC111CATirTiramisu175.6591471Ms Tau084 321 1221
14Magalies Tea Garden3 Baber streetHartbeespoortNorth West6000South Africa11223368992023/02/11ONC112CABanBanana89.131079Ms Tau084 321 1221
15Guppies56 Beach roadDurbanKwazulu Natal7000South Africa11223369102023/02/13ONC113CAChoChocolate163.043573Ms Tau084 321 1221
16Mermaid Bakery123 Strand streetCape TownWestern Cape3000South Africa11223368552023/02/14ONC114CARedRed velvet184.1331420Ms Tau084 321 1221
17Klapoog's Castle89 Seahorse avenueLangebaanWestern Cape3001South Africa11223368662023/02/14ONC115CAOraOrange163.0461471Ms Tau084 321 1221
18Klapoog's Castle89 Seahorse avenueLangebaanWestern Cape3001South Africa11223368662023/02/16ONC116CAPanPancakes78.2571471Ms Tau084 321 1221
19Guppies56 Beach roadDurbanKwazulu Natal7000South Africa11223369102023/02/17ONC117CAStrStrawberry163.046573Ms Tau084 321 1221
20UNISA student caféPreller streetPretoriaGauteng0001South Africa11223368332023/02/18ONC118CABanBanana89.13758Ms Tau084 321 1221
21Waterfall Eatery34 Mossie avenueMbombelaMpumulanga5000South Africa11223368882023/02/18ONC119CAChoChocolate163.046329Ms Tau084 321 1221
22Roos Hotel31 Daisy avenueBloemfonteinFree State4000South Africa11223368772023/02/19ONC120CAOraOrange163.047421Ms Tau084 321 1221
23Mermaid Bakery123 Strand streetCape TownWestern Cape3000South Africa11223368552023/02/20ONC121CATirTiramisu175.6591420Ms Tau084 321 1221
24Magalies Tea Garden3 Baber streetHartbeespoortNorth West6000South Africa11223368992023/02/21ONC122CATirTiramisu175.65979Ms Tau084 321 1221
25UNISA student caféPreller streetPretoriaGauteng0001South Africa11223368332023/02/21ONC123CACrmCaramel163.04258Ms Tau084 321 1221
26Lulu's Bakery321 Justice streetPretoriaGauteng0001South Africa11223367892023/02/22ONC124CAChoChocolate163.04658Ms Tau084 321 1221
27Jawsmunger5 Beach roadGqeberhaEastern Cape2000South Africa11223368442023/02/25ONC125CACrtCarrot163.0441068Ms Tau084 321 1221
28Capricorn Inn51 Unicorn drivePolokwaneLimpopo1002South Africa11223368222023/02/26ONC126CAChoChocolate163.048309Ms Tau084 321 1221
29UNISA student caféPreller streetPretoriaGauteng0001South Africa11223368332023/02/27ONC127CACrtCarrot163.04558Ms Tau084 321 1221
30Mermaid Bakery123 Strand streetCape TownWestern Cape3000South Africa11223368552023/02/27ONC128CARedRed velvet184.1361420Ms Tau084 321 1221
31Lulu's Bakery321 Justice streetPretoriaGauteng0001South Africa11223367892023/02/28ONC129CAStrStrawberry163.04158Ms Tau084 321 1221
32Sophia's Tea Garden45 Tau avenueJohannesburgGauteng0002South Africa11223368002023/02/28ONC130CALemLemon Meringue173.89719Ms Tau084 321 1221
Recorded_Sales
VBA Code:
 
Upvote 0
Well, you provided as good as no information, so it's almost impossible to help you.
Please provide more information like the code, some sample data, a.s.o.

__________
EDIT: ok, there's at least some data to start with. Now please give some more info on what you would like to achieve.
 
Upvote 0
Activity4_8 (3) - Test.xlsm
ABCDEFGHIJKLMNOP
1NameAdressCityProvincePostal CodeCountryVAT numberInvoice dateInvoice numberProduct codeDescriptionRateQtyDelivery distanceProduct Manager:Contact Number:
2Bela Bela Supermarket12 Church streetBela BelaLimpopo1001South Africa11223368112023/02/04ONC100CABanBanana89.131156Ms Tau084 321 1221
3Magalies Tea Garden3 Baber streetHartbeespoortNorth West6000South Africa11223368992023/02/04ONC101CATirTiramisu175.65679Ms Tau084 321 1221
4Klapoog's Castle89 Seahorse avenueLangebaanWestern Cape3001South Africa11223368662023/02/04ONC102CABanBanana89.1341471Ms Tau084 321 1221
5Klapoog's Castle89 Seahorse avenueLangebaanWestern Cape3001South Africa11223368662023/02/04ONC103CAPanPancakes78.2511471Ms Tau084 321 1221
6Mermaid Bakery123 Strand streetCape TownWestern Cape3000South Africa11223368552023/02/06ONC104CACrmCaramel163.0481420Ms Tau084 321 1221
7Jawsmunger5 Beach roadGqeberhaEastern Cape2000South Africa11223368442023/02/06ONC105CAFruFruit173.8981068Ms Tau084 321 1221
8Mermaid Bakery123 Strand streetCape TownWestern Cape3000South Africa11223368552023/02/07ONC106CACrmCaramel163.0461420Ms Tau084 321 1221
9Waterfall Eatery34 Mossie avenueMbombelaMpumulanga5000South Africa11223368882023/02/07ONC107CACrtCarrot163.049329Ms Tau084 321 1221
10Guppies56 Beach roadDurbanKwazulu Natal7000South Africa11223369102023/02/08ONC108CAChoChocolate163.041573Ms Tau084 321 1221
11Roos Hotel31 Daisy avenueBloemfonteinFree State4000South Africa11223368772023/02/09ONC109CAPanPancakes78.259421Ms Tau084 321 1221
12Capricorn Inn51 Unicorn drivePolokwaneLimpopo1002South Africa11223368222023/02/09ONC110CAStrStrawberry163.046309Ms Tau084 321 1221
13Klapoog's Castle89 Seahorse avenueLangebaanWestern Cape3001South Africa11223368662023/02/10ONC111CATirTiramisu175.6591471Ms Tau084 321 1221
14Magalies Tea Garden3 Baber streetHartbeespoortNorth West6000South Africa11223368992023/02/11ONC112CABanBanana89.131079Ms Tau084 321 1221
15Guppies56 Beach roadDurbanKwazulu Natal7000South Africa11223369102023/02/13ONC113CAChoChocolate163.043573Ms Tau084 321 1221
16Mermaid Bakery123 Strand streetCape TownWestern Cape3000South Africa11223368552023/02/14ONC114CARedRed velvet184.1331420Ms Tau084 321 1221
17Klapoog's Castle89 Seahorse avenueLangebaanWestern Cape3001South Africa11223368662023/02/14ONC115CAOraOrange163.0461471Ms Tau084 321 1221
18Klapoog's Castle89 Seahorse avenueLangebaanWestern Cape3001South Africa11223368662023/02/16ONC116CAPanPancakes78.2571471Ms Tau084 321 1221
19Guppies56 Beach roadDurbanKwazulu Natal7000South Africa11223369102023/02/17ONC117CAStrStrawberry163.046573Ms Tau084 321 1221
20UNISA student caféPreller streetPretoriaGauteng0001South Africa11223368332023/02/18ONC118CABanBanana89.13758Ms Tau084 321 1221
21Waterfall Eatery34 Mossie avenueMbombelaMpumulanga5000South Africa11223368882023/02/18ONC119CAChoChocolate163.046329Ms Tau084 321 1221
22Roos Hotel31 Daisy avenueBloemfonteinFree State4000South Africa11223368772023/02/19ONC120CAOraOrange163.047421Ms Tau084 321 1221
23Mermaid Bakery123 Strand streetCape TownWestern Cape3000South Africa11223368552023/02/20ONC121CATirTiramisu175.6591420Ms Tau084 321 1221
24Magalies Tea Garden3 Baber streetHartbeespoortNorth West6000South Africa11223368992023/02/21ONC122CATirTiramisu175.65979Ms Tau084 321 1221
25UNISA student caféPreller streetPretoriaGauteng0001South Africa11223368332023/02/21ONC123CACrmCaramel163.04258Ms Tau084 321 1221
26Lulu's Bakery321 Justice streetPretoriaGauteng0001South Africa11223367892023/02/22ONC124CAChoChocolate163.04658Ms Tau084 321 1221
27Jawsmunger5 Beach roadGqeberhaEastern Cape2000South Africa11223368442023/02/25ONC125CACrtCarrot163.0441068Ms Tau084 321 1221
28Capricorn Inn51 Unicorn drivePolokwaneLimpopo1002South Africa11223368222023/02/26ONC126CAChoChocolate163.048309Ms Tau084 321 1221
29UNISA student caféPreller streetPretoriaGauteng0001South Africa11223368332023/02/27ONC127CACrtCarrot163.04558Ms Tau084 321 1221
30Mermaid Bakery123 Strand streetCape TownWestern Cape3000South Africa11223368552023/02/27ONC128CARedRed velvet184.1361420Ms Tau084 321 1221
31Lulu's Bakery321 Justice streetPretoriaGauteng0001South Africa11223367892023/02/28ONC129CAStrStrawberry163.04158Ms Tau084 321 1221
32Sophia's Tea Garden45 Tau avenueJohannesburgGauteng0002South Africa11223368002023/02/28ONC130CALemLemon Meringue173.89719Ms Tau084 321 1221
Recorded_Sales
 
Upvote 0
Hi
sorry i"m new at this....

I need to generate invoice from the data sheet above
 
Upvote 0
Activity4_8 (3) - Test.xlsm
ABCDEFGHIJKLMNOP
1NameAdressCityProvincePostal CodeCountryVAT numberInvoice dateInvoice numberProduct codeDescriptionRateQtyDelivery distanceProduct Manager:Contact Number:
2Bela Bela Supermarket12 Church streetBela BelaLimpopo1001South Africa11223368112023/02/04ONC100CABanBanana89.131156Ms Tau084 321 1221
3Magalies Tea Garden3 Baber streetHartbeespoortNorth West6000South Africa11223368992023/02/04ONC101CATirTiramisu175.65679Ms Tau084 321 1221
4Klapoog's Castle89 Seahorse avenueLangebaanWestern Cape3001South Africa11223368662023/02/04ONC102CABanBanana89.1341471Ms Tau084 321 1221
5Klapoog's Castle89 Seahorse avenueLangebaanWestern Cape3001South Africa11223368662023/02/04ONC103CAPanPancakes78.2511471Ms Tau084 321 1221
6Mermaid Bakery123 Strand streetCape TownWestern Cape3000South Africa11223368552023/02/06ONC104CACrmCaramel163.0481420Ms Tau084 321 1221
7Jawsmunger5 Beach roadGqeberhaEastern Cape2000South Africa11223368442023/02/06ONC105CAFruFruit173.8981068Ms Tau084 321 1221
8Mermaid Bakery123 Strand streetCape TownWestern Cape3000South Africa11223368552023/02/07ONC106CACrmCaramel163.0461420Ms Tau084 321 1221
9Waterfall Eatery34 Mossie avenueMbombelaMpumulanga5000South Africa11223368882023/02/07ONC107CACrtCarrot163.049329Ms Tau084 321 1221
10Guppies56 Beach roadDurbanKwazulu Natal7000South Africa11223369102023/02/08ONC108CAChoChocolate163.041573Ms Tau084 321 1221
11Roos Hotel31 Daisy avenueBloemfonteinFree State4000South Africa11223368772023/02/09ONC109CAPanPancakes78.259421Ms Tau084 321 1221
12Capricorn Inn51 Unicorn drivePolokwaneLimpopo1002South Africa11223368222023/02/09ONC110CAStrStrawberry163.046309Ms Tau084 321 1221
13Klapoog's Castle89 Seahorse avenueLangebaanWestern Cape3001South Africa11223368662023/02/10ONC111CATirTiramisu175.6591471Ms Tau084 321 1221
14Magalies Tea Garden3 Baber streetHartbeespoortNorth West6000South Africa11223368992023/02/11ONC112CABanBanana89.131079Ms Tau084 321 1221
15Guppies56 Beach roadDurbanKwazulu Natal7000South Africa11223369102023/02/13ONC113CAChoChocolate163.043573Ms Tau084 321 1221
16Mermaid Bakery123 Strand streetCape TownWestern Cape3000South Africa11223368552023/02/14ONC114CARedRed velvet184.1331420Ms Tau084 321 1221
17Klapoog's Castle89 Seahorse avenueLangebaanWestern Cape3001South Africa11223368662023/02/14ONC115CAOraOrange163.0461471Ms Tau084 321 1221
18Klapoog's Castle89 Seahorse avenueLangebaanWestern Cape3001South Africa11223368662023/02/16ONC116CAPanPancakes78.2571471Ms Tau084 321 1221
19Guppies56 Beach roadDurbanKwazulu Natal7000South Africa11223369102023/02/17ONC117CAStrStrawberry163.046573Ms Tau084 321 1221
20UNISA student caféPreller streetPretoriaGauteng0001South Africa11223368332023/02/18ONC118CABanBanana89.13758Ms Tau084 321 1221
21Waterfall Eatery34 Mossie avenueMbombelaMpumulanga5000South Africa11223368882023/02/18ONC119CAChoChocolate163.046329Ms Tau084 321 1221
22Roos Hotel31 Daisy avenueBloemfonteinFree State4000South Africa11223368772023/02/19ONC120CAOraOrange163.047421Ms Tau084 321 1221
23Mermaid Bakery123 Strand streetCape TownWestern Cape3000South Africa11223368552023/02/20ONC121CATirTiramisu175.6591420Ms Tau084 321 1221
24Magalies Tea Garden3 Baber streetHartbeespoortNorth West6000South Africa11223368992023/02/21ONC122CATirTiramisu175.65979Ms Tau084 321 1221
25UNISA student caféPreller streetPretoriaGauteng0001South Africa11223368332023/02/21ONC123CACrmCaramel163.04258Ms Tau084 321 1221
26Lulu's Bakery321 Justice streetPretoriaGauteng0001South Africa11223367892023/02/22ONC124CAChoChocolate163.04658Ms Tau084 321 1221
27Jawsmunger5 Beach roadGqeberhaEastern Cape2000South Africa11223368442023/02/25ONC125CACrtCarrot163.0441068Ms Tau084 321 1221
28Capricorn Inn51 Unicorn drivePolokwaneLimpopo1002South Africa11223368222023/02/26ONC126CAChoChocolate163.048309Ms Tau084 321 1221
29UNISA student caféPreller streetPretoriaGauteng0001South Africa11223368332023/02/27ONC127CACrtCarrot163.04558Ms Tau084 321 1221
30Mermaid Bakery123 Strand streetCape TownWestern Cape3000South Africa11223368552023/02/27ONC128CARedRed velvet184.1361420Ms Tau084 321 1221
31Lulu's Bakery321 Justice streetPretoriaGauteng0001South Africa11223367892023/02/28ONC129CAStrStrawberry163.04158Ms Tau084 321 1221
32Sophia's Tea Garden45 Tau avenueJohannesburgGauteng0002South Africa11223368002023/02/28ONC130CALemLemon Meringue173.89719Ms Tau084 321 1221
Recorded_Sales
 
Upvote 0
Activity4_8 (3) - Test.xlsm
ABCDEFGHIJKL
11
22Ikhishi Likagogo (Pty) LtdIsando
33086 121 4311Gauteng
440034
55South Africa
6VAT number: 99123456781
7
8Customer Name:Invoice date:Tax invoice number:Amount due (ZAR)
9-
10
11Due dute:
12
13
14
15
16
17
18CodeDescriptionRateQtyTotal
19-
20-
21Subtotal-
22VAT (15%)-
23Total-
24
25For any queries please contact the product manager. Details are provided below:
26Product Manager:
27Contact Number:
28
29Delivery:
30Delivery is charged at R2.07 (VAT inclusive) for delivery in South Africa.
31
Invoice_Template
Cell Formulas
RangeFormula
J9J9=L23
L19:L20L19=I19*J19
L21,L23L21=L19+L20
L22L22=L21*0.15
 
Upvote 0
okay, and how should the invoice look like?
You could create an sample Excel sheet that shows the invoice.
 
Upvote 0
I Have sent a invoice template with xl2bb
Is this sufficient.

Here is the code I Have written:


Sub To_Generate_Invoices()

'Coded by: J Richter
'Student Number: 35477865
'Date: 15 June 2023
'Module: AIN 3701
'Activity 4.8

'Variable declaration:
Dim RecordedSalesWS As Worksheet
Dim InvoiceTemplateWS As Worksheet
Dim customerName As String
Dim customerAddress As String
Dim customerCity As String
Dim customerProvince As String
Dim customerPostalCode As Single
Dim customerCountry As String
Dim customerVATnumber As String
Dim invoiceDate As Date
Dim TaxinvoiceNumber As String
Dim productCode As String
Dim productDescription As String
Dim productRate As Single
Dim productQuantity As Integer
Dim DeliveryDistance As Integer
Dim productionManager As String
Dim contactNumber As String
Dim a As Long
Dim rng_dest As Range
Dim rng As Range
Dim i As Long
Dim Counter As String
Dim Val As Integer

'Copy_data_to_Invoice_Template

Application.ScreenUpdating = False
i = 1
Set rng_dest = Sheets("Recorded_Sales").Range("A:P")
' Find first empty row in columns A:P on sheet Recorded_Sales
Do Until WorksheetFunction.CountA(rng_dest.Rows(i)) = 0
i = i + 1
Loop
'Copy range A2:P32 on sheet Invoice_Template to Variant array
Set rng = Sheets("Invoice_Template").Range("A2:P32")
' Copy rows containing values to sheet Invoice_Template

For a = 1 To rng.Rows.Count
If WorksheetFunction.CountA(rng.Rows(a)) <> 0 Then
rng_dest.Rows(i).Value = rng.Rows(a).Value
'Copy Invoice number
Sheets("Recorded_Sales").Range("I" & i).Value = Sheets("Invoice_Template").Range("E9").Value
'Copy Date
Sheets("Recorded_Sales").Range("H" & i).Value = Sheets("Invoice_Template").Range("C9").Value
'Copy Costumer name
Sheets("Recorded_Sales").Range("A" & i).Value = Sheets("Invoice_Template").Range("A9").Value
'Copy Costumer Adress
Sheets("Recorded_Sales").Range("B" & i).Value = Sheets("Invoice_Template").Range("A10").Value
'Copy Costumer City
Sheets("Recorded_Sales").Range("C" & i).Value = Sheets("Invoice_Template").Range("A11").Value
'Copy Costumer Province
Sheets("Recorded_Sales").Range("D" & i).Value = Sheets("Invoice_Template").Range("A12").Value
'Copy Costumer Postal Code
Sheets("Recorded_Sales").Range("E" & i).Value = Sheets("Invoice_Template").Range("A13").Value
'Copy Costumer Country
Sheets("Recorded_Sales").Range("F" & i).Value = Sheets("Invoice_Template").Range("A14").Value
'Copy Costumer VAT Number
Sheets("Recorded_Sales").Range("G" & i).Value = Sheets("Invoice_Template").Range("A15").Value
'Copy Delivery Distance
Sheets("Recorded_Sales").Range("N" & i).Value = Sheets("Invoice_Template").Range("J20").Value
'Copy Product Code
Sheets("Recorded_Sales").Range("J" & i).Value = Sheets("Invoice_Template").Range("A19").Value
'Copy Product Description
Sheets("Recorded_Sales").Range("K" & i).Value = Sheets("Invoice_Template").Range("B19").Value
'Copy Product Manager
Sheets("Recorded_Sales").Range("O" & i).Value = Sheets("Invoice_Template").Range("C26").Value
'Copy Product Manager Contact Number
Sheets("Recorded_Sales").Range("P" & i).Value = Sheets("Invoice_Template").Range("C27").Value
'Copy Product Rate
Sheets("Recorded_Sales").Range("L" & i).Value = Sheets("Invoice_Template").Range("I19").Value
'Copy Quantity
Sheets("Recorded_Sales").Range("M" & i).Value = Sheets("Invoice_Template").Range("J19").Value
'Copy Delivery Distance
Sheets("Recorded_Sales").Range("N" & i).Value = Sheets("Invoice_Template").Range("B20").Value
'Copy Delivery Distance
Sheets("Recorded_Sales").Range("N" & i).Value = Sheets("Invoice_Template").Range("B20").Value

i = i + 1
End If
Next a
Application.ScreenUpdating = True
'Constant declaration
Const delRate = 1.8

'Set workbook variables to worksheets
Set RecordedSalesWS = Worksheets("Recorded_Sales")
Set InvoiceTemplateWS = Worksheets("Invoice_Template")



'Assign values to variables
Worksheets("Recorded_Sales").Select
customerName = Range("A2")
Worksheets("Invoice_Template").Select
Worksheets("Invoice_Template").Range("A9").Value = customerName

Worksheets("Recorded_Sales").Select
customerAddress = RecordedSalesWS.Range("B2")
Worksheets("Invoice_Template").Select
Worksheets("Invoice_Template").Range("A10").Value = customerAddress

Worksheets("Recorded_Sales").Select
customerCity = RecordedSalesWS.Range("C2")
Worksheets("Invoice_Template").Select
Worksheets("Invoice_Template").Range("A11").Value = customerCity


Worksheets("Recorded_Sales").Select
customerProvince = RecordedSalesWS.Range("D2")
Worksheets("Invoice_Template").Select
Worksheets("Invoice_Template").Range("A12").Value = customerProvince

Worksheets("Recorded_Sales").Select
customerPostalCode = RecordedSalesWS.Range("E2")
Worksheets("Invoice_Template").Select
Worksheets("Invoice_Template").Range("A13").Value = customerPostalCode

Worksheets("Recorded_Sales").Select
customerCountry = RecordedSalesWS.Range("F2")
Worksheets("Invoice_Template").Select
Worksheets("Invoice_Template").Range("A14").Value = customerCountry

Worksheets("Recorded_Sales").Select
customerVATnumber = RecordedSalesWS.Range("G2")
Worksheets("Invoice_Template").Select
Worksheets("Invoice_Template").Range("A15").Value = "VAT Number:" & customerVATnumber

Worksheets("Recorded_Sales").Select
DeliveryDistance = RecordedSalesWS.Range("N2")
Worksheets("Invoice_Template").Select
Worksheets("Invoice_Template").Range("J20").Value = DeliveryDistance

Worksheets("Recorded_Sales").Select
invoiceDate = Range("H2")
Worksheets("Invoice_Template").Select
Worksheets("Invoice_Template").Range("C9").Value = invoiceDate

Worksheets("Recorded_Sales").Select
TaxinvoiceNumber = Range("I2")
Worksheets("Invoice_Template").Select
Worksheets("Invoice_Template").Range("E9").Value = TaxinvoiceNumber

Worksheets("Recorded_Sales").Select
productCode = Range("J2")
Worksheets("Invoice_Template").Select
Worksheets("Invoice_Template").Range("A19").Value = productCode

Worksheets("Recorded_Sales").Select
productDescription = Range("K2")
Worksheets("Invoice_Template").Select
Worksheets("Invoice_Template").Range("B19").Value = productDescription

Worksheets("Recorded_Sales").Select
productionManager = Range("O2")
Worksheets("Invoice_Template").Select
Worksheets("Invoice_Template").Range("C26").Value = productionManager

Worksheets("Recorded_Sales").Select
contactNumber = Range("P2")
Worksheets("Invoice_Template").Select
Worksheets("Invoice_Template").Range("C27").Value = contactNumber

Worksheets("Recorded_Sales").Select
productRate = Range("L2")
Worksheets("Invoice_Template").Select
Worksheets("Invoice_Template").Range("I19").Value = productRate

Worksheets("Recorded_Sales").Select
productQuantity = Range("M2")
Worksheets("Invoice_Template").Select
Worksheets("Invoice_Template").Range("J19").Value = productQuantity


Worksheets("Recorded_Sales").Select
productQuantity = RecordedSalesWS.Range("M2")
Worksheets("Invoice_Template").Select
InvoiceTemplateWS.Range("J19") = productQuantity

Worksheets("Recorded_Sales").Select
DeliveryDistance = RecordedSalesWS.Range("N2")
Worksheets("Invoice_Template").Select
InvoiceTemplateWS.Range("B20") = "Kilometers - Isando to " & customerCity

Worksheets("Recorded_Sales").Select
productRate = RecordedSalesWS.Range("L2")
Worksheets("Invoice_Template").Select
InvoiceTemplateWS.Range("I20") = delRate

'Constant declaration
Const deliveryRate = 1.8


'Calculations
InvoiceTemplateWS.Range("C12") = invoiceDate + 7

'Fixed code
InvoiceTemplateWS.Range("A20") = "Delivery"


'formatting in destination sheet
InvoiceTemplateWS.Range("A13").NumberFormat = "0000"

'Activate invoice sheet
InvoiceTemplateWS.Activate











End Sub








































'Event code that runs if a cell value is changed
Private Sub Worksheet_Change(ByVal Target As Range)

'Check if the cell value is in column I
If Not Intersect(Target, Range("B:B")) Is Nothing Then

'Copy worksheet based on value in cell E2 in worksheet Sheet1 and put it last
Sheets(Worksheets("Sheet1").Range("E2").Value).Copy , Sheets(Sheets.Count)

'Rename worksheet to the value you entered.
ActiveSheet.Name = Target.Value

End If

'Go back to worksheet Sheet1
Worksheets("Sheet1").Activate




End Sub
 
Upvote 0

Forum statistics

Threads
1,216,074
Messages
6,128,654
Members
449,462
Latest member
Chislobog

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