显示以逗号分隔的第二列作为子类别

有没有办法将列B与逗号分隔的数据转换为多个行,也作为A的子类别的值是唯一的。

例:

AB 1 222,333,444 2 111,222,333 

被转换成

  AB 1 222 _ 333 _ 444 2 111 _ 222 _ 333 

_是一个单独的行

这将通过换行来代替昏迷

 Dim lastRow As Integer lastRow = Sheets("Sheet1").Range("B1").End(xlDown).Row For i = 1 To lastRow Sheets("Sheet1").Cells(i, 2).Replace ",", vbCrLf Sheets("Sheet1").Cells(i, 2).Range("B1").Rows.AutoFit Next 

编辑:这应该适合你所要求的

 Dim objRegex As Object Dim X Dim Y Dim lngRow As Long Dim lngCnt As Long Dim tempArr() As String Dim strArr Set objRegex = CreateObject("vbscript.regexp") objRegex.Pattern = "^\s+(.+?)$" 'Define the range to be analysed X = Range([a1], Cells(Rows.Count, "b").End(xlUp)).Value2 ReDim Y(1 To 2, 1 To 1000) For lngRow = 1 To UBound(X, 1) 'Split each string by "," tempArr = Split(X(lngRow, 2), ",") For Each strArr In tempArr lngCnt = lngCnt + 1 'Add another 1000 records to resorted array every 1000 records If lngCnt Mod 1000 = 0 Then ReDim Preserve Y(1 To 2, 1 To lngCnt + 1000) Y(1, lngCnt) = X(lngRow, 1) Y(2, lngCnt) = objRegex.Replace(strArr, "$1") Next Next lngRow 'Dump the re-ordered range to columns C:D [a1].Resize(lngCnt, 2).Value2 = Application.Transpose(Y) 

我testing了它,这是我的输出。 让我知道是否有问题。

在这里输入图像说明