多行和列到单列Excel

时间:2016-04-24 00:34:54

标签: excel vba excel-vba

我在Excel中有多行和多列的不同数据,我希望所有数据都在一个列上。我尝试了转置功能,但它没有帮助我得到我想要的东西。

这就是我现在所拥有的:

enter image description here

这就是我想要的:

enter image description here

有人可以告诉我如何实现这一目标吗?任何内置函数或宏都会有所帮助。感谢。

4 个答案:

答案 0 :(得分:2)

我认为本教程可以帮助您:tutorial 您可以根据自己的需要尝试该解决方案。

答案 1 :(得分:2)

试试这段代码:

[1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE

代码将从Sub RangetoColumn() Dim LastRow As Long, LastColumn As Long Dim CurrentSheet As Worksheet, TargetSheet As Worksheet Dim i As Long, j As Long, Count As Long Set CurrentSheet = ThisWorkbook.Worksheets("Sheet1")'-->change Sheet1 to your source sheet Set TargetSheet = ThisWorkbook.Worksheets("Sheet2")'-->change Sheet2 to your target sheet LastRow = CurrentSheet.Cells(Rows.Count, "A").End(xlUp).Row Count = 1 For i = 1 To LastRow LastColumn = CurrentSheet.Cells(i, Columns.Count).End(xlToLeft).Column For j = 1 To LastColumn TargetSheet.Range("A" & Count).Value = CurrentSheet.Cells(i, j).Value Count = Count + 1 Next j Next i End Sub 读取范围,并将在Sheet1中创建一个列。

答案 2 :(得分:0)

尝试在两个单元格中使用&。尝试使用此A1&B1来合并单元格A1和单元格B1中的数据;复制并粘贴,然后享受。

答案 3 :(得分:0)

这将使所有当前表格单元格在其A列中列出,并清除所有其他相邻列

Option Explicit

Sub RangeToOneColumn()
    Dim lastRow As Long, i As Long, j As Long
    Dim rng As Range
    Dim myArr As Variant

    With ThisWorkbook.Worksheets("Sheet1") '<== change it to your needs
        lastRow = .Cells(.rows.Count, "A").End(xlUp).Row
        ReDim myArr(0 To lastRow - 1)
        For i = 1 To lastRow
            Set rng = .Range(.Cells(i, 1), .Cells(i, .Columns.Count).End(xlToLeft))
            myArr(i - 1) = Application.Transpose(Application.Transpose(rng))
        Next i
        .Cells(1, 1).CurrentRegion.ClearContents

        j = 1
        For i = LBound(myArr) To UBound(myArr)
            .Cells(j, 1).Resize(UBound(myArr(i))) = Application.Transpose(myArr(i))
            j = j + UBound(myArr(i))
        Next i
    End With

End Sub

因为它使用数组,所以它比通过单元格迭代复制和粘贴运行得更快