Excel VBA - 我在我选择范围内的findnext无效

时间:2016-05-12 22:31:44

标签: excel-vba vba excel

你好,如果有谁可以帮助解决这个问题

我有一系列的1到多组(最多12行 - 每组其他一行后的一行)字符串的初始部分重复我不知道它们是如何重复的,但是我们需要时间提取重复字符串后的所有相关数据。现在不是所有的12行都在组中,有时候有11个或更少,但它们在数据组中没有空行,所以我做的是一旦找到我的标题行,我调用子程序来遍历数据块并做我的提取,但当我回来并使用.findnext(v)时,它不会转到下一个标题

    With big_rng      ' this is column A selected
        Set v = .Find("Submarket:", LookIn:=xlValues, LookAt:=xlPart)
        If Not v Is Nothing Then
            firstAddress = c.Row
            Do
                Call this1(need, c.Row, tech, daily_date)
                Set v = .FindNext(v)
                need = need + 1
            Loop While Not c Is Nothing And c.Address <> firstAddress
        End If
    End With

当我打电话给我时,我正在做的是选择另一个范围,因为我不知道我的数据是否按照正确的顺序使用了另一个选择

    Cells(i, 1).Select ' I know which row my group starts and I select down

    Range(Selection, Selection.End(xlDown)).Select

    ' check for substrings and copy across if the substring exists e.g.

    With Selection

    Set d = .Find("Degradation =", LookIn:=xlValues, LookAt:=xlPart)

            If Not d Is Nothing Then
                spos = InStrRev(Cells(d.Row, 1), "=")

                If Mid(Cells(d.Row, 1), spos + 1, 1) = " " Then
                    output_sht.Cells(n, 5) = Right(Cells(d.Row, 1), Len(Cells(d.Row, 1)) - (spos + 1))
                Else
                    output_sht.Cells(n, 5) = Right(Cells(d.Row, 1), Len(Cells(d.Row, 1)) - spos)
                End If

            Else
                output_sht.Cells(n, 5) = "Error in Data"
            End If

当这个Sub结束时我的原始选择消失了(它是A列:A列),而我的.findnext(v)给了我前一个组的最后一行,而不是下一个组的第一行(如果存在)

如何通过findnext循环,同时保持原始选择

提前谢谢

罗伯特

1 个答案:

答案 0 :(得分:0)

我玩了一下,想出了这段代码 第1行必须高于搜索范围(即标题),因此数据位于地址A2:A10和B2:B10。
我将Submarket:放入A2A4A5&amp; A7中的Sumarket1:B3B4B5&amp; B7

输出是(在各行上):

  

大 - $ A $ 2 sml - $ B $ 3 sml - $ B $ 4 sml - $ B $ 5 sml - $ B $ 7 大 - $ A $ 4 sml    - $ B $ 3 sml - $ B $ 4 sml - $ B $ 5 sml - $ B $ 7 大 - $ A $ 5 sml - $ B $ 3 sml - $ B $ 4 sml - $ B $ 5 sml - $ B $ 7 大 - $ A $ 7 sml - $ B $ 3 sml - $ B $ 4 sml - $ B $ 5   sml - $ B $ 7

Sub test2()
    Dim big_rng As Range
    Dim v As Variant
    Dim foundrow As Long

    Set big_rng = Sheet1.Range("A1:A10")
    With big_rng
        Set v = .Find("Submarket:", LookIn:=xlValues, LookAt:=xlPart)
        If Not v Is Nothing Then
            Do
                foundrow = v.Row
                Debug.Print "big - " & v.Address
                this1
                'Resize the search range to ignore rows already searched.
                With big_rng.Resize(big_rng.Rows.Count - foundrow + 1).Offset(foundrow - 1)
                    Set v = .Find("Submarket:", LookIn:=xlValues, LookAt:=xlPart, SearchDirection:=xlNext)
                End With
            Loop While Not v Is Nothing And v.Row <> foundrow
        End If
    End With

End Sub

Sub this1()

    Dim sml_rng As Range
    Dim v As Variant
    Dim firstAddress As String

    Set sml_rng = Sheet1.Range("B1:B10")

    With sml_rng
        Set v = .Find("Submarket1:", LookIn:=xlValues, LookAt:=xlPart, SearchDirection:=xlNext)
        If Not v Is Nothing Then
            firstAddress = v.Address
            Do
                Debug.Print "sml - " & v.Address
                Set v = .FindNext(v)
            Loop While Not v Is Nothing And v.Address <> firstAddress
        End If
    End With

End Sub