如何将4列合并为1列?

时间:2013-02-08 14:31:45

标签: excel vba excel-vba excel-formula excel-2010

使用office 2010.
所有内容都在同一张纸上 A A B C& A栏中的数据D可以改变(每天增加或减少)


我有4栏

                            OUTPUT --> IN column F should be 
---A-----B-----C------D---------------------------------------F
   1     5     8     AP                                       1
   2     6     9     BP                                       2
   3     7     1     CD                                       3
   4           5     QW                                       4
                                                              5
                                                              6
                                                              7
                                                              8
                                                              9
                                                              1
                                                              5
                                                              AP
                                                              BP
                                                              CD
                                                              QW

柱的长度A B C& D可以增加减少。

3 个答案:

答案 0 :(得分:1)

这个怎么样?

Sub move()
    Dim ws As Worksheet
    Dim outputColumn As Long
    Dim currentColumn As Long
    Dim currentOutputRow As Long

    Set ws = ActiveSheet
    outputColumn = 6 ' column f

    For currentColumn = 1 To 4
        currentOutputRow = ws.Cells(ws.Rows.Count, outputColumn).End(xlUp).Row
        If (currentOutputRow > 1) Then
            currentOutputRow = currentOutputRow + 1
        End If

        ws.Range(ws.Cells(1, currentColumn), ws.Cells(ws.Rows.Count, currentColumn).End(xlUp)).Copy _
            ws.Cells(currentOutputRow, outputColumn)
    Next
End Sub

答案 1 :(得分:1)

使用以下内容。它接受您需要更改的范围,并返回垂直数组值。要填充值,请使用数组公式。

Function ToVector(rng As Range)

    Dim cells()
    ReDim cells(rng.cells.Count)

    Dim i As Double

    For Each cell In rng

        cells(i) = cell
        i = i + 1

    Next cell

    ToVector = Application.WorksheetFunction.Transpose(cells)

End Function

答案 2 :(得分:1)

借助此网站get-digital-help.com/

Combine Columns但这只是静态的。

我将其转换为动态意义改变范围。

例如我发布了A B C D IN F

为了使公式更清晰,将在名称管理器中输入公式

以下是每个栏目的动态公式(名字管理员)

ALIST = =OFFSET($A$1,0,0,COUNTA($A:$A),1)
BLIST = =OFFSET($B$1,0,0,COUNTA($B:$B),1)
CLIST = =OFFSET($C$1,0,0,COUNTA($C:$C),1)
DLIST = =OFFSET($D$1,0,0,COUNTA($D:$D),1)

柱子中的公式并向下拖动

  =IFERROR(INDEX(ALIST, ROWS(F$1:$F1)), 
   IFERROR(INDEX(BLIST, ROWS(F$1:$F1)-ROWS(ALIST)), 
   IFERROR(INDEX(CLIST, ROWS(F$1:$F1)-ROWS(ALIST)-ROWS(BLIST)),
   IFERROR(INDEX(DLIST, ROWS(F$1:$F1)-ROWS(ALIST)-ROWS(BLIST)-ROWS(CLIST)),""))))

截图

enter image description here enter image description here