In column G of my spreadsheet, I would like pull in data from another column. But not each row, only every third row. How can I write a For Until loop that takes every third row and also how do I end the loop?
在我的电子表格的G列中,我想从另一列中提取数据。但不是每一行,只是每第三行。我怎样才能写一个For循环,这个循环占据了所有的第三行,我怎么结束循环呢?
Here is the code that I need looped every third row in the G column, until row 350.
这是我在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]"
Also, can anyone recommend an online resource for learning VBA macro-ing in excel?
另外,有没有人可以推荐一个学习VBA在excel中进行宏操作的在线资源?
2 个解决方案
#1
1
To give you a starting point, try
为了给你一个起点,尝试一下
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
Here you are working with a Range object rather than selecting and activating cells.
这里使用的是Range对象,而不是选择和激活单元格。
Important: Replace Do While True
by a more suitable termination condition ... like this you're creating an endless loop which is good for testing in the debugger but not for "unattended" execution.
重要提示:用更合适的终止条件替换Do,同时替换为True…就像这样,您正在创建一个无穷无尽的循环,它适合于调试器中的测试,但不适用于“无人值守”的执行。
Suitable termination conditions could be
合适的终止条件可以是
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
#2
0
You can use the Step 3
like shown below
您可以使用步骤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
And you can find online resource for learning VBA here
你可以在这里找到学习VBA的在线资源
#1
1
To give you a starting point, try
为了给你一个起点,尝试一下
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
Here you are working with a Range object rather than selecting and activating cells.
这里使用的是Range对象,而不是选择和激活单元格。
Important: Replace Do While True
by a more suitable termination condition ... like this you're creating an endless loop which is good for testing in the debugger but not for "unattended" execution.
重要提示:用更合适的终止条件替换Do,同时替换为True…就像这样,您正在创建一个无穷无尽的循环,它适合于调试器中的测试,但不适用于“无人值守”的执行。
Suitable termination conditions could be
合适的终止条件可以是
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
#2
0
You can use the Step 3
like shown below
您可以使用步骤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
And you can find online resource for learning VBA here
你可以在这里找到学习VBA的在线资源