Clean & Customize xlsx sheet with a code

RAJESH1960

Banned for repeated rules violations
Joined
Mar 26, 2020
Messages
2,313
Office Version
  1. 2019
Platform
  1. Windows
Hello guys,
I need your expertise to help me to correct this code.
When I run the code, there is just one sheet in the workbook -"Bank". After I run the code a new sheet is created with the new name (first and last dates in the Bank sheet). That new sheet is moved and copied to a different folder. Again, I replace the data in the bank sheet and run the code. This way it goes on. Each time, I have to correct the mismatches because of the code.
The code runs without any error. Still, I am not getting the expected result and the wrong msg box as mismatched.

Changes required.
1. Column G is missing to take the 2 decimals and hence it is showing a mismatch.
2. Column G may contain Cr or Dr in different scenarios. The code shows Cr only. I would like to include Dr also in that line.
3. As there are 2 different balances Dr. & Cr, the formula to be made optional. If the column G contains Cr. Then the formula in column H should be =H2+F3-E3 and if the column G contains Cr, then the formula in column H should be =H2-F3+E3. Please note that it is possible that a single workbook with data may contain both Dr and Cr. So, if the code checks each row for Dr and Cr it can apply the formula accordingly in each row.
4. If possible, Column G to be copied as it is without removing Dr. Cr.

After that, I hope that will solve all the problems
FYI, This code was posted in other forum a few days back. But as I was not able to get the expected result, I am sharing it here again.
0. Edit code Clean & customise.xlsm
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
If the balance column contains Cr. then the formula applied in cell H3 will be ==H2-E3+F3. The value in cell H2 is copied from G2 without the Dr. after sorting the data by line from largest to smallest
The expected result of Dr. is as below
LineTxn DateMonthVoucher TypeDr AmountCr AmountBalanceCheckNarration
50​
04-04-2022​
Apr-2022​
Recipt
17329.00​
2,02,34,637.3
7 Dr.
20234637.37​
SEFT_IS:269864133 31SXC/0026/ TUMKUS MESCHASTS CSE Txn no. S23164584
49​
05-04-2022​
Apr-2022​
Recipt
4050.00​
2,02,30,587.3
7 Dr.
20230587.37​
SEFT_IS:SXIS32209 5227592/0033/ VVMVP Txn no. S49355010
48​
06-04-2022​
Apr-2022​
Recipt
18209.00​
2,02,12,378.3
7 Dr.
20212378.37​
XY ISST 55443 : CTO386-1 SXAY LAT Txn no. S68979605
47​
06-04-2022​
Apr-2022​
Recipt
18000.00​
2,01,94,378.3
7 Dr.
20194378.37​
XY ISST 250688 : CTO386-1 SXAY LAT Txn no. S68979605
46​
06-04-2022​
Apr-2022​
Payment
18000.00​
2,02,12,378.3
7 Dr.
20212378.37​
WSOSGLY CHQ CSESXITESX Txn no. M1149047
45​
07-04-2022​
Apr-2022​
Recipt
18000.00​
2,01,94,378.3
7 Dr.
20194378.37​
XY ISST 250688 : CTO386-1 SXAY LAT Txn no. S84642018
44​
07-04-2022​
Apr-2022​
Recipt
2327.00​
2,01,92,051.3
7 Dr.
20192051.37​
SEFT_IS:270385894 11SXC/0029/ TUMKUS MESCHASTS CSE Txn no. S88549767
43​
11-04-2022​
Apr-2022​
Recipt
500.00​
2,01,91,551.3
7 Dr.
20191551.37​
UPI/210100547184/P 2A/0000001028/ifsc.S pciPaytm Txn no. S56993294
42​
11-04-2022​
Apr-2022​
Recipt
10613.00​
2,01,80,938.3
7 Dr.
20180938.37​
SEFT_IS:270878793 21SXC/0035/ TUMKUS MESCHASTS CSE Txn no. S77404573
41​
12-04-2022​
Apr-2022​
Recipt
9280.00​
2,01,71,658.3
7 Dr.
20171658.37​
XY ISST 202 : CTO386-1 SXAY LAT Txn no. S88579325
40​
12-04-2022​
Apr-2022​
Recipt
96819.00​
2,00,74,839.3
7 Dr.
20074839.37​
SEFT_IS:S10222191 5885587/0037/ A A VESTUSES Txn no. S96963561
39​
13-04-2022​
Apr-2022​
Recipt
30510.00​
2,00,44,329.3
7 Dr.
20044329.37​
SEFT_IS:0413i27405 850061/0022/ HASYASA HASSXLOOM CESTSE Txn no. S6836125
38​
13-04-2022​
Apr-2022​
Recipt
18000.00​
2,00,26,329.3
7 Dr.
20026329.37​
XY ISST 250689 : CTO386-1 SXAY LAT Txn no. S7402938
37​
16-04-2022​
Apr-2022​
Recipt
2466.00​
2,00,23,863.3
7 Dr.
20023863.37​
SEFT_IS:271620137 21SXC/0029/ TUMKUS MESCHASTS CSE Txn no. S66857366
36​
18-04-2022​
Apr-2022​
Recipt
23445.00​
2,00,00,418.3
7 Dr.
20000418.37​
SEFT_IS:0418i27412 872101/0024/ HASYASA HASSXLOOM CESTSE Txn no. S99909549
35​
18-04-2022​
Apr-2022​
Recipt
10886.00​
1,99,89,532.3
7 Dr.
19989532.37​
SEFT_IS:271803171 31SXC/0027/ TUMKUS MESCHASTS CSE Txn no. S1794883
34​
19-04-2022​
Apr-2022​
Payment
236.00​
1,99,89,768.3
7 Dr.
19989768.37​
SHOSTFAL SEC- ISw StSS ChSgs:297857 Txn no. S15659993
33​
19-04-2022​
Apr-2022​
Payment
236.00​
1,99,90,004.3
7 Dr.
19990004.37​
SHOSTFAL SEC- ISw StSS ChSgs:297866 Txn no. S15660860
32​
19-04-2022​
Apr-2022​
Payment
236.00​
1,99,90,240.3
7 Dr.
19990240.37​
SHOSTFAL SEC- ISw StSS ChSgs:297875 Txn no. S15660924
31​
19-04-2022​
Apr-2022​
Payment
236.00​
1,99,90,476.3
7 Dr.
19990476.37​
SHOSTFAL SEC- ISw StSS ChSgs:321803 Txn no. S15660976
30​
19-04-2022​
Apr-2022​
Payment
236.00​
1,99,90,712.3
7 Dr.
19990712.37​
SHOSTFAL SEC- ISw StSS ChSgs:404421 Txn no. S15661047
29​
19-04-2022​
Apr-2022​
Payment
236.00​
1,99,90,948.3
7 Dr.
19990948.37​
SHOSTFAL SEC- ISw StSS ChSgs:404409 Txn no. S15661113
28​
19-04-2022​
Apr-2022​
Payment
236.00​
1,99,91,184.3
7 Dr.
19991184.37​
SHOSTFAL SEC- ISw StSS ChSgs:959930 Txn no. S15661177
27​
19-04-2022​
Apr-2022​
Payment
236.00​
1,99,91,420.3
7 Dr.
19991420.37​
SHOSTFAL SEC- ISw StSS ChSgs:297866 Txn no. S15662064
26​
19-04-2022​
Apr-2022​
Payment
236.00​
1,99,91,656.3
7 Dr.
19991656.37​
SHOSTFAL SEC- ISw StSS ChSgs:297877 Txn no. S15662178
25​
19-04-2022​
Apr-2022​
Payment
236.00​
1,99,91,892.3
7 Dr.
19991892.37​
SHOSTFAL SEC- ISw StSS ChSgs:297902 Txn no. S15662306
24​
19-04-2022​
Apr-2022​
Payment
236.00​
1,99,92,128.3
7 Dr.
19992128.37​
SHOSTFAL SEC- ISw StSS ChSgs:297878 Txn no. S15662847
23​
19-04-2022​
Apr-2022​
Payment
236.00​
1,99,92,364.3
7 Dr.
19992364.37​
SHOSTFAL SEC- ISw StSS ChSgs:297843 Txn no. S15662989
22​
19-04-2022​
Apr-2022​
Payment
236.00​
1,99,92,600.3
7 Dr.
19992600.37​
SHOSTFAL SEC- ISw StSS ChSgs:297856 Txn no. S15663110
21​
19-04-2022​
Apr-2022​
Payment
236.00​
1,99,92,836.3
7 Dr.
19992836.37​
SHOSTFAL SEC- ISw StSS ChSgs:297857 Txn no. S15663248
20​
19-04-2022​
Apr-2022​
Payment
236.00​
1,99,93,072.3
7 Dr.
19993072.37​
SHOSTFAL SEC- ISw StSS ChSgs:321804 Txn no. S15663352
19​
19-04-2022​
Apr-2022​
Payment
502.68​
1,99,93,575.0
5 Dr.
19993575.05​
SHOSTFAL SEC- LeSXgeS Folio ChaSges fSom 01-01-2022 Txn no. S15663487
18​
19-04-2022​
Apr-2022​
Payment
236.00​
1,99,93,811.0
5 Dr.
19993811.05​
SHOSTFAL SEC- ISw StSS ChSgs:297918 Txn no. S15663574
17​
19-04-2022​
Apr-2022​
Payment
236.00​
1,99,94,047.0
5 Dr.
19994047.05​
SHOSTFAL SEC- ISw StSS ChSgs:115907 Txn no. S15664459
16​
19-04-2022​
Apr-2022​
Payment
236.00​
1,99,94,283.0
5 Dr.
19994283.05​
SHOSTFAL SEC- ISw StSS ChSgs:297879 Txn no. S15664537
15​
21-04-2022​
Apr-2022​
Recipt
2288.00​
1,99,91,995.0
5 Dr.
19991995.05​
SEFT_IS:272317265 71SXC/0032/ TUMKUS MESCHASTS CSE Txn no. S66220097
14​
26-04-2022​
Apr-2022​
Recipt
11015.00​
1,99,80,980.0
5 Dr.
19980980.05​
IMPS- IS/211604568698/000 0000000/OSe97 Co Txn no. S45813243
13​
26-04-2022​
Apr-2022​
Payment
17000.00​
1,99,97,980.0
5 Dr.
19997980.05​
KASHA -011900 Txn no. M1028982 Branch Name NEW DELHI BANK STREET KAROLBAGH Cheque no. 297872
12​
26-04-2022​
Apr-2022​
Recipt
34090.00​
1,99,63,890.0
5 Dr.
19963890.05​
IMPS- IS/211615145060/990 0514264/JUST LIS Txn no. S59135511
11​
26-04-2022​
Apr-2022​
Payment
17000.00​
1,99,80,890.0
5 Dr.
19980890.05​
SAI PSIYASXASSHISI Txn no. M1173671 Cheque no. 257660
10​
26-04-2022​
Apr-2022​
Payment
15000.00​
1,99,95,890.0
5 Dr.
19995890.05​
SAI PSIYASXASSHISI Txn no. M956493 Cheque no. 257659
9​
27-04-2022​
Apr-2022​
Payment
236.00​
1,99,96,126.0
5 Dr.
19996126.05​
SHOSTFAL SEC- ISw StSS ChSgs:257651 Txn no. S69927903
8​
27-04-2022​
Apr-2022​
Recipt
3914.00​
1,99,92,212.0
5 Dr.
19992212.05​
SEFT_IS:273028483 91SXC/0025/ TUMKUS MESCHASTS CSE Txn no. S75478331
7​
27-04-2022​
Apr-2022​
Recipt
7315.00​
1,99,84,897.0
5 Dr.
19984897.05​
IMPS- IS/211715196767/990 0514264/JUST LIS Txn no. S78963863
6​
28-04-2022​
Apr-2022​
Payment
15000.00​
1,99,99,897.0
5 Dr.
19999897.05​
SAI PSIYASXASSHISI Txn no. M654992 Cheque no. 257661
5​
29-04-2022​
Apr-2022​
Recipt
20051.00​
1,99,79,846.0
5 Dr.
19979846.05​
SEFT_IS:SXIS72211 9423852/0030/ SSSST XSSX Txn no. S18761777
4​
30-04-2022​
Apr-2022​
Payment
9100.00​
1,99,88,946.0
5 Dr.
19988946.05​
SX15987819CS TO004000990000077 4 Txn no. S26662454
3​
30-04-2022​
Apr-2022​
Payment
236.00​
1,99,89,182.0
5 Dr.
19989182.05​
IW CHQ : 297844 SEJ Txn no. S32462512
2​
30-04-2022​
Apr-2022​
Payment
10000.00​
1,99,99,182.05 Dr.
19999182.05​
SAI PSIYASXASSHISI Txn no. M513559 Cheque no. 257662
1​
30-04-2022​
Apr-2022​
Payment
196568.00​
2,01,95,750.05 Dr.
20195750.05​
0040008700012345 St.Coll:01-04-2022 to 30-04-202 Txn no. S44791543
 
