How to Transpose in Excel VBA (3 Methods)

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.

Data Set to Transpose in Excel VBA

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.

Opening VBA Window to Transpose in Excel VBA

Step 2:

Go to the Insert tab in the VBA window. 

Then from the options available, select Module.

Inserting Module to Transpose in Excel VBA

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

VBA Code to Transpose in Excel VBA

Step 4:

Save the workbook as Excel Macro-Enabled Workbook.

Saving Macro-Enabled Workbook to Transpose in Excel VBA

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.

Selecting One Dimensional Array to Transpose in Excel VBA

Step 6:

Press ALT+F8 on your keyboard.

A dialogue box called Macro will open. Select Transpose and click on Run.

Running Macro to Transpose in Excel VBA

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.

Input Box to Transpose in Excel VBA

Step 8:

Then click OK.

You will get the transpose of your selected array in your selected destination.

One Dimensional Array Transpose in Excel VBA

Read more: VBA to Transpose Array in Excel (3 Methods)


Similar Readings


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.

Selecting the Whole Data Set to Transpose in Excel VBA

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.

Input Box to Transpose in Excel VBA

Step 3:

Then click OK. You will find your whole data set being transposed in your destination cell range.

Multi-Dimensional Array to Transpose in Excel VBA

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

VBA Code to Transpose in Excel VBA

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.

Keeping the Format Intact to Transpose in Excel VBA

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

Rifat Hassan

Rifat Hassan

Hello! Welcome to my profile. Here I will be posting articles related to Microsoft Excel. I am a passionate Electrical Engineer holding a Bachelor’s degree in Electrical and Electronic Engineering from Bangladesh University of Engineering and Technology. Besides academic studies, I always love to keep pace with the revolution in technology that the world is rushing towards day by day. I am diligent, career-oriented, and ready to cherish knowledge throughout my life.

2 Comments
  1. 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.

Leave a reply

ExcelDemy
Logo