循环行直到值不等于续

这是较大代码的一小部分。 基本上如果单元格包含单词继续,我需要在上面的单元格中查看,如果该单元格包含单词继续,那么我需要继续循环行,直到find一个不连续的值。 这是我到目前为止?

Do If .Cells(SourceCell.Row, 3).Value = "continued." Then wsFHA.Cells(RowCounter, 7).Value = .Cells(SourceCell.Offset(rowoffset:=-1).Row, 3).Value End If Loop Until .Cells(SourceCell.Row, 3).Value <> "continued." 

上面的代码是这个代码的一小部分代码是search失败模式和原因。 但是在源数据中有时会重复相同的值。 在这种情况下单词继续出现在单元格中,并且您必须参考上面单元格中的信息。 然而,为了数据的目的,我需要的是实际的信息,而不是继续。 即时通讯试图使代码find这个信息,但我挣扎。

 Sub Create_FHA_Table() Dim Headers() As String: Headers = _ Split("FHA Ref,Engine Effect,Part No,Part Name,FM ID,Failure Mode & Cause,FMCM,PTR,ETR", ",") If Not WorksheetExists("FHA") Then Worksheets.Add().Name = "FHA" Dim wsFHA As Worksheet: Set wsFHA = Sheets("FHA") wsFHA.Move after:=Worksheets(Worksheets.Count) wsFHA.Cells.Clear Application.ScreenUpdating = False With wsFHA For i = 0 To UBound(Headers) .Cells(2, i + 2) = Headers(i) .Columns(i + 2).EntireColumn.AutoFit Next i .Cells(1, 2) = "FHA TABLE" .Range(.Cells(1, 2), .Cells(1, UBound(Headers) + 2)).MergeCells = True .Range(.Cells(1, 2), .Cells(1, UBound(Headers) + 2)).HorizontalAlignment = xlCenter .Range(.Cells(1, 2), .Cells(2, UBound(Headers) + 2)).Font.Bold = True End With Dim RowCounter As Long: RowCounter = 3 Dim SearchTarget As String 'must copy and paste between these bookmarks for each new code, "SearchTarget#" SearchTarget = "9.1" 'Must update SearchTarget# Dim SourceCell As Range, FirstAdr As String If Worksheets.Count > 1 Then For i = 1 To Worksheets.Count - 1 With Sheets(i) Set SourceCell = .Columns(7).Find(SearchTarget, LookAt:=xlWhole) 'Must Update SearchTarget# to correspond with above If Not SourceCell Is Nothing Then FirstAdr = SourceCell.Address Do wsFHA.Cells(RowCounter, 2).Value = SearchTarget 'Must Update SearchTarget# to correspond with above wsFHA.Cells(RowCounter, 3).Value = .Cells(SourceCell.Row, 6).Value wsFHA.Cells(RowCounter, 4).Value = .Cells(3, 10).Value wsFHA.Cells(RowCounter, 5).Value = .Cells(2, 10).Value wsFHA.Cells(RowCounter, 6).Value = .Cells(SourceCell.Row, 2).Value wsFHA.Cells(RowCounter, 7).Value = .Cells(SourceCell.Row, 3).Value If .Cells(SourceCell.Row, 3).Value = "continued." Then wsFHA.Cells(RowCounter, 7).Value = .Cells(SourceCell.Offset(rowoffset:=-1).Row, 3).Value End If wsFHA.Cells(RowCounter, 8).Value = .Cells(SourceCell.Row, 14).Value Set SourceCell = .Columns(7).FindNext(SourceCell) RowCounter = RowCounter + 1 Loop While Not SourceCell Is Nothing And SourceCell.Address <> FirstAdr End If End With Next i End If Application.ScreenUpdating = True End Sub Public Function WorksheetExists(ByVal WorksheetName As String) As Boolean On Error Resume Next WorksheetExists = (ThisWorkbook.Sheets(WorksheetName).Name <> "") On Error GoTo 0 

这应该工作…

  For j = 0 To SourceCell.Row - 1 If .Cells(SourceCell.Row - j, 3).Value <> "continued." Then wsFHA.Cells(RowCounter, 7).Value = .Cells(SourceCell.Row - j, 3).Value Exit For End If Next j 

并添加更多的search条件用下面的代码replace主代码循环…

  Dim SourceCell As Range, FirstAdr As String Dim RowCounter As Long: RowCounter = 3 Dim SearchTarget() As String SearchTarget = Split("9.1,SearchItem 2,etc...", ",") For i = 0 To UBound(SearchTarget) If Worksheets.Count > 1 Then For j = 1 To Worksheets.Count - 1 With Sheets(j) Set SourceCell = .Columns(7).Find(SearchTarget(i), LookAt:=xlWhole) If Not SourceCell Is Nothing Then FirstAdr = SourceCell.Address Do wsFHA.Cells(RowCounter, 2).Value = SearchTarget(i) wsFHA.Cells(RowCounter, 3).Value = .Cells(SourceCell.Row, 6).Value wsFHA.Cells(RowCounter, 4).Value = .Cells(3, 10).Value wsFHA.Cells(RowCounter, 5).Value = .Cells(2, 3).Value wsFHA.Cells(RowCounter, 6).Value = .Cells(SourceCell.Row, 2).Value For k = 0 To SourceCell.Row - 1 If .Cells(SourceCell.Row - k, 3).Value <> "continue." Then wsFHA.Cells(RowCounter, 7).Value = .Cells(SourceCell.Row - k, 3).Value Exit For End If Next k wsFHA.Cells(RowCounter, 8).Value = .Cells(SourceCell.Row, 14).Value Set SourceCell = .Columns(7).FindNext(SourceCell) RowCounter = RowCounter + 1 Loop While Not SourceCell Is Nothing And SourceCell.Address <> FirstAdr End If End With Next j End If Next i 

你将需要编辑你的术语数组,虽然,用逗号分隔每个…我也调整了循环variables为i,j,k顺序,所以有一个细微的差别,第一个代码块

  SearchTarget = Split("9.1,SearchItem 2,etc...", ",") 

要向后循环,可以使用for循环和step - 1

你将需要知道你开始的最低行是什么。 如果这只是你列中的最后一行,你可以使用它。

 Dim lastRow As Long lastRow = ActiveSheet.Cells(ActiveSheet.Rows.Count, 1).End(xlUp).Row 

然后,您循环到具有值的最高单元格,如果这是您的第一行,它将是1:

 For i = lastRow To 1 Step -1 If .Cells(i, 1) <> "continue" Then ' Do things when the value doesn't equal continue here. Exit For End If Next i