将一列值转换为一行

时间:2014-07-15 01:53:42

标签: excel vba csv

所以我有这样的数据:

 A             B
sku     custom_option_row_sku
AGR370A AGR370A-3
        AGR370A-4
        AGR370A-5
        AGR370A-6
        AGR370A-8
        AGR370A-9
        AGR370A-10
        AGR370A-210
        AGR370A-212
AGR370B AGR370B-3
        AGR370B-4
        ...

我需要取B列并将其变成一行,以便它仍然匹配A列中的值。

理想情况下最终会像:

 A                 B
sku      custom_option_row_sku
AGR370A  AGR370A-3, AGR370A-4, AGR370A-5, etc.

这可能是内置功能还是我需要VBA呢?提前谢谢。

2 个答案:

答案 0 :(得分:1)

我刚用你给我的例子用这段代码做了。如果您有任何问题请告诉我,但它对我有用。请务必制作备份副本以防万一。

 Sub resort()
 Dim thesheet As Worksheet
 Set thesheet = Sheets("Sheet1")
 Dim lastrow As Long
 Dim x As Long
 Dim newRow As Long
 Dim colA As String
 Dim deleterRow As Long
 lastrow = thesheet.Range("B" & Rows.Count).End(xlUp).Row

 With thesheet
 newRow = 0

 For x = 1 To lastrow
  colA = .Cells(x, 1).Value
   If colA <> "" Then
    colA = .Cells(x, 1).Value
    newRow = x
   Else
    .Cells(newRow, 2).Value = .Cells(newRow, 2).Value & ", " & .Cells(x, 2).Value
   End If

 Next
 deleterRow = 1
 For x = 1 To lastrow
  colA = .Cells(deleterRow, 1).Value
 If colA = "" Then
  .Rows(deleterRow).Delete
 Else
  deleterRow = deleterRow + 1
 End If
 Next
 End With
 End Sub

答案 1 :(得分:0)

如果没有那么多信息,您不一定需要VBA。只需从列中复制所需数据,转到要粘贴的行,右键单击 - &gt;选择性粘贴 - &gt;置。图标如下所示:enter image description here