Upvote 0
How about:

VBA Code:
Option Explicit

Sub Bank_Clean()
'
'shared by CheeseSandwich
    Dim ar
    Dim i                       As Long
    Dim n                       As Long
    Dim lr                      As Long
    Dim BranchName              As String, ChequeNo As String
    Dim sname                   As String
    Dim arr(1 To 10000, 1 To 9)
    Dim hdr                     As Variant
    Dim ws1                     As Worksheet, ws2   As Worksheet
'
    Application.ScreenUpdating = False
'
    hdr = Array("Line", "Txn Date", "Month", "Voucher Type", "Dr Amount", "Cr Amount", "Balance", "Check", "Narration")
'
    Set ws1 = Worksheets("Bank")
'
    lr = ws1.Range("A" & Rows.Count).End(xlUp).Row
    ws1.Range("H2:H" & lr).Replace What:="" & Chr(10) & "", Replacement:="", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False      ' Remove any 'Balance' Column H Line feeds
'
    With ws1.Range("H2:H" & lr)
        .VerticalAlignment = xlTop
        .Font.Name = "Calibri Light"
        .Font.FontStyle = "Bold"
        .Font.Size = 14
    End With
'
    ws1.Columns("H:H").AutoFit
'
    ar = ws1.[A1].CurrentRegion
    lr = UBound(ar, 1)
    sname = Replace(CStr(ar(lr, 2)) & " to " & CStr(ar(2, 2)), "/", "-")
'
    For i = 2 To UBound(ar, 1)
        BranchName = "": ChequeNo = ""
        n = i - 1
'
        arr(n, 1) = n
        arr(n, 2) = ar(i, 2): arr(n, 3) = ar(i, 2)
        arr(n, 5) = ar(i, 6)
'
        If arr(n, 5) <> 0 Then arr(n, 4) = "Payment"
        arr(n, 6) = ar(i, 7)
        If arr(n, 6) <> 0 Then arr(n, 4) = "Recipt"
        arr(n, 7) = CDbl(Split(Replace(ar(i, 8), " ", vbNewLine), Chr(10))(0))
        arr(n, 8) = CDbl(Split(Replace(ar(i, 8), " ", vbNewLine), Chr(10))(0))
        If ar(i, 4) <> "-" Then BranchName = " Branch Name " & ar(i, 4)
        If ar(i, 5) <> "" Then ChequeNo = " Cheque no. " & ar(i, 5)
        arr(n, 9) = ar(i, 3) & " Txn no. " & ar(i, 1) & BranchName & ChequeNo
    Next i
'
    Sheets.Add(After:=Sheets("Bank")).Name = sname
    Set ws2 = Worksheets(sname)

    With ws2
        lr = UBound(ar, 1)
        .[A1].Resize(, 9) = hdr
        .[A2].Resize(UBound(ar, 1) - 1, 9) = arr
'
        .Columns("I:I").WrapText = False
        .Columns("C:C").NumberFormat = "mmm-yyyy"
        .Columns("E:F").NumberFormat = "0.00"
'
        .Range("A1:I" & lr).Sort Key1:=.Range("A1"), Order1:=xlDescending
'
        For i = 2 To lr
            .Cells(n, 8) = .Cells(i, 8) + .Cells(n, 6).Value - .Cells(n, 5).Value
            n = i + 1
        Next i
        With .Columns("A:I")
            .Font.Name = "Calibri"
            .Font.Size = 11
            .AutoFit
        End With
        .Columns("I:I").Replace What:="" & Chr(10) & "", Replacement:=" ", LookAt:=xlPart, _
                SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
'
        .Range("H3:H" & lr).Formula = "=R[-1]C-RC[-2]+RC[-3]"
        .Range("H3:H" & lr).Value = .Range("H3:H" & lr).Value
'
        Application.ScreenUpdating = True
        If Left(.Range("G" & lr).Value, InStr(.Range("G" & lr).Value, ".") + 2) = Trim(Int(.Range("H" & lr).Value * 100) / 100) Then
            MsgBox "Data cleaned & Matched Sccessfully"
        Else
            MsgBox "Mismatched. Check if any row is missed to enter"
        End If
    End With
End Sub
 
Upvote 0
How about:

VBA Code:
Option Explicit

Sub Bank_Clean()
'
'shared by CheeseSandwich
    Dim ar
    Dim i                       As Long
    Dim n                       As Long
    Dim lr                      As Long
    Dim BranchName              As String, ChequeNo As String
    Dim sname                   As String
    Dim arr(1 To 10000, 1 To 9)
    Dim hdr                     As Variant
    Dim ws1                     As Worksheet, ws2   As Worksheet
'
    Application.ScreenUpdating = False
'
    hdr = Array("Line", "Txn Date", "Month", "Voucher Type", "Dr Amount", "Cr Amount", "Balance", "Check", "Narration")
