简体   繁体   English

将一列值变成一行

[英]Turning a column of values into a row

So I have data like this: 所以我有这样的数据:

 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
        ...

I need to take column B and turn it into a row so that it still matches the value in column A. 我需要将B列转换为一行,以便它仍与A列中的值匹配。

It would ideally end up like: 理想情况下,它将最终像:

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

Is this possible with a built in feature or would I need VBA for this? 内置功能是否可能实现此目的?我是否需要VBA? Thanks in advance. 提前致谢。

I just did it with this code using the example you gave me. 我只是使用您给我的示例使用此代码来完成此操作。 If you have any issues let me know, but it worked for me. 如果您有任何问题,请告诉我,但这对我有用。 Be sure to make a backup copy just in case. 确保以防万一。

 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

If it's not that much information, you don't necessarily need VBA. 如果信息不多,则不一定需要VBA。 Just copy the data you want from the column, go to the row you want to paste in, right click -> Paste Special -> Transpose. 只需从列中复制所需的数据,然后转到要粘贴的行,右键单击->选择性粘贴->转置。 The icon looks like this: 图标如下所示: 在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM