Dynamic Voucher Number

Amir Wisal

New Member
Joined
Oct 25, 2021
Messages
16
Office Version
  1. 365
Platform
  1. Windows
Hi there,

I want to generate a dynamic sequence of voucher numbers by a formula but if there is a better alternative, I am open to it.

I want it to pick the sequence number dynamically but the catch is - if column "D" states cash before the blank row, I want it to start with CPV and then the dynamic number i.e. CPV-001 etc and if the column D value is bank then the desired result needs to be BPV-001.

Each entry will be separated by a blank row however each entry might have a different number of lines.

Please see the desired result column for reference. file can also be accessed from this Link

DATEDesired ResultACC_CODEACC_DESCRIPTIONDRCR
2022/01/01​
CPV-001500-001Food
300​
2022/01/01​
500-003stationery
100​
2022/01/01​
600-001Cash
400​
2022/01/01​
BPV-001550-001fuel
320​
2022/01/01​
500-008lunch
120​
2022/01/01​
600-001bank
440​
2022/01/02​
CPV-002500-008traveling
20000​
2022/01/02​
100-005printing
10000​
2022/01/02​
550-001postage
5000​
2022/01/02​
600-001Cash
35000​
2022/01/02​
BPV-002500-008traveling
8000​
2022/01/02​
500-0011Misc
5000​
2022/01/02​
500-012entertainment
7000​
2022/01/02​
100-005printing
10000​
2022/01/02​
550-001postage
5000​
2022/01/02​
600-001bank
35000​


Many Thanks,
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
Try:
VBA Code:
Sub CreateVouncherNumber()
    Application.ScreenUpdating = False
    Dim i As Long, fRow As Long, lRow As Long, cnt1 As Long, cnt2 As Long
    With Range("A2", Range("A" & Rows.Count).End(xlUp)).SpecialCells(xlCellTypeConstants)
        For i = 1 To .Areas.Count
            fRow = .Areas(i).Cells(1).Row
            lRow = .Areas(i).Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
            If Range("D" & lRow) = "Cash" Then
                cnt1 = cnt1 + 1
                Range("B" & fRow) = "CPV-" & cnt1
            Else
                cnt2 = cnt2 + 1
                Range("B" & fRow) = "BPV-" & cnt1
            End If
        Next i
    End With
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Thank you very much, Mumps. i really appreciate your time and effort.

The above code works like a charm on the sample data however when i add more data to the sample data above and run the code, i does not update the voucher number as required.

it copies the voucher number above the new data and keeps on pasting it. i have highlighted the results as red below

Can you please make it work if i add more data to the tab.

Further, i want the macro to trigger whenever i type "Cash" or "Bank" in column D with values in column F, if possible. if not i can manually run it everytime.

DATEDesired ResultACC_CODEACC_DESCRIPTIONDRCR
2022/01/01​
CPV-1500-001Food
300​
2022/01/01​
500-003stationery
100​
2022/01/01​
600-001Cash
400​
2022/01/01​
BPV-1550-001fuel
320​
2022/01/01​
500-008lunch
120​
2022/01/01​
600-001bank
440​
2022/01/02​
CPV-2500-008travelling
20000​
2022/01/02​
100-005printing
10000​
2022/01/02​
550-001postage
5000​
2022/01/02​
600-001Cash
35000​
2022/01/02​
BPV-2500-008travelling
8000​
2022/01/02​
500-0011Misc
5000​
2022/01/02​
500-012entertainment
7000​
2022/01/02​
100-005printing
10000​
2022/01/02​
550-001postage
5000​
2022/01/02​
600-001bank
35000​
2022/01/02​
BPV-2500-008travelling
8000​
2022/01/02​
500-0011Misc
5000​
2022/01/02​
600-001bank
13000​
2022/01/01​
BPV-2500-008lunch
120​
2022/01/01​
600-001cash
120​
 
Upvote 0
Try:
VBA Code:
Option Compare Text
Sub CreateVouncherNumber()
    Application.ScreenUpdating = False
    Dim i As Long, fRow As Long, lRow As Long, cnt1 As Long, cnt2 As Long
    With Range("A2", Range("A" & Rows.Count).End(xlUp)).SpecialCells(xlCellTypeConstants)
        For i = 1 To .Areas.Count
            fRow = .Areas(i).Cells(1).Row
            lRow = .Areas(i).Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
            If Range("D" & lRow) = "Cash" Then
                cnt1 = cnt1 + 1
                Range("B" & fRow) = "CPV-" & cnt1
            Else
                cnt2 = cnt2 + 1
                Range("B" & fRow) = "BPV-" & cnt2
            End If
        Next i
    End With
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution
generate a dynamic sequence of voucher numbers by a formula
See if this does what you want.

22 12 19.xlsm
ABCD
1DATEDesired ResultACC_CODEACC_DESCRIPTION
21/01/2022CPV-001500-001Food
31/01/2022 500-003stationery
41/01/2022 600-001Cash
5 
61/01/2022BPV-001550-001fuel
71/01/2022 500-008lunch
81/01/2022 600-001bank
9 
102/01/2022CPV-002500-008traveling
112/01/2022 100-005printing
122/01/2022 550-001postage
132/01/2022 600-001Cash
14 
152/01/2022BPV-002500-008traveling
162/01/2022 500-0011Misc
172/01/2022 500-012entertainment
182/01/2022 100-005printing
192/01/2022 550-001postage
202/01/2022 600-001bank
21
Voucher Number
Cell Formulas
RangeFormula
B2:B20B2=IF(ISNUMBER(A1),"",LET(p,IF(XLOOKUP(TRUE,ISBLANK(A3:A21),D2:D20)="Cash","CPV","BPV"),p&TEXT(COUNTIF(B$1:B1,p&"*")+1,"-000")))
 
Upvote 0
Thank you both @mumps and @Peter_SSs.

You guys made my day. Both approaches work fine.

@Peter_SSs still trying to digest the formula, seeing the use of the Let function for the first time. will break it into segments, hope it will make sense to me.

I really appreciate your time, effort and energy. Thanks again
 
Upvote 0
You're welcome. Glad we could help.

If you are going for a vba approach then another option might be

VBA Code:
Sub Voucher_Nums()
  Dim rA As Range
  Dim s As String
  Dim p As Long
  
  s = "CPV-001:BPV-001"  '<- Voucher starting values
  For Each rA In Range("D2", Range("D" & Rows.Count).End(xlUp)).SpecialCells(xlConstants).Areas
    p = IIf(LCase(rA(rA.Count)) = "cash", 1, 9)
    rA(1, -1) = Mid(s, p, 7)
    Mid(s, p + 4, 3) = Format(Mid(s, p + 4, 3) + 1, "000")
  Next rA
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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