Excel重复行取决于列值

时间:2015-04-11 17:04:53

标签: excel excel-vba excel-formula excel-2010 vba

在我的Excel文件的第一列ProductName中,我有我的产品列表,每个产品都有不同的尺寸:VerySmallSmall,{{1每个Large每个产品都有自己的价格。 我试图垂直列出每个产品的每个可用尺寸及其相应的价格。
这是原始数据结构:

The Excel File that is currently available:

这就是我的结果

How the Excel File will be after Conversion:

我有大约8000多种这样的产品。我试图在过去几天找到这个问题的解决方案,但我完全失败了。有没有办法拉大小和价格并垂直列出?

1 个答案:

答案 0 :(得分: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