Hello all !
I am trying to automate the following process (check out the example below) :
- My data is provided in columns like the top table in the example (4 columns: Q1 - Q4).
- I want to translate this format to the one below to use as a pivot table source.
The macro would need to adapt itself to function with varying numbers of columns (there might be any number between 2 and 6 I'd say), and with columns of any size (usually between 5'000 and 10'000 rows). However I'm happy to fix the position of the columns (their top anyway) on the worksheet - that's absolutely ok since the macro will only be used on a specific worksheet.
If it makes any difference, we're using 2007.
I've tried doing it myself, but I'm an absolute beginner in VBA and I'm not comfortable at all with loops, and with data ranges of varying sizes. So could one of you point me in the right direction ? I've tried looking in the forum, but really didn't know how to search for this...
Thanks in advance !
<b>Example</b><br /><br /><table border="1" cellspacing="0" cellpadding="0" style="font-family:Calibri,Arial; font-size:11pt; background-color:#ffffff; padding-left:2pt; padding-right:2pt; "> <colgroup><col style="font-weight:bold; width:30px; " /><col style="width:64px;" /><col style="width:64px;" /><col style="width:64px;" /><col style="width:64px;" /><col style="width:64px;" /></colgroup><tr style="background-color:#cacaca; text-align:center; font-weight:bold; font-size:8pt; "><td >*</td><td >A</td><td >B</td><td >C</td><td >D</td><td >E</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >1</td><td >Country</td><td >Q1</td><td >Q2</td><td >Q3</td><td >Q4</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >2</td><td >France</td><td style="text-align:right; ">100</td><td style="text-align:right; ">110</td><td style="text-align:right; ">130</td><td style="text-align:right; ">120</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >3</td><td >UK</td><td style="text-align:right; ">180</td><td style="text-align:right; ">160</td><td style="text-align:right; ">220</td><td style="text-align:right; ">190</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >4</td><td >Germany</td><td style="text-align:right; ">110</td><td style="text-align:right; ">130</td><td style="text-align:right; ">170</td><td style="text-align:right; ">140</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >5</td><td >Italy</td><td style="text-align:right; ">90</td><td style="text-align:right; ">110</td><td style="text-align:right; ">120</td><td style="text-align:right; ">110</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >6</td><td >*</td><td >*</td><td >*</td><td >*</td><td >*</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >7</td><td >Country</td><td >Period</td><td >Amount</td><td >*</td><td >*</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >8</td><td >France</td><td style="text-align:center; ">Q1</td><td style="text-align:center; ">100</td><td >*</td><td >*</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >9</td><td >UK</td><td style="text-align:center; ">Q1</td><td style="text-align:center; ">180</td><td >*</td><td >*</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >10</td><td >Germany</td><td style="text-align:center; ">Q1</td><td style="text-align:center; ">110</td><td >*</td><td >*</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >11</td><td >Italy</td><td style="text-align:center; ">Q1</td><td style="text-align:center; ">90</td><td >*</td><td >*</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >12</td><td >France</td><td style="text-align:center; ">Q2</td><td style="text-align:center; ">110</td><td >*</td><td >*</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >13</td><td >UK</td><td style="text-align:center; ">Q2</td><td style="text-align:center; ">160</td><td >*</td><td >*</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >14</td><td >Germany</td><td style="text-align:center; ">Q2</td><td style="text-align:center; ">130</td><td >*</td><td >*</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >15</td><td >Italy</td><td style="text-align:center; ">Q2</td><td style="text-align:center; ">110</td><td >*</td><td >*</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >16</td><td >France</td><td style="text-align:center; ">Q3</td><td style="text-align:center; ">130</td><td >*</td><td >*</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >17</td><td >UK</td><td style="text-align:center; ">Q3</td><td style="text-align:center; ">220</td><td >*</td><td >*</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >18</td><td >Germany</td><td style="text-align:center; ">Q3</td><td style="text-align:center; ">170</td><td >*</td><td >*</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >19</td><td >Italy</td><td style="text-align:center; ">Q3</td><td style="text-align:center; ">120</td><td >*</td><td >*</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >20</td><td >France</td><td style="text-align:center; ">Q4</td><td style="text-align:center; ">120</td><td >*</td><td >*</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >21</td><td >UK</td><td style="text-align:center; ">Q4</td><td style="text-align:center; ">190</td><td >*</td><td >*</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >22</td><td >Germany</td><td style="text-align:center; ">Q4</td><td style="text-align:center; ">140</td><td >*</td><td >*</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >23</td><td >Italy</td><td style="text-align:center; ">Q4</td><td style="text-align:center; ">110</td><td >*</td><td >*</td></tr></table>
I am trying to automate the following process (check out the example below) :
- My data is provided in columns like the top table in the example (4 columns: Q1 - Q4).
- I want to translate this format to the one below to use as a pivot table source.
The macro would need to adapt itself to function with varying numbers of columns (there might be any number between 2 and 6 I'd say), and with columns of any size (usually between 5'000 and 10'000 rows). However I'm happy to fix the position of the columns (their top anyway) on the worksheet - that's absolutely ok since the macro will only be used on a specific worksheet.
If it makes any difference, we're using 2007.
I've tried doing it myself, but I'm an absolute beginner in VBA and I'm not comfortable at all with loops, and with data ranges of varying sizes. So could one of you point me in the right direction ? I've tried looking in the forum, but really didn't know how to search for this...
Thanks in advance !
<b>Example</b><br /><br /><table border="1" cellspacing="0" cellpadding="0" style="font-family:Calibri,Arial; font-size:11pt; background-color:#ffffff; padding-left:2pt; padding-right:2pt; "> <colgroup><col style="font-weight:bold; width:30px; " /><col style="width:64px;" /><col style="width:64px;" /><col style="width:64px;" /><col style="width:64px;" /><col style="width:64px;" /></colgroup><tr style="background-color:#cacaca; text-align:center; font-weight:bold; font-size:8pt; "><td >*</td><td >A</td><td >B</td><td >C</td><td >D</td><td >E</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >1</td><td >Country</td><td >Q1</td><td >Q2</td><td >Q3</td><td >Q4</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >2</td><td >France</td><td style="text-align:right; ">100</td><td style="text-align:right; ">110</td><td style="text-align:right; ">130</td><td style="text-align:right; ">120</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >3</td><td >UK</td><td style="text-align:right; ">180</td><td style="text-align:right; ">160</td><td style="text-align:right; ">220</td><td style="text-align:right; ">190</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >4</td><td >Germany</td><td style="text-align:right; ">110</td><td style="text-align:right; ">130</td><td style="text-align:right; ">170</td><td style="text-align:right; ">140</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >5</td><td >Italy</td><td style="text-align:right; ">90</td><td style="text-align:right; ">110</td><td style="text-align:right; ">120</td><td style="text-align:right; ">110</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >6</td><td >*</td><td >*</td><td >*</td><td >*</td><td >*</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >7</td><td >Country</td><td >Period</td><td >Amount</td><td >*</td><td >*</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >8</td><td >France</td><td style="text-align:center; ">Q1</td><td style="text-align:center; ">100</td><td >*</td><td >*</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >9</td><td >UK</td><td style="text-align:center; ">Q1</td><td style="text-align:center; ">180</td><td >*</td><td >*</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >10</td><td >Germany</td><td style="text-align:center; ">Q1</td><td style="text-align:center; ">110</td><td >*</td><td >*</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >11</td><td >Italy</td><td style="text-align:center; ">Q1</td><td style="text-align:center; ">90</td><td >*</td><td >*</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >12</td><td >France</td><td style="text-align:center; ">Q2</td><td style="text-align:center; ">110</td><td >*</td><td >*</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >13</td><td >UK</td><td style="text-align:center; ">Q2</td><td style="text-align:center; ">160</td><td >*</td><td >*</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >14</td><td >Germany</td><td style="text-align:center; ">Q2</td><td style="text-align:center; ">130</td><td >*</td><td >*</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >15</td><td >Italy</td><td style="text-align:center; ">Q2</td><td style="text-align:center; ">110</td><td >*</td><td >*</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >16</td><td >France</td><td style="text-align:center; ">Q3</td><td style="text-align:center; ">130</td><td >*</td><td >*</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >17</td><td >UK</td><td style="text-align:center; ">Q3</td><td style="text-align:center; ">220</td><td >*</td><td >*</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >18</td><td >Germany</td><td style="text-align:center; ">Q3</td><td style="text-align:center; ">170</td><td >*</td><td >*</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >19</td><td >Italy</td><td style="text-align:center; ">Q3</td><td style="text-align:center; ">120</td><td >*</td><td >*</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >20</td><td >France</td><td style="text-align:center; ">Q4</td><td style="text-align:center; ">120</td><td >*</td><td >*</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >21</td><td >UK</td><td style="text-align:center; ">Q4</td><td style="text-align:center; ">190</td><td >*</td><td >*</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >22</td><td >Germany</td><td style="text-align:center; ">Q4</td><td style="text-align:center; ">140</td><td >*</td><td >*</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >23</td><td >Italy</td><td style="text-align:center; ">Q4</td><td style="text-align:center; ">110</td><td >*</td><td >*</td></tr></table>