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
 
Perhaps

Code:
Sub test()
    Dim headerRange As Range
    Dim insertCell As Range
    Dim searchTerm As String
    
    Set headerRange = Sheet1.Range("A5:I5")
    Set insertCell = headerRange.Cells(2, 1)
    
    Application.CutCopyMode = False
    searchTerm = CStr(insertCell.Value)
    
    With headerRange.Cells(1, 1).EntireColumn
    
        Do Until searchTerm = vbNullString
                With .Cells(Application.Match(searchTerm, .Cells, 0), 1).Resize(Application.CountIf(.Cells, searchTerm), 1)
                    If Application.CutCopyMode = xlCopy Then
                        .Resize(1, headerRange.Columns.Count).Insert shift:=xlDown
                    End If
                    Set insertCell = .Cells(.Rows.Count + 1, 1)
                    searchTerm = CStr(insertCell.Value)
                    headerRange.Copy
                End With
        Loop
        
    End With
End Sub
 
Upvote 0

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
The code did not insert two row gap between each data set. How would the code be changed to look like #7

Perhaps

Code:
Sub test()
    Dim headerRange As Range
    Dim insertCell As Range
    Dim searchTerm As String
    
    Set headerRange = Sheet1.Range("A5:I5")
    Set insertCell = headerRange.Cells(2, 1)
    
    Application.CutCopyMode = False
    searchTerm = CStr(insertCell.Value)
    
    With headerRange.Cells(1, 1).EntireColumn
    
        Do Until searchTerm = vbNullString
                With .Cells(Application.Match(searchTerm, .Cells, 0), 1).Resize(Application.CountIf(.Cells, searchTerm), 1)
                    If Application.CutCopyMode = xlCopy Then
                        .Resize(1, headerRange.Columns.Count).Insert shift:=xlDown
                    End If
                    Set insertCell = .Cells(.Rows.Count + 1, 1)
                    searchTerm = CStr(insertCell.Value)
                    headerRange.Copy
                End With
        Loop
        
    End With
End Sub
 
Upvote 0
Code:
Sub test()
    Dim headerRange As Range
    Dim insertCell As Range
    Dim searchTerm As String
    
    Set headerRange = Sheet1.Range("A5:I5")
    Set insertCell = headerRange.Cells(2, 1)
    
    Application.CutCopyMode = False
    searchTerm = CStr(insertCell.Value)
    
    With headerRange.Cells(1, 1).EntireColumn
    
        Do Until searchTerm = vbNullString
                With .Cells(Application.Match(searchTerm, .Cells, 0), 1).Resize(Application.CountIf(.Cells, searchTerm), 1)
                    If Application.CutCopyMode = xlCopy Then
                        .Resize(1, headerRange.Columns.Count).Insert
                        [COLOR="Red"].Offset(-1, 0).Resize(1, headerRange.Columns.Count).Insert[/COLOR]
                    End If
                    Set insertCell = .Cells(.Rows.Count + 1, 1)
                    searchTerm = CStr(insertCell.Value)
                    headerRange.Copy
                End With
        Loop
        
    End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,225,151
Messages
6,183,197
Members
453,151
Latest member
Lizamaison

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