How can I transpose a range in VBA, using an example? 2

How can I transpose a range in VBA, using an example?

Transposing a range in VBA refers to rearranging the data in a row into columns, or vice versa. This can be done by using the “Transpose” function in VBA, which allows you to quickly and easily convert your data from horizontal to vertical or vice versa. For example, if you have a range of data in cells A1 to A10, and you want to transpose it into a column, you can use the following code:

Range(“A1:A10”).Select
Selection.Copy
Range(“B1”).Select
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:=False, Transpose:=True

This will copy the data from the range A1 to A10 and paste it into column B, with the data now appearing vertically instead of horizontally. Transposing a range in VBA can be useful when you want to reorganize your data or perform calculations on it in a different orientation.

Transpose a Range in VBA (With Example)


You can use the following basic syntax to transpose a range using VBA:

Sub TransposeRange()

    'specify range to transpose
    MyRange = Range("A1:B5")
    
    'find dimensions of range
    XUpper = UBound(MyRange, 1)
    XLower = LBound(MyRange, 1)
    YUpper = UBound(MyRange, 2)
    YLower = LBound(MyRange, 2)
    
    'transpose range
    Range("D1").Resize(YUpper - YLower + 1, XUpper - XLower + 1).Value = _
      WorksheetFunction.Transpose(MyRange)
      
End Sub

This particular example will transpose the cells in the range A1:B5 and output the transposed range starting in cell D1.

The following example shows how to use this syntax in practice.

Example: How to Transpose a Range in VBA

Suppose we have the following dataset in Excel that contains information about various basketball players:

Suppose we would like to transpose the range A1:B5 and output the transposed range starting in cell D1.

We can create the following macro to do so:

Sub TransposeRange()

    'specify range to transpose
    MyRange = Range("A1:B5")
    
    'find dimensions of range
    XUpper = UBound(MyRange, 1)
    XLower = LBound(MyRange, 1)
    YUpper = UBound(MyRange, 2)
    YLower = LBound(MyRange, 2)
    
    'transpose range
    Range("D1").Resize(YUpper - YLower + 1, XUpper - XLower + 1).Value = _
      WorksheetFunction.Transpose(MyRange)
      
End Sub

When we run this macro, we receive the following output:

Notice that the transposed range is displayed starting in cell D1.

That is, the rows and the columns are switched.

To transpose a different range, simply change A1:B5 in the macro to a different range.

Note: You can find the complete documentation for the VBA Transpose method .

Cite this article

stats writer (2024). How can I transpose a range in VBA, using an example?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-transpose-a-range-in-vba-using-an-example/

stats writer. "How can I transpose a range in VBA, using an example?." PSYCHOLOGICAL SCALES, 24 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-transpose-a-range-in-vba-using-an-example/.

stats writer. "How can I transpose a range in VBA, using an example?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-transpose-a-range-in-vba-using-an-example/.

stats writer (2024) 'How can I transpose a range in VBA, using an example?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-transpose-a-range-in-vba-using-an-example/.

[1] stats writer, "How can I transpose a range in VBA, using an example?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. How can I transpose a range in VBA, using an example?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top