VBA-Reorganise data

Jemma Atkinson

Well-known Member
Joined
Jul 7, 2008
Messages
509
Hi,

I need VBA to produce layout as shown below in the second screen shot

Excel Workbook
ABCDEFGHI
5GroupCase RefCustodianSet IDSourceValue DateEntry DateAgeCCY
6DOMS2CITIDOMSAMP128-Jun-1128-Jun-1117AUD
7DOMS3CITIDOMSAMP128-Jun-1128-Jun-1117AUD
8DOMS33CITIDOMSDERIV10-Jan-1110-Jan-11186AUD
9DOMS40CITIDOMSDERIV13-Apr-1113-Apr-1193AUD
10DOMS41CITIDOMSDERIV13-Apr-1113-Apr-1193AUD
11DOMS42CITIDOMSDERIV27-Jun-1125-Jun-1118AUD
12LIFE48CITILIFEAMP317-Jun-1117-Jun-1128AUD
13CITICASH66SSTREETDMPWSSAUDAMP22-May-113-May-1174AUD
14CITICASH67SSTREETDMPWSSAUDAMP22-May-113-May-1174AUD
15CITICASH68SSTREETDMPWSSAUDAMP22-May-113-May-1174AUD
16CITICASH69SSTREETDMPWSSAUDAMP22-May-113-May-1174AUD
17CITICASH70SSTREETDMPWSSAUDAMP22-May-113-May-1174AUD
18CITICASH71SSTREETDMPWSSAUDAMP22-May-113-May-1174AUD
19CITICASH72SSTREETDMPWSSAUDAMP22-May-113-May-1174AUD
20CITICASH73SSTREETDMPWSSAUDAMP22-May-113-May-1174AUD
21CITICASH74SSTREETDMPWSSAUDAMP22-May-113-May-1174AUD
Sheet1


Excel Workbook
ABCDEFGHI
5GroupCase RefCustodianSet IDSourceValue DateEntry DateAgeCCY
6DOMS2CITIDOMSAMP128-Jun-1128-Jun-1117AUD
7DOMS3CITIDOMSAMP128-Jun-1128-Jun-1117AUD
8DOMS33CITIDOMSDERIV10-Jan-1110-Jan-11186AUD
9DOMS40CITIDOMSDERIV13-Apr-1113-Apr-1193AUD
10DOMS41CITIDOMSDERIV13-Apr-1113-Apr-1193AUD
11DOMS42CITIDOMSDERIV27-Jun-1125-Jun-1118AUD
12
13GroupCase RefCustodianSet IDSourceValue DateEntry DateAgeCCY
14LIFE48CITILIFEAMP317-Jun-1117-Jun-1128AUD
15
16GroupCase RefCustodianSet IDSourceValue DateEntry DateAgeCCY
17CITICASH66SSTREETDMPWSSAUDAMP22-May-113-May-1174AUD
18CITICASH67SSTREETDMPWSSAUDAMP22-May-113-May-1174AUD
19CITICASH68SSTREETDMPWSSAUDAMP22-May-113-May-1174AUD
20CITICASH69SSTREETDMPWSSAUDAMP22-May-113-May-1174AUD
21CITICASH70SSTREETDMPWSSAUDAMP22-May-113-May-1174AUD
22CITICASH71SSTREETDMPWSSAUDAMP22-May-113-May-1174AUD
23CITICASH72SSTREETDMPWSSAUDAMP22-May-113-May-1174AUD
24CITICASH73SSTREETDMPWSSAUDAMP22-May-113-May-1174AUD
25CITICASH74SSTREETDMPWSSAUDAMP22-May-113-May-1174AUD
26
27
28
29
Sheet1
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Try:
Code:
Public Sub InsertHeader()
Dim lLastRow As Long
lLastRow = Range("A" & Rows.Count).End(xlUp).Row
Application.ScreenUpdating = False
For i = lLastRow To 3 Step -1
    If Range("A" & i).Value2 <> Range("A" & i - 1).Value2 Then
        Range("A" & i).Resize(2, 9).Insert Shift:=xlDown
        Range("A1").Resize(, 9).Copy Destination:=Range("A" & i + 1)
    End If
Next i
Application.ScreenUpdating = True
End Sub
 
Last edited:
Upvote 0
Maybe something like this:

Code:
Option Explicit
Sub AddHeadings()
Dim r As Long
Dim lastrow As Long

lastrow = Range("A" & Rows.Count).End(xlUp).Offset(1).Row - 1
For r = 3 To lastrow
    If Range("A" & r) <> Range("A" & r).Offset(-1, 0) Then
        Rows(r).Insert Shift:=xlDown
        Rows(r).Insert Shift:=xlDown
        Range("A" & r, "H" & r).Offset(1, 0) = Array("Group", "Case Ref", "Custodian", "Set ID", "Source", "Value Date", "Entry Date", "Age")
        Range("I" & r).Offset(1, 0) = "CCY"
    r = r + 2
    End If
    
Next r
End Sub

AMAS
 
Upvote 0
Does your code assume header row is 5?

Maybe something like this:

Code:
Option Explicit
Sub AddHeadings()
Dim r As Long
Dim lastrow As Long

lastrow = Range("A" & Rows.Count).End(xlUp).Offset(1).Row - 1
For r = 3 To lastrow
    If Range("A" & r) <> Range("A" & r).Offset(-1, 0) Then
        Rows(r).Insert Shift:=xlDown
        Rows(r).Insert Shift:=xlDown
        Range("A" & r, "H" & r).Offset(1, 0) = Array("Group", "Case Ref", "Custodian", "Set ID", "Source", "Value Date", "Entry Date", "Age")
        Range("I" & r).Offset(1, 0) = "CCY"
    r = r + 2
    End If
    
Next r
End Sub
AMAS
 
Upvote 0
Does your code assume header row is 5?
I missed it too!
Code:
Public Sub InsertHeader()
Dim lLastRow As Long
lLastRow = Range("A" & Rows.Count).End(xlUp).Row
Application.ScreenUpdating = False
For i = lLastRow To 7 Step -1
    If Range("A" & i).Value2 <> Range("A" & i - 1).Value2 Then
        Range("A" & i).Resize(2, 9).Insert Shift:=xlDown
        Range("A5").Resize(, 9).Copy Destination:=Range("A" & i + 1)
    End If
Next i
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Great, can we make the headers bold?


I missed it too!
Code:
Public Sub InsertHeader()
Dim lLastRow As Long
lLastRow = Range("A" & Rows.Count).End(xlUp).Row
Application.ScreenUpdating = False
For i = lLastRow To 7 Step -1
    If Range("A" & i).Value2 <> Range("A" & i - 1).Value2 Then
        Range("A" & i).Resize(2, 9).Insert Shift:=xlDown
        Range("A5").Resize(, 9).Copy Destination:=Range("A" & i + 1)
    End If
Next i
Application.ScreenUpdating = True
End Sub
 
Upvote 0
If i had formatting in COL F:G and then the macro does the splitting, how difficult would it be to get the below desired results?

Excel Workbook
ABCDEFGHI
5GroupCase RefCustodianSet IDSourceValue DateEntry DateAgeCCY
6DOMS2CITIDOMSAMP128-Jun-1128-Jun-1117AUD
7DOMS3CITIDOMSAMP128-Jun-1128-Jun-1117AUD
8DOMS33CITIDOMSDERIV10-Jan-1110-Jan-11186AUD
9DOMS40CITIDOMSDERIV13-Apr-1113-Apr-1193AUD
10DOMS41CITIDOMSDERIV13-Apr-1113-Apr-1193AUD
11DOMS42CITIDOMSDERIV27-Jun-1125-Jun-1118AUD
12
13GroupCase RefCustodianSet IDSourceValue DateEntry DateAgeCCY
14LIFE48CITILIFEAMP317-Jun-1117-Jun-1128AUD
15
16GroupCase RefCustodianSet IDSourceValue DateEntry DateAgeCCY
17CITICASH66SSTREETDMPWSSAUDAMP22-May-113-May-1174AUD
18CITICASH67SSTREETDMPWSSAUDAMP22-May-113-May-1174AUD
19CITICASH68SSTREETDMPWSSAUDAMP22-May-113-May-1174AUD
20CITICASH69SSTREETDMPWSSAUDAMP22-May-113-May-1174AUD
21CITICASH70SSTREETDMPWSSAUDAMP22-May-113-May-1174AUD
22CITICASH71SSTREETDMPWSSAUDAMP22-May-113-May-1174AUD
23CITICASH72SSTREETDMPWSSAUDAMP22-May-113-May-1174AUD
24CITICASH73SSTREETDMPWSSAUDAMP22-May-113-May-1174AUD
25CITICASH74SSTREETDMPWSSAUDAMP22-May-113-May-1174AUD
26
27
Sheet1
 
