我如何循环这个代码,直到行x到达?

在我的电子表格的G列中,我想从另一列中提取数据。 但不是每一行,只有每一行。 我如何写一个For Until循环,每隔三行,如何结束循环?

这是我需要的G代码,每行第三行,直到第350行。

ActiveCell.FormulaR1C1 = "=RC[-1]" Range("G10").Select ActiveCell.FormulaR1C1 = "=RC[-1]" Range("G13").Select ActiveCell.FormulaR1C1 = "=RC[-1]" Range("G16").Select ActiveCell.FormulaR1C1 = "=RC[-1]" 

另外,任何人都可以推荐一个在线学习VBAmacros在Excel中的资源?

为了给你一个出发点,试试

 Sub Test() Dim MyRange As Range, Idx As Integer Set MyRange = [G10] ' define a range object Idx = 1 ' row index Do While True ' see note MyRange(Idx, 1).FormulaR1C1 = "=RC[-1]" Idx = Idx + 3 ' advance by 3 rows Loop End Sub 

这里你正在使用Range对象而不是select和激活单元格。

重要事项 :用一个更合适的终止条件replaceDo While True …就像这样,你正在创build一个无限循环,这对于在debugging器中进行testing是有好处的,而对于“无人值守”的执行则是不好的。

合适的终止条件可以是

 Do While MyRange(Idx, 1) <> "" ' as long as G is not empty Do While MyRange(Idx, 0) <> "" ' as long as F is not empty DO While Idx < 200 ' until a certain position is reached 

您可以使用下面的Step 3

 Sub loopThroughColumn() Dim i As Integer Dim LastRow As Integer LastRow = ActiveSheet.Cells(ActiveSheet.Rows.Count, "G").End(xlUp).Row For i = 1 To LastRow Step 3 Range("G" & i).FormulaR1C1 = "=RC[-1]" MsgBox Range("G" & i).Value Next End Sub 

你可以在这里find在线学习VBA的资源