Isabella,
Sample raw data:
Excel Workbook |
---|
|
---|
| A | B | C | D | E | F | G | H | I | J | K |
---|
4 | PfCode | Security | * * * * * Short Name | ReportGroup | Category&Subtype | IDNumber | TransShortName | BrokerCode | *ContractDate | *SettlementDate | * * * *TransUnits |
---|
5 | RSTAHY | SMPJPD15 | SMP JP 061215 CC * * | 85 | DS04 | 157957 | DS-PUR | JPMAUD | 22/12/2010 | 22/12/2010 | -3,500,000.00 |
---|
6 | RSTAHY | SMPJPD15 | SMP JP 061215 CC * * | 85 | DS04 | 158305 | DS-PUR | JPMAUD | 23/12/2010 | 23/12/2010 | -440,000.00 |
---|
7 | RSTBFA | SMPJPD15 | SMP JP 061215 CC * * | 85 | DS04 | 157959 | DS-PUR | JPMAUD | 22/12/2010 | 22/12/2010 | -3,150,000.00 |
---|
8 | RSTBFA | SMPJPD15 | SMP JP 061215 CC * * | 85 | DS04 | 158304 | DS-PUR | JPMAUD | 23/12/2010 | 23/12/2010 | -480,000.00 |
---|
9 | RTFDBG | SMPJPD15 | SMP JP 061215 CC * * | 85 | DS04 | 157958 | DS-PUR | JPMAUD | 22/12/2010 | 22/12/2010 | -770,000.00 |
---|
10 | RTFDBG | SMPJPD15 | SMP JP 061215 CC * * | 85 | DS04 | 160168 | DS-PUR | JPMSYD | 30/12/2010 | 30/12/2010 | -280,000.00 |
---|
11 | RTFDBG | SMPJPD15 | SMP JP 061215 CC * * | 85 | DS04 | 160457 | INT * | INT * | 31/12/2010 | 31/12/2010 | 0.00 |
---|
12 | RTEYFG | SMPUB915 | SMP UBS 092015 CC * | 85 | DS04 | 150708 | DS-PUR | UBSAUD | 3/12/2010 | 3/12/2010 | -380,000.00 |
---|
13 | RTEYFG | SMPUB915 | SMP UBS 092015 CC * | 85 | DS04 | 151121 | DS-PUR | UBSAUD | 6/12/2010 | 6/12/2010 | -440,000.00 |
---|
14 | RTEYFG | SMPUB915 | SMP UBS 092015 CC * | 85 | DS04 | 152649 | DS-PUR | UBSAUD | 8/12/2010 | 8/12/2010 | -520,000.00 |
---|
15 | RTFDBG | SMPUB915 | SMP UBS 092015 CC * | 85 | DS04 | 154816 | DS-PUR | UBSAUD | 14/12/2010 | 14/12/2010 | -260,000.00 |
---|
16 | RTFDBG | SMPUB915 | SMP UBS 092015 CC * | 85 | DS04 | 155882 | DS-PUR | UBSAUD | 16/12/2010 | 16/12/2010 | -570,000.00 |
---|
17 | RTFDBG | SMPUB915 | SMP UBS 092015 CC * | 85 | DS04 | 158291 | DS-PUR | UBSAUD | 23/12/2010 | 23/12/2010 | -490,000.00 |
---|
18 | RTFDBG | SMPUB915 | SMP UBS 092015 CC * | 85 | DS04 | 159263 | DS-PUR | UBSAUD | 29/12/2010 | 29/12/2010 | -520,000.00 |
---|
19 | RSTBFA | SMPUB915 | SMP UBS 092015 CC * | 85 | DS04 | 160460 | INT * | INT * | 31/12/2010 | 31/12/2010 | 0.00 |
---|
20 | RSTBFA | SMPUB915 | SMP UBS 092015 CC * | 85 | DS04 | 160656 | DS-PUR | UBSAUD | 31/12/2010 | 31/12/2010 | -320,000.00 |
---|
21 | | | | | | | | | | | |
---|
|
---|
After the macro:
Excel Workbook |
---|
|
---|
| A | B | C | D | E | F | G | H | I | J | K |
---|
5 | PfCode | Security | * * * * * Short Name | ReportGroup | Category&Subtype | IDNumber | TransShortName | BrokerCode | *ContractDate | *SettlementDate | * * * *TransUnits |
---|
6 | RSTAHY | SMPJPD15 | SMP JP 061215 CC * * | 85 | DS04 | 157957 | DS-PUR | JPMAUD | 22/12/2010 | 22/12/2010 | -3,500,000.00 |
---|
7 | RSTAHY | SMPJPD15 | SMP JP 061215 CC * * | 85 | DS04 | 158305 | DS-PUR | JPMAUD | 23/12/2010 | 23/12/2010 | -440,000.00 |
---|
8 | | | | | | | | | | | |
---|
9 | RSTBFA | SMPJPD15 | SMP JP 061215 CC * * | 85 | DS04 | 157959 | DS-PUR | JPMAUD | 22/12/2010 | 22/12/2010 | -3,150,000.00 |
---|
10 | RSTBFA | SMPJPD15 | SMP JP 061215 CC * * | 85 | DS04 | 158304 | DS-PUR | JPMAUD | 23/12/2010 | 23/12/2010 | -480,000.00 |
---|
11 | | | | | | | | | | | |
---|
12 | RTFDBG | SMPJPD15 | SMP JP 061215 CC * * | 85 | DS04 | 157958 | DS-PUR | JPMAUD | 22/12/2010 | 22/12/2010 | -770,000.00 |
---|
13 | RTFDBG | SMPJPD15 | SMP JP 061215 CC * * | 85 | DS04 | 160168 | DS-PUR | JPMSYD | 30/12/2010 | 30/12/2010 | -280,000.00 |
---|
14 | RTFDBG | SMPJPD15 | SMP JP 061215 CC * * | 85 | DS04 | 160457 | INT * | INT * | 31/12/2010 | 31/12/2010 | 0.00 |
---|
15 | | | | | | | | | | | |
---|
|
---|
Excel Workbook |
---|
|
---|
| A | B | C | D | E | F | G | H | I | J | K |
---|
5 | PfCode | Security | * * * * * Short Name | ReportGroup | Category&Subtype | IDNumber | TransShortName | BrokerCode | *ContractDate | *SettlementDate | * * * *TransUnits |
---|
6 | RTEYFG | SMPUB915 | SMP UBS 092015 CC * | 85 | DS04 | 150708 | DS-PUR | UBSAUD | 3/12/2010 | 3/12/2010 | -380,000.00 |
---|
7 | RTEYFG | SMPUB915 | SMP UBS 092015 CC * | 85 | DS04 | 151121 | DS-PUR | UBSAUD | 6/12/2010 | 6/12/2010 | -440,000.00 |
---|
8 | RTEYFG | SMPUB915 | SMP UBS 092015 CC * | 85 | DS04 | 152649 | DS-PUR | UBSAUD | 8/12/2010 | 8/12/2010 | -520,000.00 |
---|
9 | | | | | | | | | | | |
---|
10 | RTFDBG | SMPUB915 | SMP UBS 092015 CC * | 85 | DS04 | 154816 | DS-PUR | UBSAUD | 14/12/2010 | 14/12/2010 | -260,000.00 |
---|
11 | RTFDBG | SMPUB915 | SMP UBS 092015 CC * | 85 | DS04 | 155882 | DS-PUR | UBSAUD | 16/12/2010 | 16/12/2010 | -570,000.00 |
---|
12 | RTFDBG | SMPUB915 | SMP UBS 092015 CC * | 85 | DS04 | 158291 | DS-PUR | UBSAUD | 23/12/2010 | 23/12/2010 | -490,000.00 |
---|
13 | RTFDBG | SMPUB915 | SMP UBS 092015 CC * | 85 | DS04 | 159263 | DS-PUR | UBSAUD | 29/12/2010 | 29/12/2010 | -520,000.00 |
---|
14 | | | | | | | | | | | |
---|
15 | RSTBFA | SMPUB915 | SMP UBS 092015 CC * | 85 | DS04 | 160460 | INT * | INT * | 31/12/2010 | 31/12/2010 | 0.00 |
---|
16 | RSTBFA | SMPUB915 | SMP UBS 092015 CC * | 85 | DS04 | 160656 | DS-PUR | UBSAUD | 31/12/2010 | 31/12/2010 | -320,000.00 |
---|
17 | | | | | | | | | | | |
---|
|
---|
Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).
1. Copy the below code, by highlighting the code and pressing the keys
CTRL +
C
2. Open your workbook
3. Press the keys
ALT +
F11 to open the Visual Basic Editor
4. Press the keys
ALT +
I to activate the Insert menu
5. Press
M to insert a Standard Module
6. Where the cursor is flashing, paste the code by pressing the keys
CTRL +
V
7. Press the keys
ALT +
Q to exit the Editor, and return to Excel
8. To run the macro from Excel, open the workbook, and press
ALT +
F8 to display the Run Macro Dialog. Double Click the macro's name to Run it.
Code:
Option Explicit
Sub DistributeRows()
' hiker95, 03/04/2011
' http://www.mrexcel.com/forum/showthread.php?t=533700
' Original code by Norie
' http://www.mrexcel.com/forum/showthread.php?t=315083
Dim wsAll As Worksheet, wsCrit As Worksheet, wsNew As Worksheet
Dim LastRow As Long, LR As Long, a As Long, LastRowCrit As Long, I As Long
Application.ScreenUpdating = False
Set wsAll = Worksheets("RawData")
LastRow = wsAll.Range("B" & Rows.Count).End(xlUp).Row
Set wsCrit = Worksheets.Add
wsAll.Range("B4:B" & LastRow).AdvancedFilter Action:=xlFilterCopy, CopyToRange:=wsCrit.Range("A1"), Unique:=True
LastRowCrit = wsCrit.Range("A" & Rows.Count).End(xlUp).Row
For I = 2 To LastRowCrit
Set wsNew = Worksheets.Add(After:=Worksheets(Worksheets.Count))
wsNew.Name = wsCrit.Range("A2")
wsAll.Rows("1:" & LastRow).AdvancedFilter Action:=xlFilterCopy, CriteriaRange:=wsCrit.Range("A1:A2"), CopyToRange:=wsNew.Range("A1"), Unique:=False
wsNew.UsedRange.Columns.AutoFit
LR = wsNew.Cells(Rows.Count, 1).End(xlUp).Row
For a = LR To 3 Step -1
If wsNew.Cells(a, 1) <> wsNew.Cells(a - 1, 1) Then
wsNew.Rows(a).Insert
End If
Next a
wsNew.Rows(1).Resize(4).Insert
wsCrit.Rows(2).Delete
Next I
Application.DisplayAlerts = False
wsCrit.Delete
Application.DisplayAlerts = True
wsAll.Activate
Application.ScreenUpdating = True
End Sub
Then run the DistributeRows macro.