Transpose VBA Code

rsmith80

New Member
Joined
Jun 25, 2010
Messages
7
Hello all,

Is there code or some trick that can convert excel data that has various amounts of columns into more of a "database" look. Below is what the current data looks like:

<table border="0" cellpadding="0" cellspacing="0" width="919"><col style="width: 89pt;" width="119"> <col style="width: 75pt;" width="100"> <col style="width: 69pt;" width="92"> <col style="width: 66pt;" width="88"> <col style="width: 57pt;" span="3" width="76"> <col style="width: 55pt;" span="4" width="73"> <tbody><tr style="height: 15pt;" height="20"> <td class="xl69" style="height: 15pt; width: 89pt;" height="20" width="119">Destination</td> <td class="xl69" style="border-left: medium none; width: 75pt;" width="100">Carrier</td> <td class="xl70" style="border-left: medium none; width: 69pt;" width="92">Country Code</td> <td class="xl70" style="border-left: medium none; width: 66pt;" width="88">PRICE</td> <td class="xl71" style="border-left: medium none; width: 57pt;" width="76">City Code 1</td> <td class="xl71" style="border-left: medium none; width: 57pt;" width="76">City Code 2</td> <td class="xl71" style="border-left: medium none; width: 57pt;" width="76">City Code 3</td> <td class="xl71" style="border-left: medium none; width: 55pt;" width="73">City Code 4</td> <td class="xl71" style="border-left: medium none; width: 55pt;" width="73">City Code 5</td> <td class="xl71" style="border-left: medium none; width: 55pt;" width="73">City Code 6</td> <td class="xl71" style="border-left: medium none; width: 55pt;" width="73">City Code 7</td> </tr> <tr style="height: 15pt;" height="20"> <td class="xl66" style="height: 15pt; border-top: medium none;" height="20">UNITED KINGDOM</td> <td class="xl66" style="border-top: medium none; border-left: medium none;">Orange Mobile</td> <td class="xl66" style="border-top: medium none; border-left: medium none;">44</td> <td class="xl67" style="border-top: medium none; border-left: medium none;"> $ 0.05 </td> <td class="xl68" style="border-top: medium none; border-left: medium none;">A</td> <td class="xl65" style="border-top: medium none; border-left: medium none;">B</td> <td class="xl65" style="border-top: medium none; border-left: medium none;">C</td> <td class="xl65" style="border-top: medium none; border-left: medium none;">D</td> <td class="xl65" style="border-top: medium none; border-left: medium none;">E</td> <td class="xl65" style="border-top: medium none; border-left: medium none;">F</td> <td class="xl65" style="border-top: medium none; border-left: medium none;">G</td> </tr> <tr style="height: 15pt;" height="20"> <td class="xl66" style="height: 15pt; border-top: medium none;" height="20">UNITED KINGDOM</td> <td class="xl66" style="border-top: medium none; border-left: medium none;">T-Mobile</td> <td class="xl66" style="border-top: medium none; border-left: medium none;">44</td> <td class="xl67" style="border-top: medium none; border-left: medium none;"> $ 0.03 </td> <td class="xl68" style="border-top: medium none; border-left: medium none;">R</td> <td class="xl65" style="border-top: medium none; border-left: medium none;">S</td> <td class="xl65" style="border-top: medium none; border-left: medium none;">T</td> <td class="xl65" style="border-top: medium none; border-left: medium none;"> </td> <td class="xl65" style="border-top: medium none; border-left: medium none;"> </td> <td class="xl65" style="border-top: medium none; border-left: medium none;"> </td> <td class="xl65" style="border-top: medium none; border-left: medium none;"> </td> </tr> <tr style="height: 15pt;" height="20"> <td class="xl66" style="height: 15pt; border-top: medium none;" height="20">UNITED KINGDOM</td> <td class="xl66" style="border-top: medium none; border-left: medium none;">O2</td> <td class="xl66" style="border-top: medium none; border-left: medium none;">44</td> <td class="xl67" style="border-top: medium none; border-left: medium none;"> $ 0.02 </td> <td class="xl68" style="border-top: medium none; border-left: medium none;">L</td> <td class="xl65" style="border-top: medium none; border-left: medium none;">M</td> <td class="xl65" style="border-top: medium none; border-left: medium none;">N</td> <td class="xl65" style="border-top: medium none; border-left: medium none;">O</td> <td class="xl65" style="border-top: medium none; border-left: medium none;"> </td> <td class="xl65" style="border-top: medium none; border-left: medium none;"> </td> <td class="xl65" style="border-top: medium none; border-left: medium none;"> </td> </tr> </tbody></table>

I'm trying to get it too look like:

