I currently have this data in a sheet
我现在有一个表格
Col A Col B
1 angry birds, gaming
2 nirvana,rock,band
What I want to do is split the comma separated entries in the second column and insert in new rows like below:
我要做的是分割第二列中逗号分隔的条目,然后插入新的行,如下所示:
Col A Col B
1 angry birds
1 gaming
2 nirvana
2 rock
2 band
I am sure this can be done with VBA but couldn't figure it out myself.
我相信这可以用VBA来完成,但我自己却搞不清楚。
2 个解决方案
#1
22
You are better off using variant arrays rather than cell loops - they are much quicker code wise once the data sets are meaningful. Even thoug the code is longer :)
最好使用不同的数组而不是单元格循环——一旦数据集有意义,它们的代码就会快得多。即使代码较长:)
This sample below dumps to column C and D so that you can see the orginal data. Change [c1].Resize(lngCnt, 2).Value2 = Application.Transpose(Y)
to [a1].Resize(lngCnt, 2).Value2 = Application.Transpose(Y)
to dump over your original data
下面的示例将转储到C和D列,以便您可以看到原始数据。改变(c1)。调整大小(lngCnt, 2). value2 =应用。转置(Y)到[a1]。调整大小(lngCnt, 2).Value2 = application .转置(Y)以转储原始数据
[Updated with regexp to remove any blanks after ,
ie ", band" becomes "band"]
[使用regexp更新后删除任何空格,即“,band”变为“band”]
Sub SliceNDice()
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
[c1].Resize(lngCnt, 2).Value2 = Application.Transpose(Y)
End Sub
#2
4
Takes your data in column A and puts the results in column C.
将数据放在A列,并将结果放在C列。
Sub SplitAll()
Dim src As Range
Dim result As Variant
For Each src In Range("A:A").SpecialCells(xlCellTypeConstants)
result = Split(src, ",")
'last cell in column C
With Cells(Rows.Count, 3).End(xlUp)
Range(.Offset(1, 0), .Offset(1 + UBound(result, 1), 0)) = Application.WorksheetFunction.Transpose(result)
End With
Next src
End Sub
#1
22
You are better off using variant arrays rather than cell loops - they are much quicker code wise once the data sets are meaningful. Even thoug the code is longer :)
最好使用不同的数组而不是单元格循环——一旦数据集有意义,它们的代码就会快得多。即使代码较长:)
This sample below dumps to column C and D so that you can see the orginal data. Change [c1].Resize(lngCnt, 2).Value2 = Application.Transpose(Y)
to [a1].Resize(lngCnt, 2).Value2 = Application.Transpose(Y)
to dump over your original data
下面的示例将转储到C和D列,以便您可以看到原始数据。改变(c1)。调整大小(lngCnt, 2). value2 =应用。转置(Y)到[a1]。调整大小(lngCnt, 2).Value2 = application .转置(Y)以转储原始数据
[Updated with regexp to remove any blanks after ,
ie ", band" becomes "band"]
[使用regexp更新后删除任何空格,即“,band”变为“band”]
Sub SliceNDice()
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
[c1].Resize(lngCnt, 2).Value2 = Application.Transpose(Y)
End Sub
#2
4
Takes your data in column A and puts the results in column C.
将数据放在A列,并将结果放在C列。
Sub SplitAll()
Dim src As Range
Dim result As Variant
For Each src In Range("A:A").SpecialCells(xlCellTypeConstants)
result = Split(src, ",")
'last cell in column C
With Cells(Rows.Count, 3).End(xlUp)
Range(.Offset(1, 0), .Offset(1 + UBound(result, 1), 0)) = Application.WorksheetFunction.Transpose(result)
End With
Next src
End Sub