2017-02-16 46 views
0

我正在使用下面的代碼在excel列中找到一個短語「Phase End」,其中數據存在於1到9個階段。問題是我可以通過其「階段1」「階段2」找到階段1到階段9 ......但是所有階段都具有相同的結束階段,如「階段結束」。當我執行搜索時,是否可以跳過已確定的階段並僅繼續搜索excel列的其餘部分?例如,在確定「階段3」及其「階段結束」後,我想繼續搜索階段3以下的值,但不是在階段3之前。在excel列中繼續文本搜索

Workbooks(OpenWB).Worksheets("Home").Range("B36") = Cells.Find(What:="Phase 1", After:=ActiveCell, LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=True, SearchFormat:=False).Offset(0, -1).Value 
Workbooks(OpenWB).Worksheets("Home").Range("C36") = Cells.Find(What:="End Phase", After:=ActiveCell, LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=True, SearchFormat:=False).Offset(0, -1).Value 

Workbooks(OpenWB).Worksheets("Home").Range("B37") = Cells.Find(What:="phase 2", After:=ActiveCell, LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=True, SearchFormat:=False).Offset(0, -1).Value 
Workbooks(OpenWB).Worksheets("Home").Range("C37") = Cells.Find(What:="end phase", After:=ActiveCell, LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=True, SearchFormat:=False).Offset(0, -1).Value 

Workbooks(OpenWB).Worksheets("Home").Range("B38") = Cells.Find(What:="phase 3", After:=ActiveCell, LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=True, SearchFormat:=False).Offset(0, -1).Value 
Workbooks(OpenWB).Worksheets("Home").Range("C38") = Cells.Find(What:="end phase", After:=ActiveCell, LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=True, SearchFormat:=False).Offset(0, -1).Value 
+0

從本質上講,我要尋找一個更好的搜索單元以同時識別階段1和階段結束。 – santu

回答

0

我不知道我理解你的問題,但也許這將幫助你:

Dim StartRow As Integer 
Dim EndRng As Range 
Dim StartRng As Range 
Dim SearchStr As String 
Dim i As Integer 
Dim NewStartRow As Integer 

StartRow = 35 
SearchStr = "End Phase" 

On Error Resume Next 
For i = 1 To 9 

    NewStartRow = StartRow + i 

    'Set your own search range here 
    Set StartRng = Workbooks(OpenWB).Worksheets("Home").Range("B1:B100").Find(What:="Phase " & i) 
    Workbooks(OpenWB).Worksheets("Home").Range("B" & NewStartRow).Value = Workbooks(OpenWB).Worksheets("Home").Cells(StartRng.Row, StartRng.Column - 1).Value 

    'Finds the first End Phase and renames it (also edit the range here) 
    Set EndRng = Workbooks(OpenWB).Worksheets("Home").Range("C1:C100").Find(What:=SearchStr) 
    Workbooks(OpenWB).Worksheets("Home").Range(EndRng.Address).Value = "End Phase" & i 

'Do whatever you want next 

Next i 
+0

謝謝BjornBogers,這很好。欣賞你即使在我的問題不明確的情況下也能找到解決方案的努力。 – santu

+0

歡迎您:) –

0

你可以試試這個

Sub main24() 
    Dim iPhase As Long 
    Dim afterCell As Range 

    With Workbooks(OpenWB).Worksheets("Home").UsedRange 
     Set afterCell = .Cells(.Count) '<--| set it to the last lookedup range for the first iteration so as to have 'Find()' start from the first one 
     For iPhase = 1 To 9 '<--| iterate through phases 
      Set afterCell = .Find(What:="Phase " & iPhase, After:=afterCell, LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=True, SearchFormat:=False) '<--| set 'afterCell' to the found current "Phase" one 
      .Parent.Range("B36").Offset(iPhase - 1) = afterCell.Offset(0, -1).Value '<--| update corresponding output cell with adjacent value to the left 
      Set afterCell = .Find(What:="End Phase", After:=afterCell, LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=True, SearchFormat:=False) '<--| set 'afterCell' to the found current "End Phase" one 
      .Parent.Range("C36").Offset(iPhase - 1) = afterCell.Offset(, -1).Value '<--| update corresponding output cell with adjacent value to the left 
     Next 
    End With 
End Sub 
+0

你好..好主意。感謝你,現在我可以寫你的幫助 – santu

+0

更好的代碼,歡迎你 – user3598756