【问题标题】:How to Combine 4 column into 1 column?如何将 4 列合并为 1 列?
【发布时间】:2013-01-24 08:25:18
【问题描述】:

使用office 2010。
一切都在同一张纸上。
A B C & 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 列的长度可以增减。

【问题讨论】:

标签: vba excel excel-formula excel-2010


【解决方案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

【讨论】:

  • 感谢它的工作,但仍然很少有东西,它也包括第一行,它不应该而且我知道这会完全搞砸如果它不是 A B C D 怎么办?如果是 I, O, P Q ?
  • @Mowgli 您可以修改它以使用第 2 行,然后将其复制的最后一行从 ws.Cells(1, currentColumn) 更改为 ws.Cells(2, currentColumn)。对于列问题,您必须创建一个包含所需列的数组,然后循环遍历它而不是 1 To 4
【解决方案2】:

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

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

【讨论】:

    【解决方案3】:

    借助本站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)
    

    F 列中的公式并向下拖动

      =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)),""))))
    

    截图

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-09-22
      • 1970-01-01
      • 2013-12-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多