Justify data in right columns

Panoos64

Well-known Member
Joined
Mar 1, 2014
Messages
882
Hi all, i would like to create a VBA so that to adjust the below data in right columns and entering headings too as my below extract. In sch.1. are the data which the columns are mixed and in sch.2. is the expected result. I would be greatly appreciated any help. Thanks in advance

Sch.1.


ABCDEFGHIJKLMNO
110-02-18 JV1005623 5.62 DISCOUNT 9456
2 15-02-181005648 562.40 PURCHASES9458
328-02-18 1005655 309.70PURCHASES 9465
402-03-18 JV1005701 3.00 DISCOUNT9518
503-03-181005718 83.60PURCHASES 9524

<colgroup><col style="mso-width-source:userset;mso-width-alt:1060;width:22pt" width="29"> <col style="width:48pt" width="64" span="4"> <col style="mso-width-source:userset;mso-width-alt:3181;width:65pt" width="87"> <col style="mso-width-source:userset;mso-width-alt:1682;width:35pt" width="46"> <col style="mso-width-source:userset;mso-width-alt:1097;width:23pt" width="30"> <col style="mso-width-source:userset;mso-width-alt:1682;width:35pt" width="46"> <col style="width:48pt" width="64" span="6"> <col style="mso-width-source:userset;mso-width-alt:1718;width:35pt" width="47"> </colgroup><tbody>
</tbody>


Sch.2.


ABCDEF
1DATEINV.#DEBITCREDITDESCRIPTIONTR.#
210-02-1810056235.62 DISCOUNT9456
315-02-181005648 562.40PURCHASES9458
428-02-181005655 309.70PURCHASES9465
502-03-1810057013.00 DISCOUNT9518
603-03-181005718 83.60PURCHASES9524

<colgroup><col style="mso-width-source:userset;mso-width-alt:1060;width:22pt" width="29"> <col style="width:48pt" width="64" span="4"> <col style="mso-width-source:userset;mso-width-alt:3181;width:65pt" width="87"> <col style="mso-width-source:userset;mso-width-alt:1682;width:35pt" width="46"> </colgroup><tbody>
</tbody>
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Are the cells empty that offset the columns?
 
Upvote 0
Are the cells with "JV" treated as blank?
 
Upvote 0
Hi nemmi, yes the cells offset the columns are empty, and therefore that i don't need the JV cells, so we can clear or delete them.
Therefore that the above is just an extract of data and the columns and rows, change each time when i export the data from accounting system. The maximum of rows arises to 5000 and columns' range A-O, Basically, the code should run through the whole spreadsheet and should delete by horizontal empty/blank cells, including "JV", so that to justify the columns and data, but this complicate to determine the numbers which are below to DEBIT and CREDIT side.
I would like to thank for your time and your interesting to resolve my project.
Hv a nice day.
 
Last edited:
Upvote 0
How will the debit and credit values be determined if all the blank cells are removed?
 
Upvote 0
This was my query and my questioning. Do you have any other suggestion?
Thanks once again
 
Upvote 0
Is there always a "JV" if its a debit?
 
Upvote 0
Now i think you found the key to solution. I done a mistake above and is not "JV" but "JV2". The debit side marked always by transaction type "JV2" but is not stably in col. "D". Sometimes it appears in "D" but most of the times is mixed with date column in the same cell. please see below. If this help you to create the code, it doesn't matter for my project to appear a mixed date with additional text JV2, so do not try to split such cells.

Many thanks for your advice and i express my apologies for my fault.




A
B
CDEFGHIJKLMNO
110-02-18
JV210056235.62DISCOUNT9456
215-02-181005648562.40PURCHASES9458
328-02-181005655309.70PURCHASES9465
402-03-18 JV2
10057013.00DISCOUNT9518
503-03-18100571883.60PURCHASES9524

<tbody>
</tbody>
 
Last edited:
Upvote 0
Just need to find a way to determine how many rows

Code:
Option Explicit


Dim RowCount As Long
Dim ColCount As Long
Dim CellOK As Boolean
Dim CellDelCount As Integer
Dim IsDebit As Boolean


Sub JustifyDataInRightColumns()


For RowCount = 1 To 6
   IsDebit = False
   For ColCount = 1 To 5 'A to E
        CellOK = False
        CellDelCount = 0
        Do While CellOK = False
            If IsEmpty(Cells(RowCount, ColCount).Value) Or Trim(Cells(RowCount, ColCount).Value) = "" Then
                Cells(RowCount, ColCount).Delete Shift:=xlToLeft
                CellDelCount = CellDelCount + 1
                If CellDelCount >= 15 Then 'has deleted up to column O
                    CellOK = True
                End If
            ElseIf Cells(RowCount, ColCount).Value = "JV" Then
                IsDebit = True
                Cells(RowCount, ColCount).Delete Shift:=xlToLeft
                CellDelCount = CellDelCount + 1
                If CellDelCount >= 15 Then 'has deleted up to column O
                    CellOK = True
                End If
            Else
                CellOK = True
            End If
        Loop
    Next ColCount
    If IsDebit = True Then
        Cells(RowCount, 4).Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    Else
        Cells(RowCount, 3).Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    End If


Next RowCount


'Set headers
Range("A1:F5").Cut Destination:=Range("A2:F6")
Range("A1").Value = "DATE"
Range("B1").Value = "INV.#"
Range("C1").Value = "DEBIT"
Range("D1").Value = "CREDIT"
Range("E1").Value = "DESCRIPTION"
Range("F1").Value = "TR.#"
End Sub
 
Upvote 0
Well done nemmi! Based on my data it works perfect. I express my thanks for your time spent for my project and your prompt respond. It was so kind from you.
Hv a great lovely day
 
Upvote 0

Forum statistics

Threads
1,214,908
Messages
6,122,187
Members
449,072
Latest member
DW Draft

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