简体   繁体   中英

Excel vba - rows to columns and keeping first columns

I am doing a plan for an year, where I have the labels in the first 3 columns and then the values for the 12 months in the next columns. I want to put the values one under another for the 12 months and to repeat the labels for each month then to go on the next record. I believe there should be a loop for it, but I am not sure how to make it.

Screenshot

My data looks like the one in table A on the screenshot I have to make it to look like table B on the screenshot

In order to move/copy the values of a row in a column you can use Transpose function like in my next code example:

   Dim rng1 As Range, rng2 As Range, arr1 As Variant, arr2 As Variant
   Set rng1 = Range("A1:H10"): Set rng2 = Range("J1:J8")
   arr1 = rng1.value
   arr2 = Application.WorksheetFunction.Transpose(arr1)
   rng2.value = arr2

The code can be optimized, of course, in order to automatically determine the columns number of the row to be transposed and build the column range based of it. I would like to receive a little feedback confirming (or not) that this is what you really need. At least, this is what I could suppose looking to your attached pictures... The sample code will transpose the content of the (first) row ( Range("A1:H10") ) to the column J:J ( Range("J1:J8") ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM