2014-09-10 76 views
0

我有4個不同的範圍。我需要分割這些範圍的內容。所以細胞含有; 湯姆傑裏兔寶寶傑克斯鮑羅鮑勃。我想把它分成7個不同的單元格。我在網上看過,並發現如何使用下面的代碼執行這樣的分割。Excel拆分字符串循環

Dim text As String 
Dim a As Integer 
Dim name As Variant 
text = ActiveCell.Value 
name = Split(text, " ") 
For a = 0 To UBound(name) 
Cells(1, a + 1).Value = name(a) 
Next a 

我的問題是,我需要這樣的循環中,因此將經歷一個範圍,比如說(「H1:H200」),這是被我卡住了,我已經開始循環,但不能得到分裂在裏面工作。這裏是我的循環的開始:

Dim rCell As Range 
Dim rRng As Range 

Set rRng = Range("H1:H200") 
For Each rCell In rRng.Cells 
'split string here. 

任何幫助這個問題,將不勝感激!謝謝。

回答

1

爲什麼要重新發明輪子?您可以使用TextToColumns。例如:

Dim rCell As Range 
Dim rRng As Range 

Set rRng = Range("H1:H200") 
For Each rCell In rRng.Cells 
    rCell.TextToColumns Destination:=rCell.Offset(0, 1), Space:=True 
Next 
1

您可以使用For for the inside for。喜歡的東西,

Dim txtStr As String 
Dim aCtr As Integer 
Dim nameVar() As String 
Dim rCell As Range 
Dim rRng As Range 

Set rRng = Range("H1:H200") 
For Each rCell In rRng.Cells 
    txtStr = rCell.Value 
    nameVar = Split(txtStr, " ") 
    For aCtr = 0 To UBound(nameVar) 
     Cells(1, aCtr + 1).Value = nameVar(aCtr) 
    Next 
Next 
0

得出這一結論到底對我自己,感謝儘管

Dim bb As Integer 
Dim text As String 
Dim aa As Integer 
Dim name As Variant 
Dim cc As Range 
Dim rng As Range 
bb = 2 
Set rng = Range("H2:H200") 
For Each cc In rng 
text = cc.Value 
name = Split(text, " ") 
For aa = 0 To UBound(name) 
'Ubound finds the end of an array 
Cells(bb, aa + 1).Value = name(aa) 
Next aa 

BB = BB + 1 接下來

答案