【问题标题】:Excel Duplicate Rows depending on Columns ValueExcel 根据列值重复行
【发布时间】:2015-06-17 07:18:54
【问题描述】:

在我的 excel 文件中,在第一列 ProductName 中,我有我的产品列表,每个产品可以有不同的尺寸:VerySmallSmallLarge 等。每个 @987654327 @ 每个产品都有自己的价格。 我试图垂直列出每种产品的每种可用尺寸及其相应的价格。
这是原始数据结构:


这就是我的结果的样子:

我有大约 8000 多种这样的产品。在过去的几天里,我试图找到这个问题的解决方案,但我完全失败了。有什么办法可以把尺码和价格拉出来垂直列出来吗?

【问题讨论】:

    标签: excel excel-formula excel-2010 vba


    【解决方案1】:

    假设数据在 Sheet1 并且 Sheet2 为空,将此代码编写为 VBA 宏 (ALT+F11) 并运行 (F5)

    Sub Flatten()
      Dim row as Integer,col as Integer, dst_row as Integer,dst_col as Integer
      Dim col_start as Integer,col_end as Integer
      'Your parameters:
      col_start=3'C column
      col_end=12'L column
      row=2'Data starts from second row
      dst_row=row
      'Copy the headers
      For col=1 To col_start+2
        Sheet2.Cells(1,col)=Sheet1.Cells(1,col)
      Next col
      'Flat the Table
      While Sheet1.Cells(row,col_start)<>""
        For col=col_start To col_end Step 2
          If Sheet1.Cells(row,col)<>"" Then
            For dst_col=1 To col_start
              Sheet2.Cells(dst_row,dst_col)=Sheet1.Cells(row,dst_col)
            Next dst_col
            Sheet2.Cells(dst_row,col_start)=Sheet1.Cells(row,col)
            Sheet2.Cells(dst_row,col_start+1)=Sheet1.Cells(row,col+1)
            dst_row=dst_row+1
          End If
        Next col
        row=row+1
        dst_row=dst_row+1
      Wend
    End Sub
    

    【讨论】:

    • 这是我按照您所说的执行宏后得到的:i.imgur.com/6ILHY6O.png
    • 请看图:i.imgur.com/6ILHY6O.png 只添加了第一列的值。但我期待这样的事情:i.stack.imgur.com/LK6Pz.png@Uri Goren
    • @TanvirSourov,你是对的 - 解决了这个问题。如果有帮助,请接受答案或至少投赞成票
    • 成功了!非常感谢伙计!你完全救了我!非常感谢! :) @Uri Goren
    猜你喜欢
    • 1970-01-01
    • 2016-06-27
    • 1970-01-01
    • 1970-01-01
    • 2019-05-29
    • 1970-01-01
    • 2016-03-03
    • 2012-06-11
    • 1970-01-01
    相关资源
    最近更新 更多