请问excel表格中,如何将A列、B列和C列自动排列组合,显示到D列(用VBA代码如何按A列型号排列组合到D列)

请问excel表格中,如何将A列、B列和C列自动排列组合,显示到D列(需要按型号,分开组合,不同型号,有不同的颜色和尺码)?输出结果如图,54型号有3个颜色和6个尺码,55有2个颜色和3个尺码,还有很多型号,对应不同的颜色和尺码,VBA代码需要怎么写,才能实现?

答:我用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

温馨提示:答案为网友推荐,仅供参考
第1个回答  2018-05-02
这么复杂的问题,要给一些奖赏,大家才有兴趣回答追问

我也想给,但只有2个财富值,无法送出啊!跪求!

追答

你用的是WPS表格,先要确定是否能运行宏,不然白费工夫