Upvote 0
Great, can we make the headers bold?
Thats good, make them bold @ Row 5 and then they all will be bold. Or any formatting you like. VBA can do that as well.

I had not tried formatting (not much) with VBA before. Here is the code. I have added another sub routine so that it remains simpler for usage and doesn't get mixed up.
Code:
Option Explicit
Dim lLastRow As Long
Dim i As Integer
Dim r As Range
Public Sub InsertHeader()
lLastRow = Range("A" & Rows.Count).End(xlUp).Row
Application.ScreenUpdating = False
Range("A5").Resize(, 9).Font.Bold = True
For i = lLastRow To 7 Step -1
    
    If Range("A" & i).Value2 <> Range("A" & i - 1).Value2 Then
        Range("A" & i).Resize(2, 9).Insert Shift:=xlDown
        Range("A5").Resize(, 9).Copy Destination:=Range("A" & i + 1)
    End If
Next i
Call UpdateFormatting
Application.ScreenUpdating = True
End Sub
Private Sub UpdateFormatting()
Dim o As Integer
lLastRow = Range("A" & Rows.Count).End(xlUp).Row
Set r = Range("A5")
Do While r.Row < lLastRow
    
    With r.CurrentRegion.Borders
    .LineStyle = xlContinuous
    .Weight = xlThin
    End With
    
    With r.Resize(, 9).Borders
    .LineStyle = xlContinuous
    .Weight = xlThick
    End With
        
    o = r.End(xlDown).Row - r.Row
    With r.Offset(1, 5).Resize(o, 2).Interior
    .Color = vbGreen
    End With
    
    Set r = r.End(xlDown).End(xlDown)
    
Loop
End Sub
 
Upvote 0
Since i will be running this code from another worksheet, is this the correct way of using With and End With?

Code:
Public Sub InsertHeader()

Application.ScreenUpdating = False

With Sheets(1)
lLastRow = .Range("A" & Rows.Count).End(xlUp).Row
    .Range("A5").Resize(, 9).Font.Bold = True
For i = lLastRow To 7 Step -1
    
    If .Range("A" & i).Value2 <> .Range("A" & i - 1).Value2 Then
        .Range("A" & i).Resize(2, 9).Insert Shift:=xlDown
        .Range("A5").Resize(, 9).Copy Destination:=.Range("A" & i + 1)
    End If
Next i
Call UpdateFormatting
End With
Application.ScreenUpdating = True
End Sub
Private Sub UpdateFormatting()
Dim o As Integer
    
    With Sheets(1)
    lLastRow = .Range("A" & Rows.Count).End(xlUp).Row
    Set r = .Range("A5")
    Do While r.Row < lLastRow
    
    With r.CurrentRegion.Borders
    .LineStyle = xlContinuous
    .Weight = xlThin
    End With
    
    With r.Resize(, 9).Borders
    .LineStyle = xlContinuous
    .Weight = xlThick
    End With
        
    o = r.End(xlDown).Row - r.Row
    With r.Offset(1, 5).Resize(o, 2).Interior
    .ColorIndex = 24
    End With
    
    Set r = r.End(xlDown).End(xlDown)
    Loop
    End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,600
Messages
6,179,834
Members
452,947
Latest member
Gerry_F

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