<table border="0" cellpadding="0" cellspacing="0" width="475"><col style="width: 89pt;" width="119"> <col style="width: 75pt;" width="100"> <col style="width: 69pt;" width="92"> <col style="width: 66pt;" width="88"> <col style="width: 57pt;" width="76"> <tbody><tr style="height: 15pt;" height="20"> <td class="xl68" style="height: 15pt; width: 89pt;" height="20" width="119">Destination</td> <td class="xl68" style="border-left: medium none; width: 75pt;" width="100">Carrier</td> <td class="xl69" style="border-left: medium none; width: 69pt;" width="92">Country Code</td> <td class="xl69" style="border-left: medium none; width: 66pt;" width="88">City Code</td> <td class="xl69" style="border-left: medium none; width: 57pt;" width="76">Price</td> </tr> <tr style="height: 15pt;" height="20"> <td class="xl66" style="height: 15pt; border-top: medium none;" height="20">UNITED KINGDOM</td> <td class="xl66" style="border-top: medium none; border-left: medium none;">Orange Mobile</td> <td class="xl66" style="border-top: medium none; border-left: medium none;">44</td> <td class="xl70" style="border-top: medium none; border-left: medium none;">A</td> <td class="xl67" style="border-top: medium none; border-left: medium none;"> $ 0.05 </td> </tr> <tr style="height: 15pt;" height="20"> <td class="xl66" style="height: 15pt; border-top: medium none;" height="20">UNITED KINGDOM</td> <td class="xl66" style="border-top: medium none; border-left: medium none;">Orange Mobile</td> <td class="xl66" style="border-top: medium none; border-left: medium none;">44</td> <td class="xl65" style="border-top: medium none; border-left: medium none;">B</td> <td class="xl67" style="border-top: medium none; border-left: medium none;"> $ 0.05 </td> </tr> <tr style="height: 15pt;" height="20"> <td class="xl66" style="height: 15pt; border-top: medium none;" height="20">UNITED KINGDOM</td> <td class="xl66" style="border-top: medium none; border-left: medium none;">Orange Mobile</td> <td class="xl66" style="border-top: medium none; border-left: medium none;">44</td> <td class="xl65" style="border-top: medium none; border-left: medium none;">C</td> <td class="xl67" style="border-top: medium none; border-left: medium none;"> $ 0.05 </td> </tr> <tr style="height: 15pt;" height="20"> <td class="xl66" style="height: 15pt; border-top: medium none;" height="20">UNITED KINGDOM</td> <td class="xl66" style="border-top: medium none; border-left: medium none;">Orange Mobile</td> <td class="xl66" style="border-top: medium none; border-left: medium none;">44</td> <td class="xl65" style="border-top: medium none; border-left: medium none;">D</td> <td class="xl67" style="border-top: medium none; border-left: medium none;"> $ 0.05 </td> </tr> <tr style="height: 15pt;" height="20"> <td class="xl66" style="height: 15pt; border-top: medium none;" height="20">UNITED KINGDOM</td> <td class="xl66" style="border-top: medium none; border-left: medium none;">Orange Mobile</td> <td class="xl66" style="border-top: medium none; border-left: medium none;">44</td> <td class="xl65" style="border-top: medium none; border-left: medium none;">E</td> <td class="xl67" style="border-top: medium none; border-left: medium none;"> $ 0.05 </td> </tr> <tr style="height: 15pt;" height="20"> <td class="xl66" style="height: 15pt; border-top: medium none;" height="20">UNITED KINGDOM</td> <td class="xl66" style="border-top: medium none; border-left: medium none;">Orange Mobile</td> <td class="xl66" style="border-top: medium none; border-left: medium none;">44</td> <td class="xl65" style="border-top: medium none; border-left: medium none;">F</td> <td class="xl67" style="border-top: medium none; border-left: medium none;"> $ 0.05 </td> </tr> <tr style="height: 15pt;" height="20"> <td class="xl66" style="height: 15pt; border-top: medium none;" height="20">UNITED KINGDOM</td> <td class="xl66" style="border-top: medium none; border-left: medium none;">Orange Mobile</td> <td class="xl66" style="border-top: medium none; border-left: medium none;">44</td> <td class="xl65" style="border-top: medium none; border-left: medium none;">G</td> <td class="xl67" style="border-top: medium none; border-left: medium none;"> $ 0.05 </td> </tr> <tr style="height: 15pt;" height="20"> <td class="xl66" style="height: 15pt; border-top: medium none;" height="20">UNITED KINGDOM</td> <td class="xl66" style="border-top: medium none; border-left: medium none;">T-Mobile</td> <td class="xl66" style="border-top: medium none; border-left: medium none;">44</td> <td class="xl70" style="border-top: medium none; border-left: medium none;">R</td> <td class="xl67" style="border-top: medium none; border-left: medium none;"> $ 0.03 </td> </tr> <tr style="height: 15pt;" height="20"> <td class="xl66" style="height: 15pt; border-top: medium none;" height="20">UNITED KINGDOM</td> <td class="xl66" style="border-top: medium none; border-left: medium none;">T-Mobile</td> <td class="xl66" style="border-top: medium none; border-left: medium none;">44</td> <td class="xl65" style="border-top: medium none; border-left: medium none;">S</td> <td class="xl67" style="border-top: medium none; border-left: medium none;"> $ 0.03 </td> </tr> <tr style="height: 15pt;" height="20"> <td class="xl66" style="height: 15pt; border-top: medium none;" height="20">UNITED KINGDOM</td> <td class="xl66" style="border-top: medium none; border-left: medium none;">T-Mobile</td> <td class="xl66" style="border-top: medium none; border-left: medium none;">44</td> <td class="xl65" style="border-top: medium none; border-left: medium none;">T</td> <td class="xl67" style="border-top: medium none; border-left: medium none;"> $ 0.03 </td> </tr> <tr style="height: 15pt;" height="20"> <td class="xl66" style="height: 15pt; border-top: medium none;" height="20">UNITED KINGDOM</td> <td class="xl66" style="border-top: medium none; border-left: medium none;">O2</td> <td class="xl66" style="border-top: medium none; border-left: medium none;">44</td> <td class="xl70" style="border-top: medium none; border-left: medium none;">L</td> <td class="xl67" style="border-top: medium none; border-left: medium none;"> $ 0.02 </td> </tr> <tr style="height: 15pt;" height="20"> <td class="xl66" style="height: 15pt; border-top: medium none;" height="20">UNITED KINGDOM</td> <td class="xl66" style="border-top: medium none; border-left: medium none;">O2</td> <td class="xl66" style="border-top: medium none; border-left: medium none;">44</td> <td class="xl65" style="border-top: medium none; border-left: medium none;">M</td> <td class="xl67" style="border-top: medium none; border-left: medium none;"> $ 0.02 </td> </tr> <tr style="height: 15pt;" height="20"> <td class="xl66" style="height: 15pt; border-top: medium none;" height="20">UNITED KINGDOM</td> <td class="xl66" style="border-top: medium none; border-left: medium none;">O2</td> <td class="xl66" style="border-top: medium none; border-left: medium none;">44</td> <td class="xl65" style="border-top: medium none; border-left: medium none;">N</td> <td class="xl67" style="border-top: medium none; border-left: medium none;"> $ 0.02 </td> </tr> <tr style="height: 15pt;" height="20"> <td class="xl66" style="height: 15pt; border-top: medium none;" height="20">UNITED KINGDOM</td> <td class="xl66" style="border-top: medium none; border-left: medium none;">O2</td> <td class="xl66" style="border-top: medium none; border-left: medium none;">44</td> <td class="xl65" style="border-top: medium none; border-left: medium none;">O</td> <td class="xl67" style="border-top: medium none; border-left: medium none;"> $ 0.02 </td> </tr> </tbody></table>
There are over 200+ countries and multiple carriers per country so copy and pasting values (transpose) isn't the best option. Thank you for your help.
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
My original data was on the active sheet in range A1:K4, including the headers, the City Codes were in columns E:K, hence the range $E$2:$K$4 in the code below. If that's not where your data lies, then you'll have some tweaking to do, however the following should get you started:
Code:
Sub blah()
Set Startsht = ActiveSheet
Set NewSht = Sheets.Add
With NewSht
For Each cll In Startsht.Range("$E$2:$K$4").Cells[COLOR=Red] 'adjust this range.[/COLOR]
If Not IsEmpty(cll) Then
DestRow = .Cells(.Rows.Count, 4).End(xlUp).Row + 1
.Cells(DestRow, 1).Resize(, 3).Value = Startsht.Cells(cll.Row, 1).Resize(, 3).Value
.Cells(DestRow, 4).Value = cll.Value
.Cells(DestRow, 5).Value = Startsht.Cells(cll.Row, 4).Value
End If
Next cll
End With
End Sub
It creates a new sheet each time, starts adding data at row 2, but doesn't put any headers in - I'll leave that to you.
 
