2016-05-12 76 views
2

你好,如果有任何誰可以Excel的VBA - 我在我的selcted範圍FindNext中不工作

我有一個範圍羣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 

當我打電話this1什麼我做的是選擇另一個範圍,因爲我不知道如果我的數據是我用另一種選擇

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 

當這個小組最終我原來選擇了正確的順序(它是列A:A),和我的.findnext(v)給我的最後一行前一組不下一組的第一行(如果存在)

如何通過FindNext中做我環路,同時保持我原來的選擇圓通

預先感謝您

羅伯特

+0

Excel在使用FINDNEXT時使用最後的FIND值,因此它使用'this1'搜索中的值。我想你會用最後找到的項目作爲起點替換'FINDNEXT'和另一個'FIND',但保留firstAddress,以便知道它何時回到起點。 –

回答

0

我有一個玩弄,並提出這個代碼。
行1必須高於搜索範圍(即標題),因此數據位於地址A2:A10和B2:B10中。
我放置在Submarket:A2A4A5 & A7Sumarket1:B3B4B5 & 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 big - $ 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