[英]If Next statement does not work after the second time loop in VB
I'm very confused why this if statement doesn't work after the second loop.我很困惑为什么这个 if 语句在第二个循环之后不起作用。 I want to pick up the cells which start with "[" and extract the sentence between the "[]".
我想拿起以“[”开头的单元格并提取“[]”之间的句子。 Once extracted, the sentence should be put on the different sheet as a list.
提取后,应将句子作为列表放在不同的工作表上。
This code works only first time when InStr(Cells(i, 1), "[") > 0
.此代码仅在
InStr(Cells(i, 1), "[") > 0
时第一次工作。 However, this test fails thereafter.但是,此后该测试失败。
Where am I going wrong?我哪里错了?
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
That is happening because when the first match takes place in loop, you are activating the output worksheet “Worksheets(“Sheet1).Activate” so the next iteration, the Instr evaluation takes place in output worsheet.这是因为当第一个匹配发生在循环中时,您正在激活 output 工作表“Worksheets(“Sheet1).Activate”,因此下一次迭代,Instr 评估发生在 output 工作表中。
It´s better to have always a variable for your worksheets like:最好始终为您的工作表设置一个变量,例如:
Dim myWsh
Set myWsh = Workbook("x").sheets(1)
So in further code you can reference ojects inside your worksheet without activating it:因此,在进一步的代码中,您可以在不激活工作表的情况下引用工作表中的对象:
myWsh.cells(1,1).value = "my value"
Please try the following, I have added the worksheets variables and remove the inner loop (don´t really know what that was for)请尝试以下操作,我已经添加了工作表变量并删除了内部循环(真的不知道那是什么)
Option Explicit
Public rowCount As Integer
Sub Copy()
Dim databaseWsh As Worksheet: Set databaseWsh = ThisWorkbook.Worksheets("DataBase")
Dim outputWsh As Worksheet: Set outputWsh = ThisWorkbook.Worksheets("Sheet1")
Dim rowCount As Long: rowCount = databaseWsh.Cells(databaseWsh.Rows.Count, 1).End(xlUp).Row
Dim outPutCount As Long: outPutCount = 1
Dim i As Long
For i = 1 To rowCount
Dim startNum As Integer: startNum = 0: Dim endNum As Integer: endNum = 0
If InStr(1, databaseWsh.Cells(i, 1), "[") > 0 Then
startNum = InStr(1, databaseWsh.Cells(i, 1), "[")
endNum = InStr(startNum + 1, databaseWsh.Cells(i, 1), "]")
If startNum <> 0 And endNum <> 0 Then
Dim ExtractionStrings As String
startNum = startNum + 1
ExtractionStrings = Mid(databaseWsh.Cells(i, 1), startNum, endNum - startNum)
outputWsh.Cells(outPutCount, 1) = ExtractionStrings
outPutCount = outPutCount + 1
Else: MsgBox ("Error")
End If
End If
Next i
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.