【发布时间】:2021-04-07 04:46:39
【问题描述】:
我很困惑为什么这个 if 语句在第二个循环之后不起作用。 我想拿起以“[”开头的单元格并提取“[]”之间的句子。提取后,句子应作为列表放在不同的工作表上。
此代码仅在InStr(Cells(i, 1), "[") > 0 时第一次有效。但是,此后此测试失败。
我哪里错了?
Public rowCount As Integer
Sub Copy()
Dim startNum As Integer, endNum As Integer
Dim str As String
Dim e As Long
Dim le As Long
Worksheets("DataBase").Activate
rowCount = Cells(Rows.Count, 1).End(xlUp).Row
Dim i As Long
Dim ExtractionStrings As String
Dim arr() As Variant
For i = 4 To rowCount
For e = 1 To rowCount
If InStr(Cells(i, 1), "[") > 0 Then
startNum = InStr(Cells(i, 1), "【")
endNum = InStr(startNum + 1, Cells(i, 1), "]")
If startNum <> 0 And endNum <> 0 Then
startNum = startNum + 1
ExtractionStrings = Mid(Cells(i, 1), startNum, endNum - startNum)
str = ExtractionStrings
Worksheets("Sheet1").Activate
Cells(i, 1) = str
Else: MsgBox ("Error")
End If
End If
Next e
Next i
End Sub
【问题讨论】:
-
您正在搜索一个空格和与第一个不同的字符如果:
"【"将其更改为匹配"["` -
哦,谢谢。我已经把【改成[但是结果是一样的...
-
前面的空格你也去掉了吗?您可能希望使用修复更新代码,因为其他人只会看到该错误。
-
为什么有两个重叠的循环?
i从 4 开始并转到rowCount但e从 1 开始并转到rowCount... 此外,正如 Scott 指出的那样,内部InStr语句中有一个空格。 -
^^^ 为什么是内循环?
e未使用。我会删除内部循环,只循环i
标签: arrays excel vba loops if-statement