下面的代碼回答你的問題是問:
For xx = 2 To 15494
xword = Cells(s, m)
If xword <> "" Then
le = Len(xword)
On Error GoTo NextLine
sprd = Application.Find(",", xword) '' If I am getting Error
On Error GoTo 0
old_sprd = sprd
's = 1
Star = 1
Do While sprd <> 0
word = Mid(xword, Star, sprd - 1)
xword = Mid(xword, sprd + 1, le)
s = s + 1
Rows(s).Insert
Cells(s, m) = word
sprd = Application.Find(",", xword)
If IsError(sprd) Then sprd = 0
If sprd = 0 Then
s = s + 1
Rows(s).Insert
Cells(s, m) = xword
End If
le = Len(xword)
Loop
End If
NextLine:
s = s + 1 '' My Code supposed to directing divert in This line.
Next
基本上有三個變化:(1)剛發出Application.Find
命令有一條線,告訴VBA如何在發生錯誤的情況下,之前做-->
它應該去NextLine
。 NewLine
就像書籤,可以是任何你想要的名字。你只需要告訴VBA這個書籤在哪裏。這是您的代碼中的第二次更改:(2)在s = s + 1
之前添加一行,告訴VBA這是「書籤」NewLine
。第三個變化是告訴VBA只有在行Application.Find
上發生錯誤時才使用這個「書籤」。在所有其他情況下,VBA應該只是將錯誤傳回給您(用戶)。所以,(3)直接在Application.Find
之後error trapping is being turned off again。
然而,一個更好的解決辦法是使用InStr()
像這樣:
For xx = 2 To 15494
xword = Cells(s, m)
If xword <> "" Then
le = Len(xword)
If InStr(1, xword, ",", vbTextCompare) Then
sprd = Application.Find(",", xword)
old_sprd = sprd
Star = 1
Do While sprd <> 0
word = Mid(xword, Star, sprd - 1)
xword = Mid(xword, sprd + 1, le)
s = s + 1
Rows(s).Insert
Cells(s, m) = word
sprd = Application.Find(",", xword)
If IsError(sprd) Then sprd = 0
If sprd = 0 Then
s = s + 1
Rows(s).Insert
Cells(s, m) = xword
End If
le = Len(xword)
Loop
End If
End If
s = s + 1 '' My Code supposed to directing divert in This line.
Next xx
您通過xx循環,但是在代碼中沒有使用xx? – Luuklag
爲什麼不使用'Instr'而不是'Application.Find'?如果找不到搜索字符串,它將返回0,而不是錯誤值。 – Rory