我使用下面的代码来查找短语" Phase End"在excel列中,数据存在1到9个阶段。问题是我可以通过它的#34;找到阶段1到9。第1阶段" "阶段2" ...但是所有阶段都有相同的结束短语,例如" Phase End"。当我进行搜索时,是否可以跳过已识别的阶段并仅在excel列的剩余部分继续搜索?例如,在识别"阶段3"及其#34;阶段结束"我想继续搜索第3阶段以下的值,但不是在它们之前。
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 :(得分: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
答案 1 :(得分: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