One of the most important and widely used activities that we have to accomplish while working in Excel is to transpose a data set. Today I’ll show you how you can transpose both one-dimensional and multi-dimensional arrays in Excel using VBA.
Download Practice Workbook
3 Suitable Methods to Transpose in Excel VBA
Here we’ve got a data set with the Names of some products and their Sales in three years of a company called Rainbow Group.
Today our objective is to transpose this data set using VBA in Excel.
1. Transpose One Dimensional Array in Excel VBA
First of all, we’ll try to transpose a one-dimensional array through VBA.
Step 1:
➤ First, press ALT+F11 on your keyboard. The VBA window will open.
Step 2:
➤ Go to the Insert tab in the VBA window.
➤ Then from the options available, select Module.
Step 3:
➤ A new module window called “Module 1” will open.
➤ Insert the following VBA code in the module.
♦ Code:
Sub Transpose()
Dim Destination As String
Destination = InputBox("Enter the Range Where You Want to Keep the Transposed Array: ")
Range(Destination).Value = WorksheetFunction.Transpose(Range(Selection.Address))
End Sub
Step 4:
➤ Save the workbook as Excel Macro-Enabled Workbook.
Step 5:
➤ Return to your workbook and select the one-dimensional array that you want to transpose.
➤ Here I am selecting the Product Name column.
Step 6:
➤ Press ALT+F8 on your keyboard.
➤ A dialogue box called Macro will open. Select Transpose and click on Run.
Step 7:
➤ You will get an input box that will ask you to enter the cell reference of the range where you want to keep the transposed array.
➤ Enter the cell reference of the range where you want to keep the transposed array. Here I have entered H3:R3.
Step 8:
➤ Then click OK.
➤ You will get the transpose of your selected array in your selected destination.
Read more: VBA to Transpose Array in Excel (3 Methods)
Similar Readings
- How to Transpose Duplicate Rows to Columns in Excel (4 Ways)
- Excel Power Query: Transpose Rows to Columns (Step-by-Step Guide)
- How to Transpose Rows to Columns Using Excel VBA (4 Ideal Examples)
- Transpose Multiple Columns into One Column in Excel (3 Handy Methods)
2. Transpose Multi-Dimensional Array in Excel VBA
The VBA code described above works for multi-dimensional arrays too.
Therefore, instead of transposing a single column, you can transpose the whole data set using this code.
Step 1:
➤ Follow steps 1-4 of the previous method to insert the code and save the workbook.
➤ Then select the whole data set instead of a single column.
Step 2:
➤ Follow steps 6-7 from the previous method to run the Macro.
➤ This time I have entered range H3:R7 to keep the transposed range.
Step 3:
➤ Then click OK. You will find your whole data set being transposed in your destination cell range.
Read More: Conditional Transpose in Excel (2 Examples)
3. Transpose in Excel VBA with Paste Special Method (For Keeping the Format Intact)
The VBA code described above does transpose the whole data set, but it doesn’t copy the format along with it.
That means, it just copies the values but doesn’t copy the design and other formats that we applied on our main data set for the purpose of beautification.
But you can use a slightly changed VBA code to transpose your data set along with keeping the format intact.
First, run steps 1-4 from method 1 to save the Macro. Just enter the following VBA code in place of the code.
♦ Code:
Sub Transpose_Paste_Value()
Dim sourceRng As Excel.Range
Dim taretRng As Excel.Range
Set sourceRng = Range(Selection.Address)
Set targetRng = Range(InputBox("Enter the Range Where You Want to Keep the Transposed Array: "))
sourceRng.Copy
targetRng.PasteSpecial Operation:=xlNone, SkipBlanks:=False, Transpose:=True
End Sub
Then follow steps 5-7 from method-1 to run the Macro (This time the name of the Macro is Transpose_Paste_Value.)
The transpose array of your data set will be created in your selected range, carrying the format from the original data set.
Read More: Excel Paste Transpose Shortcut: 4 Easy Ways to Use
Conclusion
Using these methods, you can transpose a data set of any dimension in your workbook taking the help of Excel VBA. Do you know any other method? Or do you have any questions? Feel free to ask us.
Related Articles
- How to Transpose Every n Rows to Columns in Excel (2 Easy Methods)
- Excel VBA: Transpose Multiple Rows in Group to Columns
- How to Transpose Rows to Columns Based on Criteria in Excel
- Transpose Rows to Columns Using Excel VBA (4 Ideal Examples)
- How to Transpose a Table in Excel (5 Suitable Methods)
- How to Transpose Rows to Columns in Excel (5 Useful Methods)
- How to Reverse Transpose in Excel (3 Simple Methods)
Dear Sir or Madam,
I appreciate your help / website in learning how to use Excel VBA.
Today I used your Code https://www.exceldemy.com/excel-vba-insert-rows-based-on-cell-value/.
But I would need help in how to accomplish my task if you have some time for advice.
I can’t send you my Excel in your “Leave a Reply”, so it might be not easy to understand what I am asking but I will try.
My result of multiplying needed columns is completely down (Additionally I made formula in my table to copy the same data of first raw into all of the rest of multiplied rows).
But do you think I can make a VBA Sub/Function which would fill all the numbers (columns 5-9) into only one column below each other to the respective person? I am the beginner with VBA so at the moment I do not have an idea how to do it. If there is a way how to do it directly in Excel table it is also possible. I have an idea but it seems to me too complicated with a large set of data (copying all numbers to additional columns always one row lower and then making only one column at the end which would join them by concatenate function).
I mean that the result should be like this:
5 columns:
1 Miriam Pekníková UKOLFKN 59718
1 Miriam Pekníková UKOLFKN 348197
1 Miriam Pekníková UKOLFKN 71891
1 Miriam Pekníková UKOLFKN 111479
1 Miriam Pekníková UKOLFKN 112065
2 Ana Kánová UKOSSK 59716
2 Ana Kánová UKOSSK 348197
3 Jana Melounová UKOCHK 59715
3 Jana Melounová UKOCHK 349197
3 Jana Melounová UKOCHK 71891
3 Jana Melounová UKOCHK 111479
… and so on further.
Original table has 9 columns:
1 Miriam Pekníková UKOLFKN 59718 348197 71891 111479 112065
1 Miriam Pekníková UKOLFKN
1 Miriam Pekníková UKOLFKN
1 Miriam Pekníková UKOLFKN
1 Miriam Pekníková UKOLFKN
1 Miriam Pekníková UKOLFKN
2 Ana Kánová UKOSSK 59716 348197
2 Ana Kánová UKOSSK
2 Ana Kánová UKOSSK
3 Jana Melounová UKOCHK 59715 349197 71891 111479
3 Jana Melounová UKOCHK
3 Jana Melounová UKOCHK
3 Jana Melounová UKOCHK
3 Jana Melounová UKOCHK
4 Petr Heler UKOFFLL 59714 379197 71891 159479
4 Petr Heler UKOFFLL
4 Petr Heler UKOFFLL
4 Petr Heler UKOFFLL
4 Petr Heler UKOFFLL
I looked at this but I do not know if it can help me (https://www.exceldemy.com/excel-transpose-multiple-rows-in-group-to-columns/). Do you have some other link which would be more related to my problem?
Thank you very much
Veronika
Completely original table was like this:
1 Miriam Pekníková UKOLFKN 59718 348197 71891 111479 112065
2 Ana Kánová UKOSSK 59716 348197
3 Jana Melounová UKOCHK 59715 349197 71891 111479 112010
4 Petr Heler UKOFFLL 59714 379197 71891 159479 896010
5 Helena Bylinková UKOOO 59713 378197 71891 111779
6 Silva Horáková UKOIMU 59712 349197 71991 159479 112010
7 David Mokrý UKOZEK 59711 348197 71891
8 Roderik Tekla UKODDDD 59710 348197 71991 111479 112065
9 Tereza Sivá UKOMMM 59722
Hi Veronika, if your problem isn’t solved yet, you can send the file to my mail or can send an image of your file. I’ll try.