【问题标题】:If Next statement does not work after the second time loop in VB如果在VB中第二次循环后Next语句不起作用
【发布时间】: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 开始并转到 rowCounte 从 1 开始并转到 rowCount... 此外,正如 Scott 指出的那样,内部 InStr 语句中有一个空格。
  • ^^^ 为什么是内循环? e 未使用。我会删除内部循环,只循环i

标签: arrays excel vba loops if-statement


【解决方案1】:

这是因为当第一个匹配发生在循环中时,您正在激活输出工作表“Worksheets(“Sheet1).Activate”,因此下一次迭代,Instr 评估发生在输出工作表中。

最好始终为您的工作表设置一个变量,例如:

 Dim myWsh 
 Set myWsh  = Workbook("x").sheets(1)

因此,在进一步的代码中,您可以在不激活工作表的情况下引用对象:

myWsh.cells(1,1).value = "my value"

请尝试以下操作,我已经添加了工作表变量并删除了内部循环(真的不知道这是做什么用的)

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

【讨论】:

    猜你喜欢
    • 2016-01-03
    • 1970-01-01
    • 2013-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-26
    • 1970-01-01
    相关资源
    最近更新 更多