Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I Transpose Data In Excel Via VBA

Excel How To

How can I Transpose Data In Excel Via VBA

by  Chance1234  Posted    (Edited  )
Please Make sure you read up on arrays and ranges so you can better understand the code below.

Code:
Sub Transpose()
' Very quick example
' On sheet1 add into cells B2:B12, any value you like

Dim intCnt As Integer ' counter
Dim intR, intC As Integer ' Row and column counters
Dim arrC(12) As Variant ' tempoary storage array

intCnt = 1
intR = 2 ' Row 2
intC = 2 ' Column B

'Load Data into array
Do Until intCnt = 12
'Nb re the 12 above, this is quick and dirty but there are
'many ways you can come up with this figure, or you could
'use a named range, or pass a range to a array and use Ubound(range)

'load data into array
    arrC(intCnt) = Sheet1.Cells(intR, intC)
'i know the size of my array here, but if i didnt
'i would use redim preserve every time i added data, to increae
'the size of the array
    intCnt = intCnt + 1 'increase count
    intR = intR + 1 'increase row
Loop

'Now data is loaded into array, we will move
'through it and populate from cell D2

intCnt = 1
intR = 2 ' Row 2
intC = 4 ' Column D

Do Until intCnt = 12
    
    Sheet1.Cells(intR, intC) = arrC(intCnt)
    intCnt = intCnt + 1 'increase count
    intC = intC + 1 ' increase column
Loop

Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top