Copy data from rows to columns based on criteria

KevNewton29

New Member
Joined
Jun 23, 2004
Messages
9
I recently posted a thread whereby I have a listy of 15,500 entries of policy numbers and the funds invested in these policies. However each seperate fund had a seperate row on Excel (i.e. a policy with four funds had four rows on Excel). What I have been trying to do is have one row with multiple columns for the funds. So far I have code (see below) that does this apart form cases where I only have one fund

Sub test()

Dim rng As Range, r As Long, c As Long, i As Long

' Puts results on sheet2

r = 1
c = 2

With Sheet2
.UsedRange.Clear
.Range("A1:B1").Value = [{"Plan No","Plan No2"}]
End With

Sheet1.Activate
' Change column in following lines if J used for something
Columns("J").Clear
Range("A1", Range("A1").End(xlDown)).AdvancedFilter Action:=xlFilterCopy, CopyToRange:=Range("J1"), Unique:=True
For Each rng In Range("J2", Range("J2").End(xlDown))
Sheet2.Cells(r + 1, 1) = rng
r = r + 1
Next rng

r = 1

For Each rng In Range("A2", Range("A2").End(xlDown))
If rng = rng.Offset(-1) Then
rng.Offset(, 1).Resize(, 2).Copy Sheet2.Cells(r + 1, c)
c = c + 2
Else
r = r + 1
c = 2
End If
Next rng
Columns("J").Clear

End Sub

I then tried to improve on this to look at only one policy line with the code belwo but it doesn't work. Any ideas?

Sub test2()

Dim rng As Range, r As Long, c As Long, i As Long

' Puts results on sheet2

r = 1
c = 2

With Sheet2
.UsedRange.Clear
.Range("A1:B1").Value = [{"Plan No","Plan No2"}]
End With

Sheet1.Activate
' Change column in following lines if J used for something
Columns("J").Clear
Range("A1", Range("A1").End(xlDown)).AdvancedFilter Action:=xlFilterCopy, CopyToRange:=Range("J1"), Unique:=True
For Each rng In Range("J2", Range("J2").End(xlDown))
Sheet2.Cells(r + 1, 1) = rng
r = r + 1
Next rng

r = 1

For Each rng In Range("A2", Range("A2").End(xlDown))
If rng = rng.Offset(-1) Or rng.Offset(1) Then
rng.Offset(, 1).Resize(, 2).Copy Sheet2.Cells(r + 1, c)
c = c + 2
Else
If rng <> rng.Offset(-1) And rng.Offset(1) Then
rng.Offset(, 1).Resize(, 2).Copy Sheet2.Cells(r + 1, c)
Else
r = r + 1
c = 2
End If
End If
Next rng
Columns("J").Clear

End Sub
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.

Forum statistics

Threads
1,223,162
Messages
6,170,431
Members
452,326
Latest member
johnshaji

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