How to add Loop

HSAR

Banned - Rules violations
Joined
Jul 6, 2020
Messages
37
Office Version
  1. 2016
Platform
  1. Windows
Created a VBA code to putt the relevant details into invoice but do not know how to add a loop to run this till used range.





1595606193255.png





VBA Code:
Sub Sheetdev()

With Sheet3
.Range("C1").Value = "Account:"
.Range("C3").Value = "Head:"
.Range("C5").Value = "Duration:"
.Range("C7").Value = "Party Name:"
.Range("C9").Value = "Type:"
.Range("C11").Value = "Location"
.Range("C14").Value = "Tax Rate:"
.Range("C15").Value = "Qty:"
.Range("C16").Value = "Tax:"
.Range("C22").Value = "Total Amount:"
.Range("C23").Value = "Remarks:"
.Range("H3").Value = "Voucher #:"
.Range("H5").Value = "Date:"
.Range("H7").Value = "Invoice #:"
.Range("H9").Value = "Billing Date:"

'Loop

.Range("E1").Value = Sheet1.Range("C2").Value
.Range("E3").Value = Sheet1.Range("D2").Value
.Range("E5").Value = Sheet1.Range("J2").Value
.Range("E7").Value = Sheet1.Range("F2").Value
.Range("E9").Value = Sheet1.Range("H2").Value
.Range("E11").Value = Sheet1.Range("G2").Value
.Range("E14").Value = Sheet1.Range("K2").Value
.Range("E15").Value = Sheet1.Range("L2").Value
.Range("E16").Value = Sheet1.Range("N2").Value
.Range("J22").Value = Sheet1.Range("O2").Value
.Range("E23").Value = Sheet1.Range("P2").Value
.Range("J3").Value = Sheet1.Range("B2").Value
.Range("J5").Value = Sheet1.Range("E2").Value
.Range("J7").Value = Sheet1.Range("I2").Value
.Range("J9").Value = Sheet1.Range("Q2").Value

End With

End Sub
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
How about:

VBA Code:
Sub Sheetdev()
  Dim ar1 As Variant, ar2 As Variant
  Dim i As Long, j As Long
  
  ar1 = Array("E1", "E3", "E5", "E7", "E9", "E11", "E14", "E15", "E16", "J22", "E23", "J3", "J5", "J7", "J9")
  ar2 = Array("C", "D", "J", "F", "H", "G", "K", "L", "N", "O", "P", "B", "E", "I", "Q")
  
  With Sheet3
    .Range("C1").Value = "Account:"
    .Range("C3").Value = "Head:"
    .Range("C5").Value = "Duration:"
    .Range("C7").Value = "Party Name:"
    .Range("C9").Value = "Type:"
    .Range("C11").Value = "Location"
    .Range("C14").Value = "Tax Rate:"
    .Range("C15").Value = "Qty:"
    .Range("C16").Value = "Tax:"
    .Range("C22").Value = "Total Amount:"
    .Range("C23").Value = "Remarks:"
    .Range("H3").Value = "Voucher #:"
    .Range("H5").Value = "Date:"
    .Range("H7").Value = "Invoice #:"
    .Range("H9").Value = "Billing Date:"
    
    'Loop
    For i = 2 To Sheet1.Range("C" & Rows.Count).End(3).Row
      For j = 0 To UBound(ar1)
        .Range(ar1(j)).Value = Sheet1.Range(ar2(j) & i).Value
      Next
      '
      'Here what you want to do with the invoice, print, save as pdf or excel, etc.
      '
    Next
  End With
End Sub
 
Upvote 0
Here is a very flexible solution, which allows anyone to make changes in future (as I'm sure will be needed) to anything, without changing any code.

1. Add a row at the top, above the table of invoice details. Create a counter cell in that row. Put 1 in it for now.
2. Set up Sheet 3 with all the headings you need (there's no need to have the code do it, when you are going to keep using the same sheet for all your invoices).
3. Insert invoice details on Sheet 3 using formulae, with the counter cell as an offset.
4. Get the code to simply put numbers in sequence in the counter cell, 1,2,3 and this will load invoices in Sheet 3

The example below shows how to set up the formulae
2020-07-25 08_42_33-Book1 - Excel.jpg


Test it by putting numbers in the counter cell and checking that the correct invoice details appear on Sheet 3

Now the code to run through the whole list

VBA Code:
Sub ProduceInvoices()
  Dim i
  i = 1
  'loop as Long as there are more invoices to produce
  Do While Sheets("Sheet1").Cells(4, 1).Offset(i, 0) <> ""
    Cells(2, 3) = i
    Calculate
 
    'insert code to produce your invoice here
 
    i = i + 1
  Loop
End Sub

Now users can change things on the sheets, and the code will still work. (If they are likely to move headings or the location of the counter, you should give them range names and use those names in the code).
 
Upvote 0
Sir thank you so much that you have created this code.

But i was looking for single invoice code i mean, Currently using this via spin button and vlookup if i press the lower spin button then invoice # 0234 will be appear i wanted to learn this process via VBA.

If i press upper spin button then it will show 0232 invoice.

I understand that it is my mistake that i cannot convey proper message before apology.

1595856357911.png
 
Upvote 0
I also wants to add 2 button with name Next and Back for invoices appearing.
 
Upvote 0

Forum statistics

Threads
1,214,976
Messages
6,122,541
Members
449,089
Latest member
davidcom

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