'
    Set ws1 = Worksheets("Bank")
'
    lr = ws1.Range("A" & Rows.Count).End(xlUp).Row
    ws1.Range("H2:H" & lr).Replace What:="" & Chr(10) & "", Replacement:="", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False      ' Remove any 'Balance' Column H Line feeds
'
    With ws1.Range("H2:H" & lr)
        .VerticalAlignment = xlTop
        .Font.Name = "Calibri Light"
        .Font.FontStyle = "Bold"
        .Font.Size = 14
    End With
'
    ws1.Columns("H:H").AutoFit
'
    ar = ws1.[A1].CurrentRegion
    lr = UBound(ar, 1)
    sname = Replace(CStr(ar(lr, 2)) & " to " & CStr(ar(2, 2)), "/", "-")
'
    For i = 2 To UBound(ar, 1)
        BranchName = "": ChequeNo = ""
        n = i - 1
'
        arr(n, 1) = n
        arr(n, 2) = ar(i, 2): arr(n, 3) = ar(i, 2)
        arr(n, 5) = ar(i, 6)
'
        If arr(n, 5) <> 0 Then arr(n, 4) = "Payment"
        arr(n, 6) = ar(i, 7)
        If arr(n, 6) <> 0 Then arr(n, 4) = "Recipt"
        arr(n, 7) = CDbl(Split(Replace(ar(i, 8), " ", vbNewLine), Chr(10))(0))
        arr(n, 8) = CDbl(Split(Replace(ar(i, 8), " ", vbNewLine), Chr(10))(0))
        If ar(i, 4) <> "-" Then BranchName = " Branch Name " & ar(i, 4)
        If ar(i, 5) <> "" Then ChequeNo = " Cheque no. " & ar(i, 5)
        arr(n, 9) = ar(i, 3) & " Txn no. " & ar(i, 1) & BranchName & ChequeNo
    Next i
'
    Sheets.Add(After:=Sheets("Bank")).Name = sname
    Set ws2 = Worksheets(sname)

    With ws2
        lr = UBound(ar, 1)
        .[A1].Resize(, 9) = hdr
        .[A2].Resize(UBound(ar, 1) - 1, 9) = arr
'
        .Columns("I:I").WrapText = False
        .Columns("C:C").NumberFormat = "mmm-yyyy"
        .Columns("E:F").NumberFormat = "0.00"
'
        .Range("A1:I" & lr).Sort Key1:=.Range("A1"), Order1:=xlDescending
'
        For i = 2 To lr
            .Cells(n, 8) = .Cells(i, 8) + .Cells(n, 6).Value - .Cells(n, 5).Value
            n = i + 1
        Next i
        With .Columns("A:I")
            .Font.Name = "Calibri"
            .Font.Size = 11
            .AutoFit
        End With
        .Columns("I:I").Replace What:="" & Chr(10) & "", Replacement:=" ", LookAt:=xlPart, _
                SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
'
        .Range("H3:H" & lr).Formula = "=R[-1]C-RC[-2]+RC[-3]"
        .Range("H3:H" & lr).Value = .Range("H3:H" & lr).Value
'
        Application.ScreenUpdating = True
        If Left(.Range("G" & lr).Value, InStr(.Range("G" & lr).Value, ".") + 2) = Trim(Int(.Range("H" & lr).Value * 100) / 100) Then
            MsgBox "Data cleaned & Matched Sccessfully"
        Else
            MsgBox "Mismatched. Check if any row is missed to enter"
        End If
    End With
End Sub
JohnnyL. This code is perfect for data with Dr Balances. If you can make it working for both kind of data Dr. & Cr it would be perfect.
'
How about an if function here, If the character is Dr. then use this formula
Rich (BB code):
 .Range("H3:H" & lr).Formula = "=R[-1]C-RC[-2]+RC[-3]"
        .Range("H3:H" & lr).Value = .Range("H3:H" & lr).Value
'
Else use this formula
'
Rich (BB code):
   .Range("H3:H" & lr).Formula = "=R[-1]C+RC[-2]-RC[-3]"
        .Range("H3:H" & lr).Value = .Range("H3:H" & lr).Value
'

