如何将列数据合并为一行?

时间:2018-08-14 20:07:46

标签: excel

我有一个产品数据电子表格,出于导入的原因,我需要将图像的文件名合并为一行。

enter image description here

我基本上需要它看起来像这样:

enter image description here

总有可能通过跳过每个空白单元格来实现这一点吗?

2 个答案:

答案 0 :(得分:1)

有些可能适合您。这会将文本块复制到一列中,并从每个块的第一行开始向右转置它们。

Sub TransposeFilenames()
    Dim lRow As Long, fRow As Long, col As Long
    Dim copyRng As Range

    fRow = 2 ' First row with data, change as needed
    col = 3 ' Column with filenames, change as needed

    With ThisWorkbook.Worksheets("Sheet1") ' Change to your sheet name
        lRow = .Cells(.Rows.Count, col).End(xlUp).Row  

        Do
            Set copyRng = .Range(.Cells(fRow, col), .Cells(fRow, col).End(xlDown))
            copyRng.Copy
            copyRng.Cells(1).Offset(, 1).PasteSpecial Paste:=xlPasteValues, Transpose:=True

            fRow = copyRng.End(xlDown).End(xlDown).Row
        Loop While fRow < lRow

    End With
End Sub

之前

enter image description here

之后

enter image description here

答案 1 :(得分:0)

以下是使用Excel公式执行此操作的简单方法:

如果您的数据是这样布置的:

    A   B      C     
  +---+---+---------+
1 |...|...|Filename |
2 |...|...|filename1|
3 |...|...|filename2|
4 |...|...|filename3|
5 |...|...|filename4|
6 |...|...|filename5|
7 |...|...|         |
8 |...|...|filename1|
9 |...|...|filename2|
10|...|...|filename3|
11|...|...|filename4|
12|...|...|filename5|
13|...|...|         |

然后将该公式放在D2中,然后拖曳填充到H2上:

=IF(OR($C2="",$C2 = "Filename"),OFFSET($C2,COLUMN(A1),0),"")

您的结果应如下所示:

    A   B      C         D        E         F          G         H
  +---+---+---------+---------+---------+---------+---------+---------+
1 |...|...|Filename |         |         |         |         |         |
2 |...|...|filename1|filename1|filename2|filename3|filename4|filename5|
3 |...|...|filename2|         |         |         |         |         |
4 |...|...|filename3|         |         |         |         |         |
5 |...|...|filename4|         |         |         |         |         |
6 |...|...|filename5|         |         |         |         |         |
7 |...|...|         |         |         |         |         |         |
8 |...|...|filename1|filename1|filename2|filename3|filename4|filename5|
9 |...|...|filename2|         |         |         |         |         |
10|...|...|filename3|         |         |         |         |         |
11|...|...|filename4|         |         |         |         |         |
12|...|...|filename5|         |         |         |         |         |
13|...|...|         |         |         |         |         |         |
相关问题