Excel 根据列值复制行
Excel Duplicate Rows depending on Columns Value
在我的 excel 文件中,在第一列 ProductName
中,我有我的产品列表,每个产品可以有不同的尺寸:VerySmall
、Small
、Large
等。每个 Size
对每个产品都有自己的价格。
我试图垂直列出每种产品的每种可用尺寸及其相应的价格。
这里是原始数据结构:
这就是我的 结果:
的样子
我有大约 8000 多种这样的产品。在过去的几天里,我试图找到这个问题的解决方案,但我完全失败了。有什么办法可以把尺码和价格拉出来竖排吗?
假设数据在 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
在我的 excel 文件中,在第一列 ProductName
中,我有我的产品列表,每个产品可以有不同的尺寸:VerySmall
、Small
、Large
等。每个 Size
对每个产品都有自己的价格。
我试图垂直列出每种产品的每种可用尺寸及其相应的价格。
这里是原始数据结构:
这就是我的 结果:
我有大约 8000 多种这样的产品。在过去的几天里,我试图找到这个问题的解决方案,但我完全失败了。有什么办法可以把尺码和价格拉出来竖排吗?
假设数据在 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