Solved Edit code Clean & customise.xlsm
ABCDEFGHI
1Txn No.Txn DateDescriptionBranch NameCheque No.Dr AmountCr AmountBalance
2S6988547127-04-2021POS 111719025266 HPCL JAI HIND \-300.00406.11 Cr.
3S6682975727-04-202100400021000010101 To: 0040005501234567ASDF24,500.00706.11 Cr.
4S6669387727-04-2021ATM REV 7124 PNB \+ NO 8 ROAD \ BENGAABCD1234567815,000.0025,206.11 Cr.
5S6669366827-04-2021ATM WDR 7124 PNB \+ NO 8 ROAD \ BENGA-1234567915,000.0010,206.11 Cr.
6S6650633227-04-20210040005501234567 To: 00400021000010101-1234567025,000.0025,206.11 Cr.
7S5689239926-04-2021POS 111617913133 Ho Transport Northgdwn \HIJK240.00206.11 Cr.
8S2463873023-04-2021POS 111313920305 Ho Transport Northgdwn \-640.00446.11 Cr.
9S1522958022-04-2021POS 111217851515 SUN ENTERPRISES \-1,000.001,086.11 Cr.
10S1421056522-04-20210040008700003314 To: 00400021000010101-2,000.002,086.11 Cr.
11S365423521-04-2021POS 111117022323 LAKSHMAMMA AND SONS \-300.0086.11 Cr.
12S119686721-04-2021CHRG BAL 111114024410 +BENGALURU CANTONMENT \-10.62386.11 Cr.
13S31225021-04-2021POS 111113828860 SAFE AUTO \-198.50396.73 Cr.
14S7821012319-04-2021POS 110917543980 SAFE AUTO \-297.75595.23 Cr.
15S7521969119-04-2021POS 110914900731 Ho Transport Northgdwn \M G ROAD123456891,370.00892.98 Cr.
16S7505971619-04-20210040005501234567 To: 00400021000010101-1,500.002,262.98 Cr.
17S7375117019-04-2021POS 110912298055 SRI HANUMAN ST\-300.00762.98 Cr.
18S5633359617-04-2021POS 110716112239 CAUVERY AUTO \-992.501,062.98 Cr.
19S5605585717-04-20210040005501234567 To: 00400021000010101-1,200.002,055.48 Cr.
20S5365258717-04-2021RREF/W01/106220931647/60 7093XX-4.50855.48 Cr.
21S3244843715-04-2021POS 110519007566 HPCL JAI HIND \-300.00850.98 Cr.
22S2968828915-04-2021POS 110516574363 SAFE AUTO \-300.001,150.98 Cr.
23S2189594515-04-2021SHORTFAL REC- QAB Charges from 01-01-2021 to 31-03-223.021,450.98 Cr.
24S2013847614-04-20210040005501234567 To: 00400021000010101XXXX213121631,000.001,674.00 Cr.
25S1510417914-04-2021POS 110413586859 INDIA GARAGE S\-300.00674.00 Cr.
26S288588213-04-2021POS 110314027233 EAST END \-300.00974.00 Cr.
27S9043821912-04-2021POS 110217030404 INDIA GARAGE ENTERPRISES \-8,726.001,274.00 Cr.
28S8957026712-04-20210040008700003314 To: 00400021000010101-10,000.0010,000.00 Cr.
29S8198382412-04-2021QAB Charges from 01-01- 2021 to 31-03-2021-130.980.00 Cr.
30S7823362411-04-2021POS 110118013506 EAST END \-300.00130.98 Cr.
31S6710008410-04-2021POS 110016918038 Ho Transport Northgdwn \-800.00430.98 Cr.
32S6701851410-04-20210040005501234567 To: 00400021000010101-1,000.001,230.98 Cr.
33S6256501910-04-2021SMS CHRG FOR:01-01- 2021to31-03-2021-29.50230.98 Cr.
34S5082156909-04-2021POS 109913060943 SAFE AUTO \-12365478300.00260.48 Cr.
35S3762409208-04-2021POS 109815242197 K N GURUSWAMY \-913.94560.48 Cr.
36S3747888208-04-2021POS 109815902040 CHAWLA MOTORS \-1,900.001,474.42 Cr.
37S3747023508-04-20210040005501234567 To: 00400021000010101-1,000.003,374.42 Cr.
38S3745331908-04-20210040005501234567 To: 00400021000010101-2,000.002,374.42 Cr.
39S1199137506-04-2021POS 109618689975 INDIA GARAGE S\-300.00374.42 Cr.
40S915349206-04-2021POS 109616519680 SAFE AUTO \-595.50674.42 Cr.
41S899402906-04-20210040005501234567 To: 00400021000010101-1,000.001,269.92 Cr.
42S553327506-04-2021POS 109612003339 EAST END \-300.00269.92 Cr.
43S9559559705-04-2021POS 109517918144 Ho Transport Northgdwn \-560.00569.92 Cr.
44S7670580204-04-2021INCIDENTAL CHARGES-306.801,129.92 Cr.
45S4716291802-04-2021POS REV 109212541089 SAFE AUTO \-300.001,436.72 Cr.
46S4708342902-04-2021POS REV 109212542261 SAFE AUTO \-22235656300.001,136.72 Cr.
47S4708089702-04-2021POS 109212542261 SAFE AUTO \-300.00836.72 Cr.
48S4706489002-04-2021POS 109212541089 SAFE AUTO \-300.001,136.72 Cr.
49S4703829502-04-2021POS REV 109212546495 SAFE AUTO \-300.001,436.72 Cr.
50S4703552602-04-2021POS 109212546495 SAFE AUTO \-300.001,136.72 Cr.
51S4698365102-04-2021POS REV 109212534066 SAFE AUTO \-297.751,436.72 Cr.
52S4698251802-04-2021POS 109212534066 SAFE AUTO \AVENUE ROAD297.751,138.97 Cr.
53S4688802902-04-20210040005501234567 To: 00400021000010101-1,000.001,436.72 Cr.
Bank
 