Last edited:
Upvote 0
…continued (screenshots):
Starting (active) sheet:
Excel Workbook
ABCDEFGHIJK
1DestinationCarrierCountry CodePRICECity Code 1City Code 2City Code 3City Code 4City Code 5City Code 6City Code 7
2UNITED KINGDOMOrange Mobile44$ 0.05ABCDEFG
3UNITED KINGDOMT-Mobile44$ 0.03RST
4UNITED KINGDOMO244$ 0.02LMNO
Sheet1


and result:
Excel Workbook
ABCDE
1
2UNITED KINGDOMOrange Mobile44A0.05
3UNITED KINGDOMOrange Mobile44B0.05
4UNITED KINGDOMOrange Mobile44C0.05
5UNITED KINGDOMOrange Mobile44D0.05
6UNITED KINGDOMOrange Mobile44E0.05
7UNITED KINGDOMOrange Mobile44F0.05
8UNITED KINGDOMOrange Mobile44G0.05
9UNITED KINGDOMT-Mobile44R0.03
10UNITED KINGDOMT-Mobile44S0.03
11UNITED KINGDOMT-Mobile44T0.03
12UNITED KINGDOMO244L0.02
13UNITED KINGDOMO244M0.02
14UNITED KINGDOMO244N0.02
15UNITED KINGDOMO244O0.02
Sheet2
 
Upvote 0

Forum statistics

Threads
1,224,513
Messages
6,179,214
Members
452,895
Latest member
BILLING GUY

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