简体   繁体   中英

Combine columns of data in excel into one column without changing sequence

I know this question has been asked before but I couldn't follow the instructions from the previous post to make it work. I am trying to combine a few columns of data that look like Table on the left is what I have trying to get to table on the right the block of data on the left which I want to combine into one, unchanged row (the sequence is very important as it follows a time series). I am compiling data for all 50 states in a separate table where I want to paste this column into. The data runs from columns (A:AY). Help would be greatly appreciated cause I really don't know what I am doing(complete novice here, I have no experience with VBA). A proper breakdown of the process would be greatly appreciated.

I tried using the CONCATENATE funcion but keep running into an error, I found a few torubleshooting methods on google but they seem to interrupt the sequence of data. I tried using the tutorial from This but couldn't get it to work.

In case you don't have tocol, you can just as well use INDEX, like so.

Table named "data":

col1 col2 col3
a g m
b h n
c i o
d j p
e k q
f l r

Then this formula will put the data into a single column. Just put it anywhere in the first row, change "data" to your input range and expand down.

=INDEX(data,MOD(ROW()-1,ROWS(data))+1,CEILING.MATH(ROW()/ROWS(data)))

Or, if your data is very large and you don't want to expand manually, you can try this VBA approach.The only thing you need to change is the "data" and "targetCell" variables at the top.

"data" is your original table so in your example, it would be Range("B2:F12"). "targetCell" is where the column will start. In your Example it's Range("BC2")


Sub singleColumn()
    Dim data As Range, targetCell As Range, targetRange As Range
    Set data = Range("data")
    Set targetCell = Range("G20")
    
    Dim resultArr() As Variant, arr As Variant
    ReDim resultArr(1 To data.Rows.Count * data.Columns.Count, 1 To 1)
    arr = data

    For i = 1 To UBound(resultArr)
        resultArr(i, 1) = arr(((i - 1) Mod UBound(arr)) + 1, WorksheetFunction.Ceiling_Math(i / UBound(arr)))
    Next i
    
    Set targetRange = Range(targetCell, Cells(targetCell.Row + UBound(resultArr) - 1, targetCell.Column))
    targetRange = resultArr
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM