按特定间隔循环复制粘贴

我是一个初学者,试图运行一个这样做的VBA:

  • 从起点复制公式(单元格B6)
  • 将该公式每隔18行粘贴到同一列上
  • 重复该过程,直到单元格显示“报告结束”

我有下面的代码,我不能让它正常运行(只从现有的报告inheritance公式):

'(a) to set the formula at starting point: Windows("RAVEN MNL adj.xlsm").Activate Range("B6").Select ActiveCell.FormulaR1C1 = "=TRIM(RIGHT(RC[-1],7))" '(b) to copy paste in loop Dim i As Long Dim ii As Long Dim strLastCell As Long Dim rng As Range Set rng = Range("B:B").Cells strLastCell = rng.Find(what:="End of Report", After:=rng.Cells(1), Lookat:=xlPart, LookIn:=xlFormulas, SearchOrder:=xlByRows, SearchDirection:=xlPrevious, MatchCase:=False).Row ii = i + 18 i = ActiveCell.Select For ii = i To strLastCell Range("B6").Copy Range("B" & ii).Paste Next ii End Sub 

错误似乎是在“strLastCell”位。 你能帮我吗?

如果你想在B6单元格中的每一个第18个单元格中放置与B6单元格相同的公式,那么直到“报告结束”单元格使用这个公式:

 Sub test() Dim i As Long Dim ii As Long Dim strLastCell As Long Dim rng As Range Set rng = Range("B:B").Cells strLastCell = rng.Find(what:="End of Report", After:=rng.Cells(1), Lookat:=xlPart, LookIn:=xlFormulas, SearchOrder:=xlByRows, SearchDirection:=xlPrevious, MatchCase:=False).ROW For ii = 6 To strLastCell Step 18 Range("B" & ii).FormulaR1C1 = "=TRIM(RIGHT(RC[-1],7))" Next ii End Sub