创build重复的Excelmacros代码,直到没有更多的文字提到

我已经使用loggingfunction创build了这个macros代码。

Sub Macro1() Cells.Find(What:="Text to find", After:=ActiveCell, LookIn:=xlFormulas, _ LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _ MatchCase:=True, SearchFormat:=False).Activate Range("E5").Select ActiveCell.FormulaR1C1 = "text to enter" Range("D6").Select Cells.Find(What:="Text to find", After:=ActiveCell, LookIn:=xlFormulas, _ LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _ MatchCase:=True, SearchFormat:=False).Activate Range("E9").Select ActiveCell.FormulaR1C1 = "text to enter" End Sub 

我需要这个macros继续通过相同的列,直到它不能find更多的search到的单词的实例,而不会返回到列的顶部。

所以它从一个列开始,每次find一个指定的单词时,它会跨越1列并粘贴到指定的单词中。

它继续在同一列中search指定的单词,直到无法从列的顶部开始find它。

希望这是有道理的。

不知道我明白,但我认为你想要的是:

 For each cell in columns(4).cells If cell.value="Text to find" Then Cell.offset(0,1) = "Text to enter" Next cell 

您可以使用FindFindNext快速执行此操作,即:

  • StrOldsearchD列的文本
  • StrIn的文本在列E中input任何匹配

 Sub Recut() Dim strAddress As String Dim StrIn As String Dim StrOut As String Dim rng1 As Range StrOld = "Old" StrIn = "New" Set rng1 = Range("D:D").Find(StrOld, , xlFormulas, xlWhole, , , True) If Not rng1 Is Nothing Then strAddress = rng1.Address Do rng1.Offset(0, 1) = StrIn Set rng1 = Range("D:D").FindNext(rng1) Loop While Not rng1 Is Nothing And rng1.Address <> strAddress End If End Sub