Upvote 0
This is the correct code shared by John Topley for Cr. The problem is it doesn't match with Dr. amounts due to the single formula in the code.
Rich (BB code):
Option Explicit

Sub Bank_Clean()
'solved by John Topley

Dim hdr As Variant
Dim ar, sname As String, i As Long, n As Long, lr As Long
Dim BranchName As String, ChequeNo As String
Dim arr(1 To 10000, 1 To 9)
Dim ws1 As Worksheet, ws2 As Worksheet
hdr = Array("Line", "Txn Date", "Month", "Voucher Type", "Dr Amount", "Cr Amount", "Balance", "Check", "Narration")
Application.ScreenUpdating = False
Set ws1 = Worksheets("Bank")
ws1.Activate
ar = ws1.[A1].CurrentRegion
Debug.Print ar(53, 1)
''lr = UBound(ar, 1) - 1
    lr = UBound(ar, 1)
sname = Replace(CStr(ar(lr, 2)) & " to " & CStr(ar(2, 2)), "/", "-")

''For i = 2 To UBound(ar, 1) - 1
    For i = 2 To UBound(ar, 1)

    BranchName = "": ChequeNo = ""
    n = i - 1
    arr(n, 1) = n
    arr(n, 2) = ar(i, 2): arr(n, 3) = ar(i, 2)
    arr(n, 5) = ar(i, 6)
    If arr(n, 5) <> 0 Then arr(n, 4) = "Payment"
    arr(n, 6) = ar(i, 7)
    If arr(n, 6) <> 0 Then arr(n, 4) = "Recipt"
    arr(n, 7) = ar(i, 8)
    arr(n, 8) = Replace(ar(i, 8), "Cr.", "")
    If ar(i, 4) <> "-" Then BranchName = " Branch Name " & ar(i, 4)
    If ar(i, 5) <> "" Then ChequeNo = " Cheque no. " & ar(i, 5)
    arr(n, 9) = ar(i, 3) & " Txn no. " & ar(i, 1) & BranchName & ChequeNo
Next i

Sheets.Add(After:=Sheets("Bank")).Name = sname
Set ws2 = Worksheets(sname)

With ws2
''  lr = UBound(ar, 1) - 1
    lr = UBound(ar, 1)
 .[A1].Resize(, 9) = hdr
 .[A2].Resize(UBound(ar, 1) - 1, 9) = arr
 .Columns("I:I").WrapText = False
 .Columns("C:C").NumberFormat = "mmm-yyyy"
 .Columns("E:F").NumberFormat = "0.00"
 .Range("A1:I" & lr).Sort Key1:=.Range("A1"), Order1:=xlDescending
 
 '
 For i = 2 To lr - 1
     n = i + 1
    .Cells(n, 8) = .Cells(i, 8) + .Cells(n, 6) - Cells(n, 5)
 Next i
'
        .Range("H3").Formula = "=H2+F3-E3"
        .Range("H3:H" & lr).FillDown
'
        With .Columns("A:I")
            .Font.Name = "Calibri"
            .Font.Size = 11
            .AutoFit
        End With
'
        .Columns("I:I").Replace What:="" & Chr(10) & "", Replacement:=" ", LookAt:=xlPart, _
                SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
'
End With

