Convert csv to xlsx

RAJESH1960

Banned for repeated rules violations
Joined
Mar 26, 2020
Messages
2,313
Office Version
  1. 2019
Platform
  1. Windows
Hello code experts,
I have this csv file which was converted from pdf. The columns are not in one order to get the data in a columnar view. It will take more than a hour to arrange each line in a particular format. JohnnyL's code has helped me a lot to get the columns in order in less than a second. I am facing one problem in one of the conversions. The code is not able to concatenate the Cheque No. to the Description in 3 different rows and in one of the lines it is not taking the amount. Hence the balances are not matching. I have colored the lines with errors. Need your expertise to correct the code. Thanks.
Conver CSV to XLSX.xlsm
 
Last edited:
Thanks for taking the time to respond. I think I am happy with were my code landed and since you have a working solution I won't worry about adding the Voucher Type column and leave you in Johnny's capable hands.
You mean to say that the ball is in JohnnyL's court now. ?
Thank you very much Alex. It was really great to see and learn so many things from your code.
 
Upvote 0

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
How about:

VBA Code:
Sub LoadCSV_FileTest4()
'
    Dim startTime                       As Single
    Dim CSV_FileToOpen                  As Variant
'
    CSV_FileToOpen = Application.GetOpenFilename("Text files,*.csv", , "Select file", , False)  ' Save full path of CSV file to CSV_FileToOpen
    If CSV_FileToOpen = False Then                                                              ' If user cancelled then ...
        MsgBox "No file selected - exiting"                                                     '   Display message box to user indicating the cancellation
        Exit Sub                                                                                '   Exit the sub
    End If
'
    startTime = Timer                                                                           ' Start the stopwatch
'
    Application.ScreenUpdating = False                                                          ' Turn ScreenUpdating off
'
    Dim ArrayRow                        As Long
    Dim CSV_ColumnMinus1                As Long, CSV_FileArrayColumn        As Long, CSV_FileArrayRow   As Long, CSV_FileRow    As Long
    Dim jLeft                           As Long, jCntMid                    As Long, jRight             As Long
    Dim DatesArrayRow                   As Long
    Dim FreeFileNumber                  As Long
    Dim OutputArrayColumn               As Long, OutputArrayRow             As Long
    Dim RowNumber                       As Long
    Dim BranchName                      As String, ChequeNumber             As String
    Dim All_CSV_RowsFromCSV_FileArray   As Variant, CSV_FileRowColumnsArray As Variant, Partitioned_CSV_FileArray               As Variant
    Dim CombinedAndReversedArray        As Variant
    Dim DatesArray                      As Variant, HeaderArray             As Variant, OutputArray()                           As Variant
    Dim destSht                         As Worksheet
'
    Set destSht = ThisWorkbook.Worksheets("JohnnyL")                                            ' <--- Set this to the desired sheet name
'
    FreeFileNumber = FreeFile                                                                   ' Get an unused file number
    Open CSV_FileToOpen For Input As #FreeFileNumber
'
    If Err.Number <> 0 Then                                                                     ' If error occurred then ...
        MsgBox "File open error #" & Err.Number & "!", vbCritical, "Error!"                     '   Display error #
        Exit Sub                                                                                '   Exit sub
    End If
