Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
195 views
in Technique[技术] by (71.8m points)

arrays - Excel macro code for every 2nd row to increase by 1

I have a list of numbers (over 100 rows). I want every 2nd number in each group of 3's row to increase by 1 from its previous row.

Think in groups of 3. The first value is given. The 2nd value is 1 higher than the first, and the 3rd value can be anything. This pattern continues for the next 3. For example, if Row 1 = 1, Row 2 = 2 (because increase 1 from previous row). Row 3 = any #. Row 4 = 5, Row 5 = 6 (because increase 1 from previous row). Row 6 = any number. Row 7 = 7, Row 8 = 8 (because 1 from orevious), Row 9 = any.

I can't figure out how to loop through this. THis is what I tried.

Sub DoSomething
  For ColNum = 4 To 500 Step 2
    Range(Cells(3, ColNum), Cells(1159, ColNum)).FormulaR1C1 = "='H2'+1"
  Next ColNum
End Sub 
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If I understand your question correctly, you may want somthing like:

Sub test()

For i = 3 To 1159 Step 3

     Cells(i + 1, 1).FormulaR1C1 = "=R[-1]C+1"

Next i

End Sub

Starting in Row 3, up to row 1159, in sets of 3, grab the next cell and enter a formula that increases the value of the cell in the row above by 1.

Here are screenshots to illustrate the code. Left is before, right is after. Column B has =Formulatext(A3) copied down.

Thinking in groups of three, every second cell in each group of three is treated to the formula.

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...