Converting a table of cell values to a row

glad_ir

Board Regular
Joined
Nov 22, 2020
Messages
143
Office Version
  1. 2010
Platform
  1. Windows
Hello,

Please could somebody show me how to code VBA to copy table values into a single row using VBA - e.g. copy A2:C4 into row E1 to E9 as shown below

I have a number of larger tables to do this with so am looking for some code for the below I can modify to handle these.

Any help much appreciated.

Thank you
Iain

Cell to be copiedDestination
A2E1
A3E2
A4E3
B2E4
B3E5
B4E6
C2E7
C3E8
C4E9
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
If it's only the values you want try this - adjust the range to suit and change 2 to 1 on the first loop if there are no headers.
VBA Code:
Sub TableToCol()
Dim arrIn As Variant
Dim arrOut As Variant
Dim cnt As Long
Dim idxCol As Long
Dim idxRow As Long

    arrIn = Range("A1").CurrentRegion.Value

    ReDim arrOut(1 To UBound(arrIn, 1) * UBound(arrIn, 2), 1 To 1)

    For idxRow = 2 To UBound(arrIn, 1)
        For idxCol = 1 To UBound(arrIn, 2)
            cnt = cnt + 1
            arrOut(cnt, 1) = arrIn(idxRow, idxCol)
        Next idxCol
    Next idxRow

    Range("E1").Resize(cnt).Value = arrOut

End Sub
 
Upvote 0
If it's only the values you want try this - adjust the range to suit and change 2 to 1 on the first loop if there are no headers.
VBA Code:
Sub TableToCol()
Dim arrIn As Variant
Dim arrOut As Variant
Dim cnt As Long
Dim idxCol As Long
Dim idxRow As Long

    arrIn = Range("A1").CurrentRegion.Value

    ReDim arrOut(1 To UBound(arrIn, 1) * UBound(arrIn, 2), 1 To 1)

    For idxRow = 2 To UBound(arrIn, 1)
        For idxCol = 1 To UBound(arrIn, 2)
            cnt = cnt + 1
            arrOut(cnt, 1) = arrIn(idxRow, idxCol)
        Next idxCol
    Next idxRow

    Range("E1").Resize(cnt).Value = arrOut

End Sub
Hi, thank you for the prompt reply and your help - I really appreciate it.

The code works a treat for what I asked........but I asked the wrong question. I'm sorry, I need to convert to a row rather than a column. Please could you advise changes to the code for this?

Sorry,
Iain

Cell to be copiedDestination
A2E1
A3F1
A4G1
B2H1
B3I1
B4J1
C2K1
C3L1
C4M1
 
Upvote 0
Oops, misread your post - think the way the layout of the posted data was confusing too.

Nevermind easily fixed.
VBA Code:
Sub TableToRow()
Dim arrIn As Variant
Dim arrOut As Variant
Dim cnt As Long
Dim idxCol As Long
Dim idxRow As Long

    arrIn = Range("A1").CurrentRegion.Value

    ReDim arrOut(1 To 1, 1 To UBound(arrIn, 1) * UBound(arrIn, 2))

    For idxRow = 2 To UBound(arrIn, 1)
        For idxCol = 1 To UBound(arrIn, 2)
            cnt = cnt + 1
            arrOut(1, cnt) = arrIn(idxRow, idxCol)
        Next idxCol
    Next idxRow

    Range("E1").Resize(, cnt).Value = arrOut

End Sub
 
Upvote 0
Solution
Oops, misread your post - think the way the layout of the posted data was confusing too.

Nevermind easily fixed.
VBA Code:
Sub TableToRow()
Dim arrIn As Variant
Dim arrOut As Variant
Dim cnt As Long
Dim idxCol As Long
Dim idxRow As Long

    arrIn = Range("A1").CurrentRegion.Value

    ReDim arrOut(1 To 1, 1 To UBound(arrIn, 1) * UBound(arrIn, 2))

    For idxRow = 2 To UBound(arrIn, 1)
        For idxCol = 1 To UBound(arrIn, 2)
            cnt = cnt + 1
            arrOut(1, cnt) = arrIn(idxRow, idxCol)
        Next idxCol
    Next idxRow

    Range("E1").Resize(, cnt).Value = arrOut

End Sub
All my fault. Thank you so much for this - it will really help me avoid some really mundane work!

cheers,
Iain
 
Upvote 0

Forum statistics

Threads
1,214,388
Messages
6,119,229
Members
448,879
Latest member
VanGirl

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top