這是我對兩列數據的答案。但我想做三欄,有人可以幫我嗎?
你最好使用變體數組而不是單元循環 - 一旦數據集有意義,它們的代碼更加快速。即使你的代碼更長:)
下面的示例轉儲到C和D列,以便您可以看到原始數據。改變[C1] .Resize(lngCnt,2).Value2 = Application.Transpose(Y),以[A1] .Resize(lngCnt,2).Value2 = Application.Transpose(Y)轉儲在你的原始數據
[用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
'將重新排序的範圍轉儲到列C:D [c1] .Resize(lngCnt,2).Value2 = Application .Transpose(Y) End Sub
你好,歡迎來到StackOverflow。請花一些時間閱讀幫助頁面,尤其是名爲[「我可以詢問什麼主題?」(http://stackoverflow.com/help/on-topic)和[「我應該問什麼類型的問題避免問?「](http://stackoverflow.com/help/dont-ask)。更重要的是,請閱讀[Stack Overflow問題清單](http://meta.stackexchange.com/q/156810/204922)。您可能還想了解[MCVE](http://stackoverflow.com/help/mcve)。並且包括你正在努力通過的代碼...所以人們可以提供幫助。 – Rdster