尝试此解决方案:包装
Find
在一个
On Error Resume Next
,那么如果您的返回结果是空字符串,它将打击您的
Else
条件,您可以在单元格中得到最后一个词:
编辑:经过进一步的测试,当移到下一行时,这似乎有点搞砸了…但这以前有用吗?
Dim a As Double
Dim b As Double
Dim c As Variant
Dim d As Integer
Dim e As String
Dim f As Double
Dim g As Variant
Dim h As Variant
Dim i As Integer
a = 1
f = 2
i = 1
b = Len(Cells(i, 1))
While Cells(i, 1) <> vbNullString
While a < b
c = vbNullString
d = 0
e = vbNullString
On Error Resume Next
c = WorksheetFunction.Find(Chr(32), Cells(i, 1), a)
On Error GoTo 0
If c <> "" Then
d = c - a
Else
d = b - a + 1
End If
e = Mid(Cells(1, 1), a, d)
If Left(e, 4) = "true" Then
e = "'" & e
ElseIf Left(e, 5) = "false" Then
e = "'" & e
End If
If e <> vbNullString Then
Worksheets("Words").Cells(f, 1) = WorksheetFunction.Trim(e)
f = f + 1
End If
If c <> "" Then
a = c + 1
Else
a = a + d
End If
Wend
i = i + 1
b = Len(Cells(i, 1))
a = 1
Wend
如果我是白手起家写的话,我会这样做。通过使用
Split
,我们可以用一个特定的分隔符(在我们的示例中是空格字符)分隔单元格的值,然后将所有这些字符写入其目标:
Option Explicit
Sub Test()
Dim sht As Worksheet
Dim i As Long, j As Long, k As Long
Dim temparr As Variant
Set sht = ActiveSheet
k = 1
For i = 1 To sht.Cells(sht.Rows.Count, 1).End(xlUp).Row
temparr = Split(sht.Cells(i, 1).Value, " ")
For j = 0 To UBound(temparr)
Sheets("Words").Cells(k, 1).Value = temparr(j)
k = k + 1
Next j
Next i
End Sub