Moving Data From One Worksheet to Another Worksheet using VBA

lannok

New Member
Joined
Sep 16, 2014
Messages
1
Hello - I'm attempting to convert column data to row data. I currently have 16 columns and I would like to shrink this down to 6 columns. I have about 20 hours experience working with VBA and I'm stuck. Any guidance would be most appreciated.

The current headers and layout is as follows:

abcdefghijklmnopq
1Unique IDZ1 MKT TTC 25%ileZ1 MKT TTC 50%ileZ1 MKT TTC 75%ileZ2 MKT TTC 25%ileZ2 MKT TTC 50%ileZ2 MKT TTC 75%ileZ3 MKT TTC 25%ileZ3 MKT TTC 50%ileZ3 MKT TTC 75%ileZ4 MKT TTC 25%ileZ4 MKT TTC 50%ileZ4 MKT TTC 75%ileCPR Low - $CPR Mid - $CPR High - $AVG TTC
2SW1144:UK:IIG:057032000000000000507510078
3SW1144:USA:IIG:054838234567891011121314151618
4SW1213:UK:IIG:04668700010111200000020405045
No Limit to the numbe of rows

<tbody>
</tbody>


Create VBA to organize data as follows:

abcdef
1Unique IDMkt TTC %ileMkt TTC ValueCPR: Low/Mid/HighCPR Value2014 TTC Value
2SW1144:UK:IIG:057032Z1 MKT TTC 25%ile0Low5078
3SW1144:UK:IIG:057032Z1 MKT TTC 50%ile0Mid7578
4SW1144:UK:IIG:057032Z1 MKT TTC 75%ile0High10078
5SW1144:UK:IIG:057032Z2 MKT TTC 25%ile0Low5078
6SW1144:UK:IIG:057032Z2 MKT TTC 50%ile0Mid7578
7SW1144:UK:IIG:057032Z2 MKT TTC 75%ile0High10078
8SW1144:UK:IIG:057032Z3 MKT TTC 25%ile0Low5078
9SW1144:UK:IIG:057032Z3 MKT TTC 50%ile0Mid7578
10SW1144:UK:IIG:057032Z3 MKT TTC 75%ile0High10078
11SW1144:UK:IIG:057032Z4 MKT TTC 25%ile0Low5078
12SW1144:UK:IIG:057032Z4 MKT TTC 50%ile0Mid7578
13SW1144:UK:IIG:057032Z4 MKT TTC 75%ile0High10078
14SW1144:UK:IIG:057032Same as aboveSometimes 0 and sometimes >0 both are okSame as aboveSame as aboveSame as above
3SW1144:USA:IIG:054838Same as aboveSometimes 0 and sometimes >0 both are okSame as aboveSame as aboveSame as above
4SW1213:UK:IIG:046687Same as aboveSometimes 0 and sometimes >0 both are okSame as aboveSame as aboveSame as above
No Limit to number of rows.No Limit to number of rows.No Limit to number of rows.No Limit to number of rows.No Limit to number of rows.No Limit to number of rows.No Limit to number of rows.

<tbody>
</tbody>


Current VBA code:

Sub CreateDataWithNewSheet()
Dim CountRows As Double
Dim CountColumns As Integer
Dim data As Variant
CountRows = ActiveSheet.UsedRange.Rows.Count
CountColumns = ActiveSheet.UsedRange.Columns.Count

data = Range(Cells(1, 1), Cells(CountRows, CountColumns))

Dim newSheet As Worksheet
Set newSheet = Sheets.Add

newSheet.Cells(1, 1).Value = "Unique ID"
newSheet.Cells(1, 2).Value = "Mkt TTC %ile"
newSheet.Cells(1, 3).Value = "Mkt TTC Value"
newSheet.Cells(1, 4).Value = "CPR: Low/Mid/High"
newSheet.Cells(1, 5).Value = "CPR: Value"
Dim StartRowPosition As Double
StartRowPosition = 2

Dim row_in_mainsheet As Double
row_in_mainsheet = 2
Dim column_in_mainsheet As Integer

Do While True

column_in_mainsheet = 2
Do While True

'Unique ID
newSheet.Cells(StartRowPosition, 1).Value = data(row_in_mainsheet, 1)

'Mkt TTC %ile
newSheet.Cells(StartRowPosition, 2).Value = data(1, column_in_mainsheet)

'Mkt TTC Value
newSheet.Cells(StartRowPosition, 3).Value = data(row_in_mainsheet, column_in_mainsheet)

'CPR: Low/Mid/High
newSheet.Cells(StartRowPosition, 4).Value = data(1, column_in_mainsheet)

'Mkt TTC Value
newSheet.Cells(StartRowPosition, 5).Value = data(row_in_mainsheet, column_in_mainsheet)


StartRowPosition = StartRowPosition + 1
If column_in_mainsheet = CountColumns Then Exit Do
column_in_mainsheet = column_in_mainsheet + 1
Loop

If row_in_mainsheet = CountRows Then Exit Do
row_in_mainsheet = row_in_mainsheet + 1
Loop


End Sub

The VBA code produces the following results:

Unique IDMkt TTC %ileMkt TTC ValueCPR: Low/Mid/HighCPR: Value
SW1144:UK:IIG:057032Z1 MKT TTC 25%ile0Z1 MKT TTC 25%ile0
SW1144:UK:IIG:057032Z1 MKT TTC 50%ile0Z1 MKT TTC 50%ile0
SW1144:UK:IIG:057032Z1 MKT TTC 75%ile0Z1 MKT TTC 75%ile0
SW1144:UK:IIG:057032Z2 MKT TTC 25%ile0Z2 MKT TTC 25%ile0
SW1144:UK:IIG:057032Z2 MKT TTC 50%ile0Z2 MKT TTC 50%ile0
SW1144:UK:IIG:057032Z2 MKT TTC 75%ile0Z2 MKT TTC 75%ile0
SW1144:UK:IIG:057032Z3 MKT TTC 25%ile0Z3 MKT TTC 25%ile0
SW1144:UK:IIG:057032Z3 MKT TTC 50%ile0Z3 MKT TTC 50%ile0
SW1144:UK:IIG:057032Z3 MKT TTC 75%ile0Z3 MKT TTC 75%ile0
SW1144:UK:IIG:057032Z4 MKT TTC 25%ile0Z4 MKT TTC 25%ile0
SW1144:UK:IIG:057032Z4 MKT TTC 50%ile0Z4 MKT TTC 50%ile0
SW1144:UK:IIG:057032Z4 MKT TTC 75%ile0Z4 MKT TTC 75%ile0
SW1144:UK:IIG:057032CPR Low - $50CPR Low - $50
SW1144:UK:IIG:057032CPR Mid - $75CPR Mid - $75
SW1144:UK:IIG:057032CPR High - $100CPR High - $100

<tbody>
</tbody>

Thanks in advance for any suggestions.

Thanks,
Ken
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.

Forum statistics

Threads
1,215,433
Messages
6,124,861
Members
449,194
Latest member
HellScout

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