'
    All_CSV_RowsFromCSV_FileArray = Split(Input(LOF(FreeFileNumber), #FreeFileNumber), vbCrLf)  ' Load all Rows in csv file to All_CSV_RowsFromCSV_FileArray
    Close #FreeFileNumber                                                                       ' Close the csv file
'
    RowNumber = 0                                                                               ' Initialize RowNumber
'
    ReDim Partitioned_CSV_FileArray(1 To UBound(All_CSV_RowsFromCSV_FileArray), 1 To 100)       ' Set rows/columns for Partitioned_CSV_FileArray
'
    For CSV_FileRow = LBound(All_CSV_RowsFromCSV_FileArray) To UBound(All_CSV_RowsFromCSV_FileArray)    ' Loop through all rows of CSV file
        If All_CSV_RowsFromCSV_FileArray(CSV_FileRow) <> vbNullString Then                              '   If CSV row is not blank then ...
            CSV_FileRowColumnsArray = Split(All_CSV_RowsFromCSV_FileArray(CSV_FileRow), ",")            '       Load contents of row to CSV_FileRowColumnsArray
'
            RowNumber = RowNumber + 1                                                                   '       Increment RowNumber
'
            For CSV_ColumnMinus1 = LBound(CSV_FileRowColumnsArray) To UBound(CSV_FileRowColumnsArray)   '       Loop through csv columns
                Partitioned_CSV_FileArray(RowNumber, CSV_ColumnMinus1 + 1) = _
                        CSV_FileRowColumnsArray(CSV_ColumnMinus1)                                       '           Save values to Partitioned_CSV_FileArray
            Next                                                                                        '       Loop back
        End If
    Next                                                                                                ' Loop back
'
Sheets("Sheet2").Range("A1").Resize(UBound(Partitioned_CSV_FileArray, 1), UBound(Partitioned_CSV_FileArray, 2)) = Partitioned_CSV_FileArray
'
'   All values from the CSV File are now loaded into Partitioned_CSV_FileArray
'
'-----------------------------------------------------------------------------------------------------
'
'   Intercept CSV file dates before excel gets it's hands on them
'
    ReDim DatesArray(1 To UBound(Partitioned_CSV_FileArray, 1))                                 ' Set # of rows for DatesArray
'
    DatesArrayRow = 0                                                                           ' Initialize DatesArrayRow
'
    For CSV_FileArrayRow = 1 To UBound(Partitioned_CSV_FileArray, 1)                            ' Loop through Partitioned_CSV_FileArray rows
        If IsNumeric(Right(Partitioned_CSV_FileArray(CSV_FileArrayRow, 1), 6)) Then             '   If transaction # found then ...
            DatesArrayRow = DatesArrayRow + 1                                                   '       Increment DatesArrayRow
'
            For CSV_FileArrayColumn = 2 To UBound(Partitioned_CSV_FileArray, 2)                 '       Loop through csv file columns starting from 2nd column
                If Partitioned_CSV_FileArray(CSV_FileArrayRow, CSV_FileArrayColumn) <> "" Then  '           If a value is found then ...
                    DatesArray(DatesArrayRow) = Partitioned_CSV_FileArray(CSV_FileArrayRow, CSV_FileArrayColumn)    '            Save the date into DatesArray
                    Exit For                                                                                        '            Exit loop
                End If
            Next                                                                                                    '       Loop back
        End If
    Next                                                                                                            ' Loop back
'
'   Dates needed are now safely stored into DatesArray
'
'-------------------------------------------------------------------------------------------------
'
    Workbooks.Open Filename:=CSV_FileToOpen                                                     ' Open the CSV file into Excel
'
    Partitioned_CSV_FileArray = ActiveWorkbook.ActiveSheet.UsedRange.Value                      ' Save the values from CSV file into Partitioned_CSV_FileArray
'
    ReDim OutputArray(1 To UBound(Partitioned_CSV_FileArray, 1), 1 To 5)                        ' Set rows/columns for OutputArray
'
    OutputArrayRow = 1                                                                          ' Initialize OutputArrayRow
'
    For CSV_FileArrayRow = 1 To UBound(Partitioned_CSV_FileArray, 1)                            ' Loop through rows of Partitioned_CSV_FileArray
        If IsNumeric(Right(Partitioned_CSV_FileArray(CSV_FileArrayRow, 1), 6)) Then             '   If transaction # found then ...
            OutputArrayColumn = 0                                                               '       Initialize OutputArrayColumn
'
            For CSV_FileArrayColumn = 2 To UBound(Partitioned_CSV_FileArray, 2)                 '       Loop through columns of Partitioned_CSV_FileArray
                If Partitioned_CSV_FileArray(CSV_FileArrayRow, CSV_FileArrayColumn) <> "" Then  '           If value found then ...
                    OutputArrayColumn = OutputArrayColumn + 1                                   '               Increment OutputArrayColumn
'
                    If OutputArrayColumn = 2 Then                                               '               If 'Description' is found then ...
                        OutputArray(OutputArrayRow, OutputArrayColumn - 1) = _
                                Partitioned_CSV_FileArray(CSV_FileArrayRow, CSV_FileArrayColumn)    '                   Save it into OutputArray Column 1
                    End If
'
                    If OutputArrayColumn = 3 Then                                               '               If 4th value in row found then ...
                        If Partitioned_CSV_FileArray(CSV_FileArrayRow, CSV_FileArrayColumn) <> "-" Then '                       If value found <> '-' then ...
                            BranchName = Partitioned_CSV_FileArray(CSV_FileArrayRow, CSV_FileArrayColumn)   '                       save it into BranchName
                        End If
'
                        jLeft = CSV_FileArrayColumn + 1                                         '                   Set col # to next ie. col 29->30
                        Exit For                                                                '                   Exit column loop
                    End If
                End If
            Next                                                                                '       Loop back
             
            For CSV_FileArrayColumn = UBound(Partitioned_CSV_FileArray, 2) To jLeft Step -1     '       Loop backwards from last column of csv to 4th value found column + 1
                If Partitioned_CSV_FileArray(CSV_FileArrayRow, CSV_FileArrayColumn) <> "" Then  '           If a value is found then ...
                    OutputArray(OutputArrayRow, 5) = _
                            Left(Partitioned_CSV_FileArray(CSV_FileArrayRow, CSV_FileArrayColumn), _
                            Len(Partitioned_CSV_FileArray(CSV_FileArrayRow, CSV_FileArrayColumn)) - 4)  '               Save the value to Column 5 of OutputArray
                    OutputArray(OutputArrayRow, 4) = _
                            Partitioned_CSV_FileArray(CSV_FileArrayRow, CSV_FileArrayColumn - 1)    '           Save Cr amount to column 4 of OutputArray
                    OutputArray(OutputArrayRow, 3) = _
                            Partitioned_CSV_FileArray(CSV_FileArrayRow, CSV_FileArrayColumn - 3)    '           Save Dr amount to column 3 of OutputArray
'
                    If OutputArray(OutputArrayRow, 3) <> vbNullString Then _
                            OutputArray(OutputArrayRow, 2) = "Payment"                          ' If OutputArray column 3 is not blank then set "Payment"
                    If OutputArray(OutputArrayRow, 4) <> vbNullString Then _
                            OutputArray(OutputArrayRow, 2) = "Receipt"                          ' If OutputArray column 4 is not blank then set "Receipt"
'
                    jRight = CSV_FileArrayColumn - 3 - 1
                    Exit For                                                                    ' Exit loop
                End If
            Next
            
            For jCntMid = jLeft To jRight                                                       ' Loop through columns for a cheque # value between the
'                                                                                               '       Branch Name column and the DR/CR amount column
                 If Partitioned_CSV_FileArray(CSV_FileArrayRow, jCntMid) <> "" Then             '   If cheque # found then ...
                    ChequeNumber = Partitioned_CSV_FileArray(CSV_FileArrayRow, jCntMid)         '       Save the cheque # into ChequeNumber
                    Exit For                                                                    '       Exit loop
                 End If
            Next
'
            OutputArray(OutputArrayRow, 1) = OutputArray(OutputArrayRow, 1) & _
                    " Txn No." & Partitioned_CSV_FileArray(CSV_FileArrayRow, 1)                 ' Append Transaction # to column 1 of OutputArray
'
            If BranchName <> "" Then OutputArray(OutputArrayRow, 1) = _
                    OutputArray(OutputArrayRow, 1) & " Branch Name - " & BranchName             ' If a BranchName was found then Append BranchName to column 1 of OutputArray
'
            If ChequeNumber <> "" Then OutputArray(OutputArrayRow, 1) = _
                    OutputArray(OutputArrayRow, 1) & " Cheque No. " & ChequeNumber              ' If a ChequeNumber was found then Append ChequeNumber to column 1 of OutputArray
'
            OutputArray(OutputArrayRow, 1) = Replace(OutputArray(OutputArrayRow, 1), Chr(10), "")   ' delete line feeds in description
'
            OutputArrayRow = OutputArrayRow + 1                                                 ' Increment OutputArrayRow
            BranchName = ""                                                                     ' Clear BranchName
            ChequeNumber = ""                                                                   ' Clear ChequeNumber
        End If
    Next
'
'-------------------------------------------------------------------------------------------------
'
'   Combine and Reverse order of 2 arrays ... DatesArray & OutputArray into CombinedAndReversedArray
'
    ReDim CombinedAndReversedArray(1 To UBound(DatesArray), 1 To 6)
'
    RowNumber = 0
'
    For ArrayRow = UBound(DatesArray) To 1 Step -1
        If DatesArray(ArrayRow) <> vbNullString Then
            RowNumber = RowNumber + 1
'
            CombinedAndReversedArray(RowNumber, 1) = DatesArray(ArrayRow)
            CombinedAndReversedArray(RowNumber, 2) = OutputArray(ArrayRow, 1)
            CombinedAndReversedArray(RowNumber, 3) = OutputArray(ArrayRow, 2)
            CombinedAndReversedArray(RowNumber, 4) = OutputArray(ArrayRow, 3)
            CombinedAndReversedArray(RowNumber, 5) = OutputArray(ArrayRow, 4)
            CombinedAndReversedArray(RowNumber, 6) = OutputArray(ArrayRow, 5)
        End If
    Next
'
'-------------------------------------------------------------------------------------------------
'
    HeaderArray = Array("Txn Date", "Description", "Voucher Type", "Dr Amount", "Cr Amount", "Balance") ' Establish HeaderArray
'
    With destSht
        .UsedRange.Clear                                                                        ' Clear the destination sheet
        .Columns("A:A").NumberFormat = "@"                                                      ' Set column A to text so Excel don't mess up the dates
        .Range("A1").Resize(, UBound(HeaderArray) + 1) = HeaderArray                            ' Write the Header columns to destination sheet
        .Range("A1").Resize(, UBound(HeaderArray) + 1).Font.Bold = True                         ' Set the Header to Bold type font
'
        .Range("A2").Resize(UBound(CombinedAndReversedArray, 1), _
                UBound(CombinedAndReversedArray, 2)).Value = CombinedAndReversedArray           ' Display results to destination sheet
'
''        .Range("F:H").EntireColumn.NumberFormat = "_(* #,##0.00_);[Red]_(* (#,##0.00);;_(@_)"   ' Amount fields
        .Columns("D:F").NumberFormat = "0.00"                                                          ' Format Columns D:E to two decimal places
        .UsedRange.Columns.AutoFit                                                              ' Resize the columns to fit the data
    End With
'
    Application.DisplayAlerts = False
    ActiveWorkbook.Close SaveChanges:=False                                                     ' Close the csv file that was opened earlier
    Application.DisplayAlerts = True
'
    Application.ScreenUpdating = True                                                           ' Turn ScreenUpdating back on
'
    Debug.Print "Time to complete = " & Timer - startTime & " seconds."                         ' about .5 seconds
End Sub

CopyOfConver CSV to XLSX_VBA_Pasworded.xlsm
ABCDEFG
1Txn DateDescriptionVoucher TypeDr AmountCr AmountBalance
201-01-2022IMPS-IN/200116132954/9773902678/ramjatan Txn No.S69726393Receipt1.002,01,37,347.95
303-01-2022IMPS-IN/200312912441/9880980396/B AKBAR Txn No.S97073495Receipt3350.002,01,33,997.95
403-01-2022NEFT_IN:CMS2323377990/0034/ NEFT SAIPRIYADARSHINI PNB BANK Txn No.S6213774Receipt7392.002,01,26,605.95
503-01-2022NEFT_IN:CMS2323377378/0034/ NEFT SAIPRIYADARSHINI PNB BANK Txn No.S6260314Receipt8936.002,01,17,669.95
604-01-2022NEFT_IN:CMS2324991576/0030/ NEFT SAIPRIYADARSHINI Txn No.S21160790Receipt2548.002,01,15,121.95
706-01-2022NEFT_IN:CMS2328856703/0029/ NEFT SAIPRIYADARSHINI PNB Txn No.S57819510Receipt8639.002,01,06,482.95
807-01-2022BY INST 9313 : CTO386-1DAY LAT Txn No.S75158828Receipt34045.002,00,72,437.95
907-01-2022REJECT:9313:FUNDSINSUFFICIENT Txn No.S78275908Payment34045.002,01,06,482.95
1011-01-2022BY INST 703548 : CTO386-1DAY LAT Txn No.S40009419Receipt5592.002,01,00,890.95
1111-01-2022IMPS-IN/201120510599/9894388772/Mr PRAK Txn No.S50001720Receipt9750.002,00,91,140.95
1212-01-2022NEFT_IN:N012221791060836/0025/ PUNJABHANDLOOM WORLD Txn No.S58034732Receipt14730.002,00,76,410.95
1312-01-2022IMPS-IN/201212156665/9449494958/CHANDRAS Txn No.S58438813Receipt2200.002,00,74,210.95
1412-01-2022NEFT_IN:CMS2339567883/0027/ NEFT SAIPRIYADARSHINI PNB BANK Txn No.S59433971Receipt1960.002,00,72,250.95
1512-01-2022IMPS-IN/201215947955/9880980396/B AKBAR Txn No.S60925182Receipt950.002,00,71,300.95
1613-01-2022NEFT_IN:CMS2341384059/0028/ NEFT SAIPRIYADARSHINI PNB BANK Txn No.S78097253Receipt398.002,00,70,902.95
1714-01-2022UPI/201431318251/P2A/8600772299/ifsc.npciSALIM HUS Txn No.S92950893Receipt6586.002,00,64,316.95
1814-01-2022NEFT_IN:BKIDN22014196827/0033/ THE VILLAGEHYPER MARKET Txn No.S99388905Receipt53715.002,00,10,601.95
1917-01-2022NEFT_IN:CMS2347712228/0027/ NEFT SAIPRIYADARSHINI PNB Txn No.S38602282Receipt2533.002,00,08,068.95
2017-01-2022BY INST 2225 : CTO386-1DAY LAT Txn No.S38761291Receipt35200.001,99,72,868.95
2117-01-2022BY INST 6213 : CTO386-1DAY LAT Txn No.S38761291Receipt21350.001,99,51,518.95
2217-01-2022From:0040005500052250:SAIPRIYADARSHINI Txn No.S43317822Receipt13000.001,99,38,518.95
2317-01-2022To:0040000400022033:THIRDPARTY Txn No.S43356097Payment25000.001,99,63,518.95
2417-01-2022NEFT_IN:N017221796938992/0034/ SUPER TASTYBAKERY FOODS INDIA PVT Txn No.S44193305Receipt8640.001,99,54,878.95
2517-01-2022From:0040005500052250:SAIPRIYADARSHINI Txn No.S47688222Receipt13000.001,99,41,878.95
2617-01-2022To:0040000400022051:THIRDPARTY Txn No.S47704379Payment6000.001,99,47,878.95
2717-01-2022To:0040000400037202:SANJEEV KUMAR Txn No.S47719590Payment4848.001,99,52,726.95
2817-01-2022CDS/CRTR/127710B1127710/2872/17-01-2022 Txn No.S48313360Receipt38500.001,99,14,226.95
2917-01-2022CDS/CRTR/127710B1127710/2876/17-01-2022 Txn No.S48353282Receipt45500.001,98,68,726.95
3017-01-2022NEFT_OUT:PUNBH22017574102/Shivanaa Homes/ICIC0000052/005205009149 Txn No.S48928367Payment28650.001,98,97,376.95
3117-01-2022NEFT_OUT:PUNBH22017574130/Shivanaa Homes/ICIC0000052/005205009149 Txn No.S48927778Payment22579.001,99,19,955.95
3217-01-2022NEFT_OUT:PUNBH22017574163/VrindavanCreatio/HDFC0000076/50200062038863 Txn No.S48935235Payment32534.001,99,52,489.95
3317-01-2022From:0040005500052250:SAIPRIYADARSHINI Txn No.S48934634Receipt28000.001,99,24,489.95
3417-01-2022NEFT_OUT:PUNBH22017574241/ShyamInnovations/KKBK0001435/0413090674 Txn No.S48942860Payment13256.001,99,37,745.95
3517-01-2022To:0335002100027863:MAYUR TEX Txn No.S48942946Payment25000.001,99,62,745.95
3617-01-2022NEFT_OUT:PUNBH22017574509/Sumit Kumar/HDFC0000076/50100453150353 Txn No.S48970678Payment1500.001,99,64,245.95
3718-01-2022D13152402CRTO004000NG00026214 Txn No.S49438594Payment29250.001,99,93,495.95
3818-01-2022SHORTFAL REC- Inw RtrnChrgs:115845 Txn No.S49516917Payment236.001,99,93,731.95
3918-01-2022SHORTFAL REC- Inw RtrnChrgs:115844 Txn No.S49516938Payment236.001,99,93,967.95
4018-01-2022SHORTFAL REC- Inw RtrnChrgs:115843 Txn No.S49516956Payment236.001,99,94,203.95
4118-01-2022SHORTFAL REC- Inw RtrnChrgs:115846 Txn No.S49516971Payment236.001,99,94,439.95
4218-01-2022SHORTFAL REC- Inw RtrnChrgs:115847 Txn No.S49516987Payment236.001,99,94,675.95
4318-01-2022SHORTFAL REC- O/W RtrnChrgs: 9313 Txn No.S49517001Payment118.001,99,94,793.95
4418-01-2022SHORTFAL REC- LedgerFolio Charges from 01-10-2021 Txn No.S49517019Payment1144.601,99,95,938.55
4518-01-2022SHORTFAL REC- Inw RtrnChrgs:321801 Txn No.S49517037Payment236.001,99,96,174.55
4618-01-2022SHORTFAL REC- Inw RtrnChrgs:404446 Txn No.S49517054Payment236.001,99,96,410.55
4718-01-2022SHORTFAL REC- Inw RtrnChrgs:404403 Txn No.S49517065Payment236.001,99,96,646.55
4818-01-2022SHORTFAL REC- Inw RtrnChrgs:404479 Txn No.S49517085Payment236.001,99,96,882.55
4918-01-2022SHORTFAL REC- Inw RtrnChrgs:404447 Txn No.S49517098Payment236.001,99,97,118.55
5018-01-2022SHORTFAL REC- Inw RtrnChrgs:297834 Txn No.S49517124Payment236.001,99,97,354.55
5118-01-2022SHORTFAL REC- Inw RtrnChrgs:404480 Txn No.S49517138Payment236.001,99,97,590.55
5218-01-2022SHORTFAL REC- Inw RtrnChrgs:115904 Txn No.S49517150Payment236.001,99,97,826.55
5318-01-2022IW CHQ : 297833 REJ Txn No.S52754065Payment236.001,99,98,062.55
5418-01-2022BY INST 961315 : CTO386-1DAY LAT Txn No.S55077092Receipt36080.001,99,61,982.55
5518-01-2022From:0040005500052250:SAIPRIYADARSHINI Txn No.S60472346Receipt98000.001,98,63,982.55
5618-01-2022To:0335002100027863:MAYUR TEX Txn No.S60506843Payment100000.001,99,63,982.55
5718-01-2022CDS/CRTR/120200B1120200/1904/18-01-2022 Txn No.S64677210Receipt15000.001,99,48,982.55
5818-01-2022From:0040005500052250:SAIPRIYADARSHINI Txn No.S65303152Receipt9500.001,99,39,482.55
5918-01-2022NEFT_OUT:PUNBH22018894955/IndigoIndustries/HDFC0000251/50200005260507 Txn No.S65322005Payment16600.001,99,56,082.55
6018-01-2022From:0040005500052250:SAIPRIYADARSHINI Txn No.S65322115Receipt500.001,99,55,582.55
6118-01-2022NEFT_OUT:PUNBH22018895008/P Rosy/UBIN0902802/520101073176733 Txn No.S65322173Payment8000.001,99,63,582.55
6219-01-2022SHORTFAL REC- Inw RtrnChrgs:297833 Txn No.S65885600Payment236.001,99,63,818.55
6319-01-2022SHORTFAL REC- Inw RtrnChrgs:404458 Txn No.S65886483Payment236.001,99,64,054.55
6419-01-2022SHORTFAL REC- Inw RtrnChrgs:404448 Txn No.S65886548Payment236.001,99,64,290.55
6519-01-2022NEFT_IN:CMS2351761982/0026/ NEFT SAIPRIYADARSHINI PNB Txn No.S71440598Receipt15081.001,99,49,209.55
6619-01-2022To:0040005500052250:SAIPRIYADARSHINI Txn No.S71468006Payment10000.001,99,59,209.55
6719-01-2022NEFT_IN:CMS2351784603/0026/ NEFT SAIPRIYADARSHINI PNB BANK Txn No.S71503430Receipt7881.001,99,51,328.55
6819-01-2022To:0040000400022033:THIRDPARTY Txn No.S71848111Payment3067.001,99,54,395.55
6919-01-2022To:0040000400022033:THIRDPARTY Txn No.S71861881Payment2000.001,99,56,395.55
7019-01-2022BAJAJ ALLIANZ GENERALINSURANCE CO LTD Txn No.M654161 Cheque No. 297837Payment39294.001,99,95,689.55
7120-01-2022NEFT_IN:N020221800403427/0022/ THE INDOORCOLLECTION Txn No.S85418472Receipt3000.001,99,92,689.55
7220-01-2022BY INST 4809 : CTO386-1DAY LAT Txn No.S89723704Receipt29664.001,99,63,025.55
7320-01-2022To:0040005500052250:SAIPRIYADARSHINI Txn No.S98751533Payment1100.001,99,64,125.55
7421-01-2022DHC LOGISTICS PVT LTD Txn No.S2141266 Cheque No. 297835Payment10050.001,99,74,175.55
7521-01-2022TO TR BAJAJ ALLIANZGENERAL INSURANCE -215410 Txn No.M103376 Branch Name - SARJAPUR ROAD,BANGALORE Cheque No. 297839Payment21240.001,99,95,415.55
7621-01-2022NEFT_IN:CMS2355644388/0028/ NEFT SAIPRIYADARSHINI PNB Txn No.S5999448Receipt637.001,99,94,778.55
7721-01-2022BY INST 5020 : CTO386-1DAY LAT Txn No.S6117680Receipt18000.001,99,76,778.55
7821-01-2022BY INST 28600 : CTO386-1DAY LAT Txn No.S6117680Receipt13860.001,99,62,918.55
7921-01-2022REJECT:28600:FUNDSINSUFFICIENT Txn No.S8055280Payment13860.001,99,76,778.55
8021-01-2022OW CHQ : 28600 REJ Txn No.S8826093Payment177.001,99,76,955.55
8125-01-2022SHORTFAL REC- Inw RtrnChrgs:404449 Txn No.S59088095Payment236.001,99,77,191.55
8225-01-2022BY INST 13778 : CTO386-1DAY LAT Txn No.S64215257Receipt200000.001,97,77,191.55
8325-01-2022BY INST 2310 : CTO386-1DAY LAT Txn No.S64215257Receipt2730.001,97,74,461.55
8425-01-2022BY INST 6228 : CTO386-1DAY LAT Txn No.S64215257Receipt8692.001,97,65,769.55
8525-01-2022BY INST 8 : CTO386-1 DAYLAT Txn No.S64215257Receipt30000.001,97,35,769.55
8625-01-2022IW CHQ : 404450 REJ Txn No.S64576849Payment236.001,97,36,005.55
8725-01-2022JAI SHRI KRISHANHANDLOO Txn No.S64604285 Cheque No. 404458Payment15000.001,97,51,005.55
8825-01-2022NEFT_OUT:PUNBH22025654362/MK Fab and FurnP/CNRB0000422/0422201002863 Txn No.S71145240Payment8463.001,97,59,468.55
8925-01-2022NEFT_OUT:PUNBH22025664529/Sumit Kumar/HDFC0000076/50100453150353 Txn No.S72269403Payment25000.001,97,84,468.55
9025-01-2022NEFT_OUT:PUNBH22025664748/Guru Kirpa Texco/HDFC0001419/50200015879735 Txn No.S72305292Payment16349.001,98,00,817.55
9125-01-2022NEFT_OUT:PUNBH22025665078/Dhruv Industries/HDFC0002707/50200008690824 Txn No.S72354267Payment22600.001,98,23,417.55
9225-01-2022To:0040000400022033:THIRDPARTY Txn No.S73539862Payment50000.001,98,73,417.55
9325-01-2022To:0335002100027863:MAYUR TEX Txn No.S73759969Payment50000.001,99,23,417.55
9425-01-2022NEFT_OUT:PUNBH22025679255/Reshma Firdose/IOBA0002838/283801000002794 Txn No.S75096820Payment3000.001,99,26,417.55
9525-01-2022NEFT_OUT:PUNBH22025679273/P Rosy/UBIN0902802/520101073176733 Txn No.S75096946Payment6638.001,99,33,055.55
9625-01-2022NEFT_OUT:PUNBH22025679297/Haji Hamza Cloth/BARB0PILAKH/37090200000553 Txn No.S75097098Payment10000.001,99,43,055.55
9725-01-2022NEFT_OUT:PUNBH22025679303/Das furnishing/FDRL0001546/15465500000540 Txn No.S75097199Payment15000.001,99,58,055.55
9825-01-2022From:0040005500052250:SAIPRIYADARSHINI Txn No.S75105293Receipt15000.001,99,43,055.55
9926-01-2022NEFT:PUNBH22026679338/5074103288/Vrindavan Creatio Txn No.S75105463Payment11145.001,99,54,200.55
10026-01-2022To:0040000400017800:THIRDPARTY Txn No.S75105575Payment4000.001,99,58,200.55
10126-01-2022To:0040000400022033:THIRDPARTY Txn No.S75113303Payment5000.001,99,63,200.55
10226-01-2022From:0040000400022033:RAJ KUMAR Txn No.S83672401Receipt15000.001,99,48,200.55
10326-01-2022From:0040005500052250:SAIPRIYADARSHINI Txn No.S83734339Receipt5000.001,99,43,200.55
10426-01-2022NEFT_OUT:PUNBH22026706732/K B TEXFAB PVTLT/UBIN0539287/392801010050845 Txn No.S83749201Payment20000.001,99,63,200.55
10527-01-2022DHC LOGISTICS PVT LTD Txn No.S92312801 Cheque No. 297836Payment4229.001,99,67,429.55
10627-01-2022NEFT_IN:000467863512/0028/ DEVI FURNISHING Txn No.S95873762Receipt3498.001,99,63,931.55
10727-01-2022DEEPIKLA TEXTILES-097710 Txn No.M698300 Branch Name - AMRITSAR-RANJITAVENUE Cheque No. 115915Payment23940.001,99,87,871.55
10828-01-2022DHC LOGISTICS PVT LTD Txn No.S8610328 Cheque No. 297841Payment2920.001,99,90,791.55
10928-01-2022IW CHQ : 115925 REJ Txn No.S9240214Payment236.001,99,91,027.55
11028-01-2022BY INST 700 : CTO386-1DAY LAT Txn No.S11116242Receipt7945.001,99,83,082.55
11128-01-2022BY INST 9 : CTO386-1 DAYLAT Txn No.S11116242Receipt41363.001,99,41,719.55
11228-01-2022NEFT_IN:CMS2366034725/0028/ NEFT SAIPRIYADARSHINI PNB Txn No.S12552876Receipt10079.001,99,31,640.55
11328-01-2022NEFT_OUT:PUNBH22028406780/VRL LogisticsLtd/SVCB0000151/115104180000724 Txn No.S17099015Payment890.001,99,32,530.55
11428-01-2022NEFT_OUT:PUNBH22028407183/Balwant Sharma/CSBK0000009/000901014064190001 Txn No.S17122507Payment5000.001,99,37,530.55
11528-01-2022IMPS-IN/202820202905/9008333000/HIT KARA Txn No.S19855554Receipt30000.001,99,07,530.55
11628-01-2022From:0040005500052250:SAIPRIYADARSHINI Txn No.S20048431Receipt14000.001,98,93,530.55
11728-01-2022To:0040000400022033:THIRDPARTY Txn No.S20078016Payment65000.001,99,58,530.55
11828-01-2022IMPS-IN/202823229653/9008333000/HIT KARA Txn No.S21646457Receipt50000.001,99,08,530.55
11928-01-2022NEFT_OUT:PUNBH22028444122/Ambujammal GadiC/UBIN0533114/004600101009347 Txn No.S21649604Payment25000.001,99,33,530.55
12028-01-2022To:0335002100027863:MAYUR TEX Txn No.S21652886Payment25000.001,99,58,530.55
12129-01-2022NEFT_IN:0129i27266126981/0020/ HARYANAHANDLOOM CENTRE Txn No.S24127932Receipt14157.001,99,44,373.55
12229-01-2022SAAKAN Txn No.S25594848 Cheque No. 404451Payment35805.001,99,80,178.55
12329-01-2022NRTGS/SIBLR52022012900217708/RONAK KOTHARI Txn No.S27350940Receipt1000000.001,89,80,178.55
12429-01-2022To:0040005500052250:SAIPRIYADARSHINI Txn No.S28330610Payment160000.001,91,40,178.55
12529-01-2022NEFT_IN:CMS2368136936/0028/ NEFT SAIPRIYADARSHINI PNB Txn No.S28662620Receipt11226.001,91,28,952.55
12629-01-2022NEFT_OUT:PUNBH22029571773/SnehaEnterprises/KARB0000715/7157000600342001 Txn No.S29261280Payment20000.001,91,48,952.55
12729-01-2022NEFT_OUT:PUNBH22029669930/Bajrang Textile/SBIN0016970/36189935432 Txn No.S31710586Payment27270.001,91,76,222.55
12829-01-2022NEFT_OUT:PUNBH22029724591/Deutsche Bank/DEUT0784PBC/350032923110028 Txn No.S35546415Payment53000.001,92,29,222.55
12929-01-2022To:3616002100032433:SHRIRAVI TEXTILES Txn No.S36568018Payment20000.001,92,49,222.55
13030-01-2022NEFT_OUT:PUNBH22030738112/Ronak Kothari/SIBL0000008/0008053000019810 Txn No.S38918271Payment99000.001,93,48,222.55
13130-01-2022NEFT_OUT:PUNBH22030738146/ShyamInnovations/KKBK0001435/0413090674 Txn No.S38927953Payment23137.001,93,71,359.55
13230-01-2022NEFT_OUT:PUNBH22030738195/VrindavanCreatio/HDFC0000076/50200062038863 Txn No.S38943508Payment37128.001,94,08,487.55
13330-01-2022NEFT_OUT:PUNBH22030738240/Das Furnishing/FDRL0001546/15460200009412 Txn No.S38957395Payment41234.001,94,49,721.55
13430-01-2022NEFT_OUT:PUNBH22030738265/DR DRAPES/KKBK0000961/8011205001 Txn No.S38966951Payment34791.001,94,84,512.55
13530-01-2022NEFT_OUT:PUNBH22030738290/K B TEXFAB PVTLT/UBIN0539287/392801010050845 Txn No.S38978925Payment47204.001,95,31,716.55
13630-01-2022NEFT_OUT:PUNBH22030738330/Dhruv Industries/HDFC0002707/50200008690824 Txn No.S39001769Payment47595.001,95,79,311.55
13730-01-2022To:1456002100112933:TANISHQ FAB Txn No.S39064636Payment10790.001,95,90,101.55
13830-01-2022NEFT_OUT:PUNBH22030738693/SM EXPRESSLOGIST/HDFC0001471/14712560000622 Txn No.S39147626Payment17904.001,96,08,005.55
13930-01-2022IMPS-IN/203016277937/9743495430/NAGARAJA Txn No.S45137001Receipt2727.001,96,05,278.55
14030-01-2022NEFT_OUT:PUNBH22030759225/Rubmini Fabrics/UBIN0904597/560101000102178 Txn No.S48299812Payment25000.001,96,30,278.55
14130-01-2022To:4365002100104033:S BENTERPRISES Txn No.S48332081Payment18000.001,96,48,278.55
14230-01-2022NEFT_OUT:PUNBH22030759359/Jai BharatPrints/HDFC0000926/50200017200515 Txn No.S48373944Payment50000.001,96,98,278.55
14330-01-2022NEFT_OUT:PUNBH22030759414/Anjali Textiles/DCBL0000191/19122900003933 Txn No.S48390952Payment25000.001,97,23,278.55
14430-01-2022NEFT_OUT:PUNBH22030759834/SG Textiles/UBIN0540242/402405010000124 Txn No.S48639497Payment20000.001,97,43,278.55
14530-01-2022NEFT_OUT:PUNBH22030759904/Kapish Fabrics/BARB0PANCHK/03340500014101 Txn No.S48681378Payment20000.001,97,63,278.55
14630-01-2022NEFT_OUT:PUNBH22030759942/Surya Prints/HDFC0000926/09262320000091 Txn No.S48704812Payment26240.001,97,89,518.55
14730-01-2022NEFT_OUT:PUNBH22030759984/VrindavanCreatio/HDFC0000076/50200062038863 Txn No.S48727027Payment2600.001,97,92,118.55
14830-01-2022NEFT_OUT:PUNBH22030760012/VrindavanCreatio/HDFC0000076/50200062038863 Txn No.S48741255Payment2890.001,97,95,008.55
14930-01-2022To:0040002100096132:SAIPRIYADARSHINI Txn No.S49135071Payment15000.001,98,10,008.55
15030-01-2022NEFT_OUT:PUNBH22030760788/VrindavanCreatio/HDFC0000076/50200062038863 Txn No.S49192306Payment1400.001,98,11,408.55
15130-01-2022NEFT_OUT:PUNBH22030760842/VrindavanCreatio/HDFC0000076/50200062038863 Txn No.S49210304Payment1850.001,98,13,258.55
15230-01-2022NEFT_OUT:PUNBH22030760882/VrindavanCreatio/HDFC0000076/50200062038863 Txn No.S49228683Payment1290.001,98,14,548.55
15330-01-2022To:0040000400022033:THIRDPARTY Txn No.S49606885Payment15000.001,98,29,548.55
15430-01-2022To:0040000400017794:THIRDPARTY Txn No.S49617550Payment2500.001,98,32,048.55
15531-01-2022NEFT_OUT:PUNBH22031769695/Shivanaa Homes/ICIC0000052/005205009149 Txn No.S52731762Payment35000.001,98,67,048.55
15631-01-2022NEFT_OUT:PUNBH22031769728/Sumit Kumar/KKBK0000631/CSG152957522 Txn No.S52731878Payment75056.001,99,42,104.55
15731-01-2022From:0040005500052250:SAIPRIYADARSHINI Txn No.S52731921Receipt100000.001,98,42,104.55
15831-01-2022NEFT_OUT:PUNBH22031769901/Indusind BankLtd/INDB0000007/00073564614017 Txn No.S52749740Payment25993.001,98,68,097.55
15931-01-2022NEFT_OUT:PUNBH22031770116/Deutsche Bank/DEUT0784PBC/350032923110028 Txn No.S52791765Payment45000.001,99,13,097.55
16031-01-2022To:0040000400022033:THIRDPARTY Txn No.S52803831Payment6000.001,99,19,097.55
16131-01-2022To:0573008700001276:MAGMA FINCORP LIMITED Txn No.S52811577Payment10755.001,99,29,852.55
16231-01-2022To:0335002100027863:MAYUR TEX Txn No.S52818356Payment30000.001,99,59,852.55
16331-01-2022IMPS-IN/203108352889/9008333000/SUMIT KU Txn No.S52914499Receipt100000.001,98,59,852.55
16431-01-2022NEFT_OUT:PUNBH22031771000/JagdambaFurnishi/HDFC0001416/50200011850889 Txn No.S52922892Payment46820.001,99,06,672.55
16531-01-2022NEFT_OUT:PUNBH22031771036/VrindavanCreatio/HDFC0000076/50200062038863 Txn No.S52931538Payment25000.001,99,31,672.55
16631-01-2022NEFT_OUT:PUNBH22031771064/AEBC376932184342/SCBL0036020/52205899087 Txn No.S52937494Payment20000.001,99,51,672.55
16731-01-2022To:0040000400037211:ABALU Txn No.S52946961Payment12000.001,99,63,672.55
16831-01-2022SRD LOGISTICS PRIVATELIM Txn No.S55190966 Cheque No. 297840Payment3063.001,99,66,735.55
16931-01-2022N PRAKASH Txn No.S55291435 Cheque No. 115927Payment25800.001,99,92,535.55
17031-01-20220040008700003314:Int.Coll:01-01-2022 to 31-01-202 Txn No.S69538859Payment202268.002,01,94,803.55
171
JohnnyL


Results from CC Jan 2022_Fixed.csv file.
 
Upvote 0
Rich (BB code):
Sheets("Sheet2").Range("A1").Resize(UBound(Partitioned_CSV_FileArray, 1), UBound(Partitioned_CSV_FileArray, 2)) = Partitioned_CSV_FileArray
run time error.
 
Upvote 0
JohnnyL. FYI,
Your last code is working perfectly. Everything is good.
Only 2 things left to complete this with your yesterday's last code . One is the Line number column which you can insert at the end of the code before column A, then sort the data by line number largest to smallest and then finally delete the line number column. I want the dates from descending to ascending order but in the same order as in the csv sheet but reversed.
Please note that the line number should insert numbers only till the last row with date in that particular conversion. The range of last row will be different in each conversion.
Don’t tamper the balance column with Dr. & Cr. If possible, with the help of a code or formula in cell H2 remove the Dr. & Cr. And get the amount in number format with 2 decimal places and resize column H with G.
That way the sheet will have
Secondly, each time I press the convert button, I have to rename the sheet, insert a new sheet and rename it to JohnnyL. So, I a code to be added at the end of your code.
This will complete the project.
Please continue with this code.....
Rich (BB code):
Sub LoadCSV_FileTest4()
'solved by JohnnyL except for sort data by date
    Dim startTime                       As Single
    Dim CSV_FileToOpen                  As Variant
'
    CSV_FileToOpen = Application.GetOpenFilename("Text files,*.csv", , "Select file", , False)  ' Save full path of CSV file to CSV_FileToOpen
    If CSV_FileToOpen = False Then                                                              ' If user cancelled then ...
        MsgBox "No file selected - exiting"                                                     '   Display message box to user indicating the cancellation
        Exit Sub                                                                                '   Exit the sub
    End If
'
    startTime = Timer                                                                           ' Start the stopwatch
'
    Application.ScreenUpdating = False                                                          ' Turn ScreenUpdating off
'
    Dim CSV_ColumnMinus1                As Long, CSV_FileArrayColumn        As Long, CSV_FileArrayRow   As Long, CSV_FileRow    As Long
    Dim jLeft                           As Long, jCntMid                    As Long, jRight             As Long
    Dim DatesArrayRow                   As Long
    Dim FreeFileNumber                  As Long
    Dim OutputArrayColumn               As Long, OutputArrayRow             As Long
    Dim RowNumber                       As Long
    Dim BranchName                      As String, ChequeNumber             As String
    Dim All_CSV_RowsFromCSV_FileArray   As Variant, CSV_FileRowColumnsArray As Variant, Partitioned_CSV_FileArray               As Variant
    Dim DatesArray                      As Variant, HeaderArray             As Variant, OutputArray()                           As Variant
    Dim destSht                         As Worksheet
'
    Set destSht = ThisWorkbook.Worksheets("JohnnyL")                                            ' <--- Set this to the desired sheet name
'
    FreeFileNumber = FreeFile                                                                   ' Get an unused file number
    Open CSV_FileToOpen For Input As #FreeFileNumber
'
    If Err.Number <> 0 Then                                                                     ' If error occurred then ...
        MsgBox "File open error #" & Err.Number & "!", vbCritical, "Error!"                     '   Display error #
        Exit Sub                                                                                '   Exit sub
    End If
'
    All_CSV_RowsFromCSV_FileArray = Split(Input(LOF(FreeFileNumber), #FreeFileNumber), vbCrLf)  ' Load all Rows in csv file to All_CSV_RowsFromCSV_FileArray
    Close #FreeFileNumber                                                                       ' Close the csv file
'
    RowNumber = 0                                                                               ' Initialize RowNumber
'
    ReDim Partitioned_CSV_FileArray(1 To UBound(All_CSV_RowsFromCSV_FileArray), 1 To 100)       ' Set rows/columns for Partitioned_CSV_FileArray
'
    For CSV_FileRow = LBound(All_CSV_RowsFromCSV_FileArray) To UBound(All_CSV_RowsFromCSV_FileArray)    ' Loop through all rows of CSV file
        If All_CSV_RowsFromCSV_FileArray(CSV_FileRow) <> vbNullString Then                              '   If CSV row is not blank then ...
            CSV_FileRowColumnsArray = Split(All_CSV_RowsFromCSV_FileArray(CSV_FileRow), ",")            '       Load contents of row to CSV_FileRowColumnsArray
'
            RowNumber = RowNumber + 1                                                                   '       Increment RowNumber
'
            For CSV_ColumnMinus1 = LBound(CSV_FileRowColumnsArray) To UBound(CSV_FileRowColumnsArray)   '       Loop through csv columns
                Partitioned_CSV_FileArray(RowNumber, CSV_ColumnMinus1 + 1) = _
                        CSV_FileRowColumnsArray(CSV_ColumnMinus1)                                       '           Save values to Partitioned_CSV_FileArray
            Next                                                                                        '       Loop back
        End If
    Next                                                                                                ' Loop back
'
'   All values from the CSV File are now loaded into Partitioned_CSV_FileArray
'
'-----------------------------------------------------------------------------------------------------
'
'   Intercept CSV file dates before excel gets it's hands on them
'
    ReDim DatesArray(1 To UBound(Partitioned_CSV_FileArray, 1))                                 ' Set # of rows for DatesArray
'
    DatesArrayRow = 0                                                                           ' Initialize DatesArrayRow
'
    For CSV_FileArrayRow = 1 To UBound(Partitioned_CSV_FileArray, 1)                            ' Loop through Partitioned_CSV_FileArray rows
        If IsNumeric(Right(Partitioned_CSV_FileArray(CSV_FileArrayRow, 1), 6)) Then             '   If transaction # found then ...
            DatesArrayRow = DatesArrayRow + 1                                                   '       Increment DatesArrayRow
'
            For CSV_FileArrayColumn = 2 To UBound(Partitioned_CSV_FileArray, 2)                 '       Loop through csv file columns starting from 2nd column
                If Partitioned_CSV_FileArray(CSV_FileArrayRow, CSV_FileArrayColumn) <> "" Then  '           If a value is found then ...
                    DatesArray(DatesArrayRow) = Partitioned_CSV_FileArray(CSV_FileArrayRow, CSV_FileArrayColumn)    '            Save the date into DatesArray
                    Exit For                                                                                        '            Exit loop
                End If
            Next                                                                                                    '       Loop back
        End If
    Next                                                                                                            ' Loop back
'
'   Dates needed are now safely stored into DatesArray
'
'-------------------------------------------------------------------------------------------------
'
    Workbooks.Open Filename:=CSV_FileToOpen                                                     ' Open the CSV file into Excel
'
    Partitioned_CSV_FileArray = ActiveWorkbook.ActiveSheet.UsedRange.Value                      ' Save the values from CSV file into Partitioned_CSV_FileArray
'
    ReDim OutputArray(1 To UBound(Partitioned_CSV_FileArray, 1), 1 To 5)                        ' Set rows/columns for OutputArray
'
    OutputArrayRow = 1                                                                          ' Initialize OutputArrayRow
'
    For CSV_FileArrayRow = 1 To UBound(Partitioned_CSV_FileArray, 1)                            ' Loop through rows of Partitioned_CSV_FileArray
        If IsNumeric(Right(Partitioned_CSV_FileArray(CSV_FileArrayRow, 1), 6)) Then             '   If transaction # found then ...
            OutputArrayColumn = 0                                                               '       Initialize OutputArrayColumn
'
            For CSV_FileArrayColumn = 2 To UBound(Partitioned_CSV_FileArray, 2)                 '       Loop through columns of Partitioned_CSV_FileArray
                If Partitioned_CSV_FileArray(CSV_FileArrayRow, CSV_FileArrayColumn) <> "" Then  '           If value found then ...
                    OutputArrayColumn = OutputArrayColumn + 1                                   '               Increment OutputArrayColumn
'
                    If OutputArrayColumn = 2 Then                                               '               If 'Description' is found then ...
                        OutputArray(OutputArrayRow, OutputArrayColumn - 1) = _
                                Partitioned_CSV_FileArray(CSV_FileArrayRow, CSV_FileArrayColumn)    '                   Save it into OutputArray Column 1
                    End If
'
                    If OutputArrayColumn = 3 Then                                               '               If 4th value in row found then ...
                        If Partitioned_CSV_FileArray(CSV_FileArrayRow, CSV_FileArrayColumn) <> "-" Then '                       If value found <> '-' then ...
                            BranchName = Partitioned_CSV_FileArray(CSV_FileArrayRow, CSV_FileArrayColumn)   '                       save it into BranchName
                        End If
'
                        jLeft = CSV_FileArrayColumn + 1                                         '                   Set col # to next ie. col 29->30
                        Exit For                                                                '                   Exit column loop
                    End If
                End If
            Next                                                                                '       Loop back
         
            For CSV_FileArrayColumn = UBound(Partitioned_CSV_FileArray, 2) To jLeft Step -1     '       Loop backwards from last column of csv to 4th value found column + 1
                If Partitioned_CSV_FileArray(CSV_FileArrayRow, CSV_FileArrayColumn) <> "" Then  '           If a value is found then ...
                    OutputArray(OutputArrayRow, 5) = _
                            Partitioned_CSV_FileArray(CSV_FileArrayRow, CSV_FileArrayColumn)    '               Save the value to Column 5 of OutputArray
                    OutputArray(OutputArrayRow, 4) = _
                            Partitioned_CSV_FileArray(CSV_FileArrayRow, CSV_FileArrayColumn - 1)    '           Save Cr amount to column 4 of OutputArray
                    OutputArray(OutputArrayRow, 3) = _
                            Partitioned_CSV_FileArray(CSV_FileArrayRow, CSV_FileArrayColumn - 3)    '           Save Dr amount to column 3 of OutputArray
'
                    If OutputArray(OutputArrayRow, 3) <> vbNullString Then _
                            OutputArray(OutputArrayRow, 2) = "Payment"                          ' If OutputArray column 3 is not blank then set "Payment"
                    If OutputArray(OutputArrayRow, 4) <> vbNullString Then _
                            OutputArray(OutputArrayRow, 2) = "Receipt"                          ' If OutputArray column 4 is not blank then set "Receipt"
'
                    jRight = CSV_FileArrayColumn - 3 - 1
                    Exit For                                                                    ' Exit loop
                End If
            Next
        
            For jCntMid = jLeft To jRight                                                       ' Loop through columns for a cheque # value between the
'                                                                                               '       Branch Name column and the DR/CR amount column
                 If Partitioned_CSV_FileArray(CSV_FileArrayRow, jCntMid) <> "" Then             '   If cheque # found then ...
                    ChequeNumber = Partitioned_CSV_FileArray(CSV_FileArrayRow, jCntMid)         '       Save the cheque # into ChequeNumber
                    Exit For                                                                    '       Exit loop
                 End If
            Next
'
            OutputArray(OutputArrayRow, 1) = OutputArray(OutputArrayRow, 1) & _
                    " Txn No." & Partitioned_CSV_FileArray(CSV_FileArrayRow, 1)                 ' Append Transaction # to column 1 of OutputArray
'
            If BranchName <> "" Then OutputArray(OutputArrayRow, 1) = _
                    OutputArray(OutputArrayRow, 1) & " Branch Name - " & BranchName             ' If a BranchName was found then Append BranchName to column 1 of OutputArray
'
            If ChequeNumber <> "" Then OutputArray(OutputArrayRow, 1) = _
                    OutputArray(OutputArrayRow, 1) & " Cheque No. " & ChequeNumber              ' If a ChequeNumber was found then Append ChequeNumber to column 1 of OutputArray
'
            OutputArray(OutputArrayRow, 1) = Replace(OutputArray(OutputArrayRow, 1), Chr(10), "")   ' delete line feeds in description
'
            OutputArrayRow = OutputArrayRow + 1                                                 ' Increment OutputArrayRow
            BranchName = ""                                                                     ' Clear BranchName
            ChequeNumber = ""                                                                   ' Clear ChequeNumber
        End If
    Next
 
    HeaderArray = Array("Txn Date", "Description", "Voucher Type", "Dr Amount", "Cr Amount", "Balance") ' Establish HeaderArray

    With destSht
        .UsedRange.Clear                                                                        ' Clear the destination sheet
        .Columns("A:A").NumberFormat = "@"                                                      ' Set column A to text so Excel don't mess up the dates
        .Range("A1").Resize(, UBound(HeaderArray) + 1) = HeaderArray                            ' Write the Header columns to destination sheet
        .Range("A1").Resize(, UBound(HeaderArray) + 1).Font.Bold = True                         ' Set the Header to Bold type font
'
        .Range("A2").Resize(UBound(DatesArray)).Value = Application.Transpose(DatesArray)       ' Display the dates to destination sheet Column A
'
        .Range("B2").Resize(UBound(OutputArray, 1), UBound(OutputArray, 2)).Value = OutputArray ' Display other results to destination sheet column B
'
''        .Range("F:H").EntireColumn.NumberFormat = "_(* #,##0.00_);[Red]_(* (#,##0.00);;_(@_)"   ' Amount fields
        .UsedRange.Columns.AutoFit                                                              ' Resize the columns to fit the data
    End With
'
    
'
    Application.ScreenUpdating = True                                                           ' Turn ScreenUpdating back on
'
    Debug.Print "Time to complete = " & Timer - startTime & " seconds."                         ' about .4 seconds
End Sub
 
Upvote 0
Rich (BB code):
Sheets("Sheet2").Range("A1").Resize(UBound(Partitioned_CSV_FileArray, 1), UBound(Partitioned_CSV_FileArray, 2)) = Partitioned_CSV_FileArray
run time error.
Delete that line
 
Upvote 0
This is not the only sheet I have to convert. There will be 25 -30 sheets each time to convert. So, I was thinking if the JohnnyL sheet is copied and pasted to a new sheet which will be named depending on the first date and the last date, it would be great. Example sheet name = 01-01-2021 to 31-12-2021
 
Upvote 0
Secondly, each time I press the convert button, I have to rename the sheet, insert a new sheet and rename it to JohnnyL. So, I a code to be added at the end of your code.
This is why I need by the new sheet name.
 
Upvote 0
This is not the only sheet I have to convert. There will be 25 -30 sheets each time to convert. So, I was thinking if the JohnnyL sheet is copied and pasted to a new sheet which will be named depending on the first date and the last date, it would be great. Example sheet name = 01-01-2021 to 31-12-2021
How about:

VBA Code:
Sub LoadCSV_FileTest4()
'
    Dim startTime                       As Single
    Dim CSV_FileToOpen                  As Variant
'
    CSV_FileToOpen = Application.GetOpenFilename("Text files,*.csv", , "Select file", , False)  ' Save full path of CSV file to CSV_FileToOpen
    If CSV_FileToOpen = False Then                                                              ' If user cancelled then ...
        MsgBox "No file selected - exiting"                                                     '   Display message box to user indicating the cancellation
        Exit Sub                                                                                '   Exit the sub
    End If
'
    startTime = Timer                                                                           ' Start the stopwatch
'
    Application.ScreenUpdating = False                                                          ' Turn ScreenUpdating off
'
    Dim ArrayRow                        As Long
    Dim CSV_ColumnMinus1                As Long, CSV_FileArrayColumn        As Long, CSV_FileArrayRow   As Long, CSV_FileRow    As Long
    Dim jLeft                           As Long, jCntMid                    As Long, jRight             As Long
    Dim DatesArrayRow                   As Long
    Dim FreeFileNumber                  As Long
    Dim OutputArrayColumn               As Long, OutputArrayRow             As Long
    Dim RowNumber                       As Long
    Dim BranchName                      As String, ChequeNumber             As String
    Dim All_CSV_RowsFromCSV_FileArray   As Variant, CSV_FileRowColumnsArray As Variant, Partitioned_CSV_FileArray               As Variant
    Dim CombinedAndReversedArray        As Variant
    Dim DatesArray                      As Variant, HeaderArray             As Variant, OutputArray()                           As Variant
    Dim destSht                         As Worksheet
'
    FreeFileNumber = FreeFile                                                                   ' Get an unused file number
    Open CSV_FileToOpen For Input As #FreeFileNumber
'
    If Err.Number <> 0 Then                                                                     ' If error occurred then ...
        MsgBox "File open error #" & Err.Number & "!", vbCritical, "Error!"                     '   Display error #
        Exit Sub                                                                                '   Exit sub
    End If
'
    All_CSV_RowsFromCSV_FileArray = Split(Input(LOF(FreeFileNumber), #FreeFileNumber), vbCrLf)  ' Load all Rows in csv file to All_CSV_RowsFromCSV_FileArray
    Close #FreeFileNumber                                                                       ' Close the csv file
'
    RowNumber = 0                                                                               ' Initialize RowNumber
'
    ReDim Partitioned_CSV_FileArray(1 To UBound(All_CSV_RowsFromCSV_FileArray), 1 To 100)       ' Set rows/columns for Partitioned_CSV_FileArray
'
    For CSV_FileRow = LBound(All_CSV_RowsFromCSV_FileArray) To UBound(All_CSV_RowsFromCSV_FileArray)    ' Loop through all rows of CSV file
        If All_CSV_RowsFromCSV_FileArray(CSV_FileRow) <> vbNullString Then                              '   If CSV row is not blank then ...
            CSV_FileRowColumnsArray = Split(All_CSV_RowsFromCSV_FileArray(CSV_FileRow), ",")            '       Load contents of row to CSV_FileRowColumnsArray
'
            RowNumber = RowNumber + 1                                                                   '       Increment RowNumber
'
            For CSV_ColumnMinus1 = LBound(CSV_FileRowColumnsArray) To UBound(CSV_FileRowColumnsArray)   '       Loop through csv columns
                Partitioned_CSV_FileArray(RowNumber, CSV_ColumnMinus1 + 1) = _
                        CSV_FileRowColumnsArray(CSV_ColumnMinus1)                                       '           Save values to Partitioned_CSV_FileArray
            Next                                                                                        '       Loop back
        End If
    Next                                                                                                ' Loop back
'
''Sheets("Sheet2").Range("A1").Resize(UBound(Partitioned_CSV_FileArray, 1), UBound(Partitioned_CSV_FileArray, 2)) = Partitioned_CSV_FileArray
'
'   All values from the CSV File are now loaded into Partitioned_CSV_FileArray
'
'-----------------------------------------------------------------------------------------------------
'
'   Intercept CSV file dates before excel gets it's hands on them
'
    ReDim DatesArray(1 To UBound(Partitioned_CSV_FileArray, 1))                                 ' Set # of rows for DatesArray
'
    DatesArrayRow = 0                                                                           ' Initialize DatesArrayRow
'
    For CSV_FileArrayRow = 1 To UBound(Partitioned_CSV_FileArray, 1)                            ' Loop through Partitioned_CSV_FileArray rows
        If IsNumeric(Right(Partitioned_CSV_FileArray(CSV_FileArrayRow, 1), 6)) Then             '   If transaction # found then ...
            DatesArrayRow = DatesArrayRow + 1                                                   '       Increment DatesArrayRow
'
            For CSV_FileArrayColumn = 2 To UBound(Partitioned_CSV_FileArray, 2)                 '       Loop through csv file columns starting from 2nd column
                If Partitioned_CSV_FileArray(CSV_FileArrayRow, CSV_FileArrayColumn) <> "" Then  '           If a value is found then ...
                    DatesArray(DatesArrayRow) = Partitioned_CSV_FileArray(CSV_FileArrayRow, CSV_FileArrayColumn)    '            Save the date into DatesArray
                    Exit For                                                                                        '            Exit loop
                End If
            Next                                                                                                    '       Loop back
        End If
    Next                                                                                                            ' Loop back
'
'   Dates needed are now safely stored into DatesArray
'
'-------------------------------------------------------------------------------------------------
'
    Workbooks.Open Filename:=CSV_FileToOpen                                                     ' Open the CSV file into Excel
'
    Partitioned_CSV_FileArray = ActiveWorkbook.ActiveSheet.UsedRange.Value                      ' Save the values from CSV file into Partitioned_CSV_FileArray
'
    ReDim OutputArray(1 To UBound(Partitioned_CSV_FileArray, 1), 1 To 5)                        ' Set rows/columns for OutputArray
'
    OutputArrayRow = 1                                                                          ' Initialize OutputArrayRow
'
    For CSV_FileArrayRow = 1 To UBound(Partitioned_CSV_FileArray, 1)                            ' Loop through rows of Partitioned_CSV_FileArray
        If IsNumeric(Right(Partitioned_CSV_FileArray(CSV_FileArrayRow, 1), 6)) Then             '   If transaction # found then ...
            OutputArrayColumn = 0                                                               '       Initialize OutputArrayColumn
'
            For CSV_FileArrayColumn = 2 To UBound(Partitioned_CSV_FileArray, 2)                 '       Loop through columns of Partitioned_CSV_FileArray
                If Partitioned_CSV_FileArray(CSV_FileArrayRow, CSV_FileArrayColumn) <> "" Then  '           If value found then ...
                    OutputArrayColumn = OutputArrayColumn + 1                                   '               Increment OutputArrayColumn
'
                    If OutputArrayColumn = 2 Then                                               '               If 'Description' is found then ...
                        OutputArray(OutputArrayRow, OutputArrayColumn - 1) = _
                                Partitioned_CSV_FileArray(CSV_FileArrayRow, CSV_FileArrayColumn)    '                   Save it into OutputArray Column 1
                    End If
'
                    If OutputArrayColumn = 3 Then                                               '               If 4th value in row found then ...
                        If Partitioned_CSV_FileArray(CSV_FileArrayRow, CSV_FileArrayColumn) <> "-" Then '                       If value found <> '-' then ...
                            BranchName = Partitioned_CSV_FileArray(CSV_FileArrayRow, CSV_FileArrayColumn)   '                       save it into BranchName
                        End If
'
                        jLeft = CSV_FileArrayColumn + 1                                         '                   Set col # to next ie. col 29->30
                        Exit For                                                                '                   Exit column loop
                    End If
                End If
            Next                                                                                '       Loop back
             
            For CSV_FileArrayColumn = UBound(Partitioned_CSV_FileArray, 2) To jLeft Step -1     '       Loop backwards from last column of csv to 4th value found column + 1
                If Partitioned_CSV_FileArray(CSV_FileArrayRow, CSV_FileArrayColumn) <> "" Then  '           If a value is found then ...
                    OutputArray(OutputArrayRow, 5) = _
                            Left(Partitioned_CSV_FileArray(CSV_FileArrayRow, CSV_FileArrayColumn), _
                            Len(Partitioned_CSV_FileArray(CSV_FileArrayRow, CSV_FileArrayColumn)) - 4)  '               Save the value to Column 5 of OutputArray
                    OutputArray(OutputArrayRow, 4) = _
                            Partitioned_CSV_FileArray(CSV_FileArrayRow, CSV_FileArrayColumn - 1)    '           Save Cr amount to column 4 of OutputArray
                    OutputArray(OutputArrayRow, 3) = _
                            Partitioned_CSV_FileArray(CSV_FileArrayRow, CSV_FileArrayColumn - 3)    '           Save Dr amount to column 3 of OutputArray
'
                    If OutputArray(OutputArrayRow, 3) <> vbNullString Then _
                            OutputArray(OutputArrayRow, 2) = "Payment"                          ' If OutputArray column 3 is not blank then set "Payment"
                    If OutputArray(OutputArrayRow, 4) <> vbNullString Then _
                            OutputArray(OutputArrayRow, 2) = "Receipt"                          ' If OutputArray column 4 is not blank then set "Receipt"
'
                    jRight = CSV_FileArrayColumn - 3 - 1
                    Exit For                                                                    ' Exit loop
                End If
            Next
            
            For jCntMid = jLeft To jRight                                                       ' Loop through columns for a cheque # value between the
'                                                                                               '       Branch Name column and the DR/CR amount column
                 If Partitioned_CSV_FileArray(CSV_FileArrayRow, jCntMid) <> "" Then             '   If cheque # found then ...
                    ChequeNumber = Partitioned_CSV_FileArray(CSV_FileArrayRow, jCntMid)         '       Save the cheque # into ChequeNumber
                    Exit For                                                                    '       Exit loop
                 End If
            Next
'
            OutputArray(OutputArrayRow, 1) = OutputArray(OutputArrayRow, 1) & _
                    " Txn No." & Partitioned_CSV_FileArray(CSV_FileArrayRow, 1)                 ' Append Transaction # to column 1 of OutputArray
'
            If BranchName <> "" Then OutputArray(OutputArrayRow, 1) = _
                    OutputArray(OutputArrayRow, 1) & " Branch Name - " & BranchName             ' If a BranchName was found then Append BranchName to column 1 of OutputArray
'
            If ChequeNumber <> "" Then OutputArray(OutputArrayRow, 1) = _
                    OutputArray(OutputArrayRow, 1) & " Cheque No. " & ChequeNumber              ' If a ChequeNumber was found then Append ChequeNumber to column 1 of OutputArray
'
            OutputArray(OutputArrayRow, 1) = Replace(OutputArray(OutputArrayRow, 1), Chr(10), "")   ' delete line feeds in description
'
            OutputArrayRow = OutputArrayRow + 1                                                 ' Increment OutputArrayRow
            BranchName = ""                                                                     ' Clear BranchName
            ChequeNumber = ""                                                                   ' Clear ChequeNumber
        End If
    Next
'
    Application.DisplayAlerts = False
    ActiveWorkbook.Close SaveChanges:=False                                                     ' Close the csv file that was opened earlier
    Application.DisplayAlerts = True
'
'-------------------------------------------------------------------------------------------------
'
'   Combine and Reverse order of 2 arrays ... DatesArray & OutputArray into CombinedAndReversedArray
'
    ReDim CombinedAndReversedArray(1 To UBound(DatesArray), 1 To 6)
'
    RowNumber = 0
'
    For ArrayRow = UBound(DatesArray) To 1 Step -1
        If DatesArray(ArrayRow) <> vbNullString Then
            RowNumber = RowNumber + 1
'
            CombinedAndReversedArray(RowNumber, 1) = DatesArray(ArrayRow)
            CombinedAndReversedArray(RowNumber, 2) = OutputArray(ArrayRow, 1)
            CombinedAndReversedArray(RowNumber, 3) = OutputArray(ArrayRow, 2)
            CombinedAndReversedArray(RowNumber, 4) = OutputArray(ArrayRow, 3)
            CombinedAndReversedArray(RowNumber, 5) = OutputArray(ArrayRow, 4)
            CombinedAndReversedArray(RowNumber, 6) = OutputArray(ArrayRow, 5)
        End If
    Next
'
'-------------------------------------------------------------------------------------------------
'
    Sheets.Add(After:=ActiveSheet).Name = CombinedAndReversedArray(1, 1) & " to " & CombinedAndReversedArray(RowNumber, 1)
'
    Set destSht = ActiveSheet                                                                   ' Set the sheet name to display results into
'
    HeaderArray = Array("Txn Date", "Description", "Voucher Type", "Dr Amount", "Cr Amount", "Balance") ' Establish HeaderArray
'
    With destSht
        .UsedRange.Clear                                                                        ' Clear the destination sheet
        .Columns("A:A").NumberFormat = "@"                                                      ' Set column A to text so Excel don't mess up the dates
        .Range("A1").Resize(, UBound(HeaderArray) + 1) = HeaderArray                            ' Write the Header columns to destination sheet
        .Range("A1").Resize(, UBound(HeaderArray) + 1).Font.Bold = True                         ' Set the Header to Bold type font
'
        .Range("A2").Resize(UBound(CombinedAndReversedArray, 1), _
                UBound(CombinedAndReversedArray, 2)).Value = CombinedAndReversedArray           ' Display results to destination sheet
'
''        .Range("F:H").EntireColumn.NumberFormat = "_(* #,##0.00_);[Red]_(* (#,##0.00);;_(@_)"   ' Amount fields
        .Columns("D:F").NumberFormat = "0.00"                                                          ' Format Columns D:E to two decimal places
        .UsedRange.Columns.AutoFit                                                              ' Resize the columns to fit the data
    End With
'
    Application.ScreenUpdating = True                                                           ' Turn ScreenUpdating back on
'
    Debug.Print "Time to complete = " & Timer - startTime & " seconds."                         ' about .5 seconds
End Sub

You will get an error if it works out that more than one sheet is trying to be named the same as a previously named sheet.
 
Upvote 0
Solution
I
How about:

VBA Code:
Sub LoadCSV_FileTest4()
'
    Dim startTime                       As Single
    Dim CSV_FileToOpen                  As Variant
'
    CSV_FileToOpen = Application.GetOpenFilename("Text files,*.csv", , "Select file", , False)  ' Save full path of CSV file to CSV_FileToOpen
    If CSV_FileToOpen = False Then                                                              ' If user cancelled then ...
        MsgBox "No file selected - exiting"                                                     '   Display message box to user indicating the cancellation
        Exit Sub                                                                                '   Exit the sub
    End If
'
    startTime = Timer                                                                           ' Start the stopwatch
'
    Application.ScreenUpdating = False                                                          ' Turn ScreenUpdating off
'
    Dim ArrayRow                        As Long
    Dim CSV_ColumnMinus1                As Long, CSV_FileArrayColumn        As Long, CSV_FileArrayRow   As Long, CSV_FileRow    As Long
    Dim jLeft                           As Long, jCntMid                    As Long, jRight             As Long
    Dim DatesArrayRow                   As Long
    Dim FreeFileNumber                  As Long
    Dim OutputArrayColumn               As Long, OutputArrayRow             As Long
    Dim RowNumber                       As Long
    Dim BranchName                      As String, ChequeNumber             As String
    Dim All_CSV_RowsFromCSV_FileArray   As Variant, CSV_FileRowColumnsArray As Variant, Partitioned_CSV_FileArray               As Variant
    Dim CombinedAndReversedArray        As Variant
    Dim DatesArray                      As Variant, HeaderArray             As Variant, OutputArray()                           As Variant
    Dim destSht                         As Worksheet
'
    FreeFileNumber = FreeFile                                                                   ' Get an unused file number
    Open CSV_FileToOpen For Input As #FreeFileNumber
'
    If Err.Number <> 0 Then                                                                     ' If error occurred then ...
        MsgBox "File open error #" & Err.Number & "!", vbCritical, "Error!"                     '   Display error #
        Exit Sub                                                                                '   Exit sub
    End If
'
    All_CSV_RowsFromCSV_FileArray = Split(Input(LOF(FreeFileNumber), #FreeFileNumber), vbCrLf)  ' Load all Rows in csv file to All_CSV_RowsFromCSV_FileArray
    Close #FreeFileNumber                                                                       ' Close the csv file
'
    RowNumber = 0                                                                               ' Initialize RowNumber
'
    ReDim Partitioned_CSV_FileArray(1 To UBound(All_CSV_RowsFromCSV_FileArray), 1 To 100)       ' Set rows/columns for Partitioned_CSV_FileArray
'
    For CSV_FileRow = LBound(All_CSV_RowsFromCSV_FileArray) To UBound(All_CSV_RowsFromCSV_FileArray)    ' Loop through all rows of CSV file
        If All_CSV_RowsFromCSV_FileArray(CSV_FileRow) <> vbNullString Then                              '   If CSV row is not blank then ...
            CSV_FileRowColumnsArray = Split(All_CSV_RowsFromCSV_FileArray(CSV_FileRow), ",")            '       Load contents of row to CSV_FileRowColumnsArray
'
            RowNumber = RowNumber + 1                                                                   '       Increment RowNumber
'
            For CSV_ColumnMinus1 = LBound(CSV_FileRowColumnsArray) To UBound(CSV_FileRowColumnsArray)   '       Loop through csv columns
                Partitioned_CSV_FileArray(RowNumber, CSV_ColumnMinus1 + 1) = _
                        CSV_FileRowColumnsArray(CSV_ColumnMinus1)                                       '           Save values to Partitioned_CSV_FileArray
            Next                                                                                        '       Loop back
        End If
    Next                                                                                                ' Loop back
'
''Sheets("Sheet2").Range("A1").Resize(UBound(Partitioned_CSV_FileArray, 1), UBound(Partitioned_CSV_FileArray, 2)) = Partitioned_CSV_FileArray
'
'   All values from the CSV File are now loaded into Partitioned_CSV_FileArray
'
'-----------------------------------------------------------------------------------------------------
'
'   Intercept CSV file dates before excel gets it's hands on them
'
    ReDim DatesArray(1 To UBound(Partitioned_CSV_FileArray, 1))                                 ' Set # of rows for DatesArray
'
    DatesArrayRow = 0                                                                           ' Initialize DatesArrayRow
'
    For CSV_FileArrayRow = 1 To UBound(Partitioned_CSV_FileArray, 1)                            ' Loop through Partitioned_CSV_FileArray rows
        If IsNumeric(Right(Partitioned_CSV_FileArray(CSV_FileArrayRow, 1), 6)) Then             '   If transaction # found then ...
            DatesArrayRow = DatesArrayRow + 1                                                   '       Increment DatesArrayRow
'
            For CSV_FileArrayColumn = 2 To UBound(Partitioned_CSV_FileArray, 2)                 '       Loop through csv file columns starting from 2nd column
                If Partitioned_CSV_FileArray(CSV_FileArrayRow, CSV_FileArrayColumn) <> "" Then  '           If a value is found then ...
                    DatesArray(DatesArrayRow) = Partitioned_CSV_FileArray(CSV_FileArrayRow, CSV_FileArrayColumn)    '            Save the date into DatesArray
                    Exit For                                                                                        '            Exit loop
                End If
            Next                                                                                                    '       Loop back
        End If
    Next                                                                                                            ' Loop back
'
'   Dates needed are now safely stored into DatesArray
'
'-------------------------------------------------------------------------------------------------
'
    Workbooks.Open Filename:=CSV_FileToOpen                                                     ' Open the CSV file into Excel
'
    Partitioned_CSV_FileArray = ActiveWorkbook.ActiveSheet.UsedRange.Value                      ' Save the values from CSV file into Partitioned_CSV_FileArray
'
    ReDim OutputArray(1 To UBound(Partitioned_CSV_FileArray, 1), 1 To 5)                        ' Set rows/columns for OutputArray
'
    OutputArrayRow = 1                                                                          ' Initialize OutputArrayRow
'
    For CSV_FileArrayRow = 1 To UBound(Partitioned_CSV_FileArray, 1)                            ' Loop through rows of Partitioned_CSV_FileArray
        If IsNumeric(Right(Partitioned_CSV_FileArray(CSV_FileArrayRow, 1), 6)) Then             '   If transaction # found then ...
            OutputArrayColumn = 0                                                               '       Initialize OutputArrayColumn
'
            For CSV_FileArrayColumn = 2 To UBound(Partitioned_CSV_FileArray, 2)                 '       Loop through columns of Partitioned_CSV_FileArray
                If Partitioned_CSV_FileArray(CSV_FileArrayRow, CSV_FileArrayColumn) <> "" Then  '           If value found then ...
                    OutputArrayColumn = OutputArrayColumn + 1                                   '               Increment OutputArrayColumn
'
                    If OutputArrayColumn = 2 Then                                               '               If 'Description' is found then ...
                        OutputArray(OutputArrayRow, OutputArrayColumn - 1) = _
                                Partitioned_CSV_FileArray(CSV_FileArrayRow, CSV_FileArrayColumn)    '                   Save it into OutputArray Column 1
                    End If
'
                    If OutputArrayColumn = 3 Then                                               '               If 4th value in row found then ...
                        If Partitioned_CSV_FileArray(CSV_FileArrayRow, CSV_FileArrayColumn) <> "-" Then '                       If value found <> '-' then ...
                            BranchName = Partitioned_CSV_FileArray(CSV_FileArrayRow, CSV_FileArrayColumn)   '                       save it into BranchName
                        End If
'
                        jLeft = CSV_FileArrayColumn + 1                                         '                   Set col # to next ie. col 29->30
                        Exit For                                                                '                   Exit column loop
                    End If
                End If
            Next                                                                                '       Loop back
            
            For CSV_FileArrayColumn = UBound(Partitioned_CSV_FileArray, 2) To jLeft Step -1     '       Loop backwards from last column of csv to 4th value found column + 1
                If Partitioned_CSV_FileArray(CSV_FileArrayRow, CSV_FileArrayColumn) <> "" Then  '           If a value is found then ...
                    OutputArray(OutputArrayRow, 5) = _
                            Left(Partitioned_CSV_FileArray(CSV_FileArrayRow, CSV_FileArrayColumn), _
                            Len(Partitioned_CSV_FileArray(CSV_FileArrayRow, CSV_FileArrayColumn)) - 4)  '               Save the value to Column 5 of OutputArray
                    OutputArray(OutputArrayRow, 4) = _
                            Partitioned_CSV_FileArray(CSV_FileArrayRow, CSV_FileArrayColumn - 1)    '           Save Cr amount to column 4 of OutputArray
                    OutputArray(OutputArrayRow, 3) = _
                            Partitioned_CSV_FileArray(CSV_FileArrayRow, CSV_FileArrayColumn - 3)    '           Save Dr amount to column 3 of OutputArray
'
                    If OutputArray(OutputArrayRow, 3) <> vbNullString Then _
                            OutputArray(OutputArrayRow, 2) = "Payment"                          ' If OutputArray column 3 is not blank then set "Payment"
                    If OutputArray(OutputArrayRow, 4) <> vbNullString Then _
                            OutputArray(OutputArrayRow, 2) = "Receipt"                          ' If OutputArray column 4 is not blank then set "Receipt"
'
                    jRight = CSV_FileArrayColumn - 3 - 1
                    Exit For                                                                    ' Exit loop
                End If
            Next
           
            For jCntMid = jLeft To jRight                                                       ' Loop through columns for a cheque # value between the
'                                                                                               '       Branch Name column and the DR/CR amount column
                 If Partitioned_CSV_FileArray(CSV_FileArrayRow, jCntMid) <> "" Then             '   If cheque # found then ...
                    ChequeNumber = Partitioned_CSV_FileArray(CSV_FileArrayRow, jCntMid)         '       Save the cheque # into ChequeNumber
                    Exit For                                                                    '       Exit loop
                 End If
            Next
'
            OutputArray(OutputArrayRow, 1) = OutputArray(OutputArrayRow, 1) & _
                    " Txn No." & Partitioned_CSV_FileArray(CSV_FileArrayRow, 1)                 ' Append Transaction # to column 1 of OutputArray
'
            If BranchName <> "" Then OutputArray(OutputArrayRow, 1) = _
                    OutputArray(OutputArrayRow, 1) & " Branch Name - " & BranchName             ' If a BranchName was found then Append BranchName to column 1 of OutputArray
'
            If ChequeNumber <> "" Then OutputArray(OutputArrayRow, 1) = _
                    OutputArray(OutputArrayRow, 1) & " Cheque No. " & ChequeNumber              ' If a ChequeNumber was found then Append ChequeNumber to column 1 of OutputArray
'
            OutputArray(OutputArrayRow, 1) = Replace(OutputArray(OutputArrayRow, 1), Chr(10), "")   ' delete line feeds in description
'
            OutputArrayRow = OutputArrayRow + 1                                                 ' Increment OutputArrayRow
            BranchName = ""                                                                     ' Clear BranchName
            ChequeNumber = ""                                                                   ' Clear ChequeNumber
        End If
    Next
'
    Application.DisplayAlerts = False
    ActiveWorkbook.Close SaveChanges:=False                                                     ' Close the csv file that was opened earlier
    Application.DisplayAlerts = True
'
'-------------------------------------------------------------------------------------------------
'
'   Combine and Reverse order of 2 arrays ... DatesArray & OutputArray into CombinedAndReversedArray
'
    ReDim CombinedAndReversedArray(1 To UBound(DatesArray), 1 To 6)
'
    RowNumber = 0
'
    For ArrayRow = UBound(DatesArray) To 1 Step -1
        If DatesArray(ArrayRow) <> vbNullString Then
            RowNumber = RowNumber + 1
'
            CombinedAndReversedArray(RowNumber, 1) = DatesArray(ArrayRow)
            CombinedAndReversedArray(RowNumber, 2) = OutputArray(ArrayRow, 1)
            CombinedAndReversedArray(RowNumber, 3) = OutputArray(ArrayRow, 2)
            CombinedAndReversedArray(RowNumber, 4) = OutputArray(ArrayRow, 3)
            CombinedAndReversedArray(RowNumber, 5) = OutputArray(ArrayRow, 4)
            CombinedAndReversedArray(RowNumber, 6) = OutputArray(ArrayRow, 5)
        End If
    Next
'
'-------------------------------------------------------------------------------------------------
'
    Sheets.Add(After:=ActiveSheet).Name = CombinedAndReversedArray(1, 1) & " to " & CombinedAndReversedArray(RowNumber, 1)
'
    Set destSht = ActiveSheet                                                                   ' Set the sheet name to display results into
'
    HeaderArray = Array("Txn Date", "Description", "Voucher Type", "Dr Amount", "Cr Amount", "Balance") ' Establish HeaderArray
'
    With destSht
        .UsedRange.Clear                                                                        ' Clear the destination sheet
        .Columns("A:A").NumberFormat = "@"                                                      ' Set column A to text so Excel don't mess up the dates
        .Range("A1").Resize(, UBound(HeaderArray) + 1) = HeaderArray                            ' Write the Header columns to destination sheet
        .Range("A1").Resize(, UBound(HeaderArray) + 1).Font.Bold = True                         ' Set the Header to Bold type font
'
        .Range("A2").Resize(UBound(CombinedAndReversedArray, 1), _
                UBound(CombinedAndReversedArray, 2)).Value = CombinedAndReversedArray           ' Display results to destination sheet
'
''        .Range("F:H").EntireColumn.NumberFormat = "_(* #,##0.00_);[Red]_(* (#,##0.00);;_(@_)"   ' Amount fields
        .Columns("D:F").NumberFormat = "0.00"                                                          ' Format Columns D:E to two decimal places
        .UsedRange.Columns.AutoFit                                                              ' Resize the columns to fit the data
    End With
'
    Application.ScreenUpdating = True                                                           ' Turn ScreenUpdating back on
'
    Debug.Print "Time to complete = " & Timer - startTime & " seconds."                         ' about .5 seconds
End Sub

You will get an error if it works out that more than one sheet is trying to be named the same as a previously named sheet.
JohnnyL. It is Perfect. Finally, an end of a wonderful era. Thanks a Million JohnnyL.
 
Upvote 0

Forum statistics

Threads
1,215,063
Messages
6,122,935
Members
449,094
Latest member
teemeren

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