簡體   English   中英

Excel VBA:嵌套循環中的FindNext

[英]Excel VBA: FindNext in nested loops

我試圖在另一個已經使用.Find的循環中使用.Find函數創建一個循環。 我想搜索已保存在數組中的字符串。

例如,這些是保存在Sheet1中的數組strItem中的字符串值。

"unit1", "unit2", "unit3"

我想從Sheet2逐一搜索它們。 Sheet2看起來像這樣:

unit1
unit2
unit3
unit1.pdf
text1
subject1
subject2
subject3
text2
=========
unit2.pdf
text1
subject1
subject2
subject3
text2
=========
unit3.pdf
text1
subject1
subject2
subject3
text2
=========

搜索"unit1.pdf" ,我搜索其下面的所有單元格中的“subject”,並獲取subject1,2和3的單元格值。搜索“subject”單元格應該停在包含“==”的下一個單元格==”。

接下來,我搜索"unit2" ,如果發現像以前一樣在其下搜索“subject”單元格。 再次,停在包含“====”的單元格。 等等。

在我的代碼中,我想要做的是

  • 搜索字符串“unit”。
  • 使用.row作為范圍開始搜索“主題”。
  • 返回所有主題,直到單元格包含"====" 這是我的代碼的一部分,我無法真正使用

碼:

Wb2.Sheets("Sheet2").Activate
With Wb2.Sheets("Sheet2").Range("A1:A1048575")
    For Each strItem In arrExcelValues
        myStr = strItem & ".pdf"
        Set p = .Find(What:=myStr, LookIn:=xlValues, _
                      LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False)
        If Not p Is Nothing Then
            firstAddress = p.Address
            Do
                myStr2 = p.row
                strStart = "A" & myStr2
                strEnd = "A1048575"

                With Wb2.Sheets("Sheet2").Range(strStart, strEnd)
                    Set p1 = .Find(What:="Subject", LookIn:=xlValues, _
                                   LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False)
                    If Not p1 Is Nothing Then
                        firstAddress = p1.Address
                        Do
                            myStr2 = myStr2 + 1
                            If p1.Offset(myStr2, 0).Value = "====" Then
                                Exit Do
                            Else
                                MsgBox p1.Value & strItem
                            End If
                            Set p1 = .FindNext(p1)
                        Loop While Not p1 Is Nothing And p1.Address <> firstAddress
                    Else
                        MsgBox "Not found"
                    End If
                End With
                    Set p = .FindNext(p)
            Loop While Not p Is Nothing And p.Address <> firstAddress
        Else
            MsgBox "Not found"
        End If
    Next
End With

你離這里不遠,但有幾點需要考慮:

  • 您似乎知道數據的順序,您可以使用它來比在整個列上使用Find更容易。
  • 除非進一步使用子元素,否則不能使用嵌套的With語句。 你想要完全符合條件,這很好,但要小心。 例如,

     ' This is okay With ThisWorkbook.Sheets("Sheet2") With .Range("A1") MsbBox .Value End With With .Range("A2") MsgBox .Value End With End With ' This is not okay, and present in your code With ThisWorkbook.Sheets("Sheet2").Range("A1") MsgBox .Value With ThisWorkbook.Sheets("Sheet2").Range("A2") Msgbox .Value End With End With 

我已經在您的代碼中采用了這些想法,並重新編寫它以使其更清晰,並希望實現您想要的。 有關詳細信息,請參閱注釋

Dim Wb2 As Workbook
Dim lastRow As Long
Set Wb2 = ThisWorkbook
' Get last used row in sheet, so search isn't on entire column
lastRow = Wb2.Sheets("Sheet2").UsedRange.Rows.Count
' Set up array of "unit" values
Dim arrExcelValues() As String
arrExcelValues = Split("unit1,unit2,unit3", ",")

' Declare variables
Dim pdfCell As Range
Dim eqCell As Range
Dim eqRow As Long
eqRow = 1
Dim subjCell As Range
Dim strItem As Variant

' Loop over unit array
With Wb2.Sheets("Sheet2")
For Each strItem In arrExcelValues
    ' Find the next "unitX.pdf" cell after the last equals row (equals row starts at 1)
    Set pdfCell = .Range("A" & eqRow, "A" & lastRow).Find(what:=strItem & ".pdf", lookat:=xlPart)
    If Not pdfCell Is Nothing Then
        ' pdf row found, find next equals row, store row value or use last row
        Set eqCell = .Range("A" & pdfCell.Row, "A" & lastRow).Find(what:="====", lookat:=xlPart)
        If eqCell Is Nothing Then
            eqRow = lastRow
        Else
            eqRow = eqCell.Row
        End If
        ' Loop through cells between pdf row and equals row
        For Each subjCell In .Range("A" & pdfCell.Row, "A" & eqRow)
            ' If cell contents contain the word "subject" then do something (display message)
            If InStr(UCase(subjCell.Value), "SUBJECT") > 0 Then
                MsgBox "Subject: " & subjCell.Value & ", Unit: " & strItem
            End If
        Next subjCell
    Else
        MsgBox "Item not found: " & strItem & ".pdf"
    End If
Next strItem
End With   

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM