请问excel表格中,如何将A列、B列和C列自动排列组合,显示到D列(用VBA代码如何按A列型号排列组合到D列)
请问excel表格中,如何将A列、B列和C列自动排列组合,显示到D列(需要按型号,分开组合,不同型号,有不同的颜色和尺码)?输出结果如图,54型号有3个颜色和6个尺码,5...
请问excel表格中,如何将A列、B列和C列自动排列组合,显示到D列(需要按型号,分开组合,不同型号,有不同的颜色和尺码)?输出结果如图,54型号有3个颜色和6个尺码,55有2个颜色和3个尺码,还有很多型号,对应不同的颜色和尺码,VBA代码需要怎么写,才能实现?
展开
2个回答
展开全部
答:我用VBA写了一小段代码实现了上述功能。要运行宏,可参阅WPS运行宏的方法。
Sub Demo()
Dim FirstRow As Long, LastRow As Long, i As Integer, j As Integer
Dim Cell As Range, DesRng As Range
Set DesRng = Range("D2")
For Each Cell In Range("A2:A" & Cells(Rows.Count, 1).End(xlUp).Row).SpecialCells(xlCellTypeConstants, 23)
FirstRow = Cell.Row
LastRow = Cell.End(xlDown).Row - 1
If Cell.End(xlDown).Row = Cells.Rows.Count Then
LastRow = Application.Max(Cells(Rows.Count, "B").End(xlUp).Row, Cells(Rows.Count, "C").End(xlUp).Row)
End If
For i = FirstRow To LastRow
If Not IsEmpty(Cells(i, "B")) Then
For j = FirstRow To LastRow
If Not IsEmpty(Cells(j, "C")) Then
DesRng = Cell & "+" & Cells(i, "B") & "+" & Cells(j, "C")
Set DesRng = DesRng.Offset(1, 0)
End If
Next j
End If
Next i
Next
MsgBox "转换完成", vbInformation, "提示"
End Sub
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询