Application.ScreenUpdating = True
'
    If Left(ws2.Range("G" & lr).Value, InStr(ws2.Range("G" & lr).Value, ".") + 2) = Trim(Int(ws2.Range("H" & lr).Value * 100) / 100) Then
        MsgBox "Data cleaned & Matched Successfully"
    Else
        MsgBox "Mismatched. Check if any row is missed to enter"
    End If

End Sub
 
Upvote 0
There is a more easier and simple way to solve the above issue. Your code is working correctly if the data is Dr right. You can just add one line in the code stating that if the data has Cr then interchange the columns with values in column F and G like post Cr amount in column F and Dr amount in column G in the sheet extracted from the code. That way the code will work with a single formula.
 
Upvote 0
Try this:

VBA Code:
Option Explicit

Sub Bank_Clean()
'
'shared by CheeseSandwich
    Dim ar
    Dim i                       As Long
    Dim n                       As Long
    Dim lr                      As Long
    Dim BranchName              As String, ChequeNo As String
    Dim sname                   As String
    Dim arr(1 To 10000, 1 To 9)
    Dim hdr                     As Variant
    Dim ws1                     As Worksheet, ws2   As Worksheet
'
    Application.ScreenUpdating = False
'
    hdr = Array("Line", "Txn Date", "Month", "Voucher Type", "Dr Amount", "Cr Amount", "Balance", "Check", "Narration")
'
    Set ws1 = Worksheets("Bank")
'
    ar = ws1.[A1].CurrentRegion
    lr = UBound(ar, 1)
    sname = Replace(CStr(ar(lr, 2)) & " to " & CStr(ar(2, 2)), "/", "-")
'
    For i = 2 To UBound(ar, 1)
        BranchName = "": ChequeNo = ""
        n = i - 1
'
        arr(n, 1) = n
        arr(n, 2) = ar(i, 2): arr(n, 3) = ar(i, 2)
        arr(n, 5) = ar(i, 6)
'
        If arr(n, 5) <> 0 Then arr(n, 4) = "Payment"
        arr(n, 6) = ar(i, 7)
        If arr(n, 6) <> 0 Then arr(n, 4) = "Recipt"
        arr(n, 7) = Replace(ar(i, 8), Chr(10), "")
        arr(n, 8) = CDbl(Left$(arr(n, 7), Len(arr(n, 7)) - 4))
        If ar(i, 4) <> "-" Then BranchName = " Branch Name " & ar(i, 4)
        If ar(i, 5) <> "" Then ChequeNo = " Cheque no. " & ar(i, 5)
        arr(n, 9) = ar(i, 3) & " Txn no. " & ar(i, 1) & BranchName & ChequeNo
    Next i
'
    Sheets.Add(After:=Sheets("Bank")).Name = sname
    Set ws2 = Worksheets(sname)

    With ws2
        lr = UBound(ar, 1)
        .[A1].Resize(, 9) = hdr
        .[A2].Resize(UBound(ar, 1) - 1, 9) = arr
'
        .Columns("I:I").WrapText = False
        .Columns("C:C").NumberFormat = "mmm-yyyy"
        .Columns("E:F").NumberFormat = "0.00"
'
        .Range("A1:I" & lr).Sort Key1:=.Range("A1"), Order1:=xlDescending
'
        For i = 2 To lr
            .Cells(n, 8) = .Cells(i, 8) + .Cells(n, 6).Value - .Cells(n, 5).Value
            n = i + 1
        Next i
        With .Columns("A:I")
            .Font.Name = "Calibri"
            .Font.Size = 11
            .AutoFit
        End With
        .Columns("I:I").Replace What:="" & Chr(10) & "", Replacement:=" ", LookAt:=xlPart, _
                SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
'
        .Range("H3:H" & lr).Formula = "=IF(RIGHT(RC[-1],3)=""Dr."", R[-1]C-RC[-2]+RC[-3],R[-1]C+RC[-2]-RC[-3])"
        .Range("H3:H" & lr).Value = .Range("H3:H" & lr).Value
'
        Application.ScreenUpdating = True
        If Replace(Left(.Range("G" & lr).Value, InStr(.Range("G" & lr).Value, ".") + 2), ",", "") _
                = Trim(Int(.Range("H" & lr).Value * 100) / 100) Then
            MsgBox "Data cleaned & Matched Sccessfully"
        Else
            MsgBox "Mismatched. Check if any row is missed to enter"
        End If
    End With
End Sub
 
Upvote 0
Solution
Will check and revert back in sometime.
 
Upvote 0
JohnnyL. The great. The code is perfect. All the 4 requirements I had stated have been incorporated and the result is superb. Thanks man.
 
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