The following code answers your question as asked:
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
Basically, there are three changes: (1) just before issuing the Application.Find
command there is a line telling VBA what to do in case of an error -->
it should go to NextLine
. NewLine
is like a bookmark and can be any name you want. You merely need to tell VBA where this bookmark is. That's the second change in your code: (2) adding a line just before s = s + 1
telling VBA that this is the "bookmark" called NewLine
. The third change is to tell VBA to only use this "bookmark" if the error occurs on the line Application.Find
. In all other cases VBA should just pass the error back to you (the user). So, (3) directly after the line Application.Find
the error trapping is being turned off again.
Yet, a better solution would be to use InStr()
like so:
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