PDA

View Full Version : Macro to fill values of row numbers in a 10x20 grid



robertsa123
03-06-2013, 09:08 AM
Hi,

I am trying to run a sub-routine that will fill in the values of row numbers over 10 columns and 20 rows. I want the first value of the second column to be the last value of the previous column +1, as seen below

1 21
2 22
3 23
4 24
5 25
6 26
7 27
8 28
9 29
10 30
11 31
12 32
13 33
14 34
15 35
16 36
17 37
18 38
19 39
20 40

Here is the code i've been using so far:-

Sub sForLoop5()
Dim iCol As Integer
Dim iRow As Integer
Dim iFirstCol As Integer: iFirstCol = 1
Dim iLastCol As Integer: iLastCol = 10
Dim iFirstRow As Integer: iFirstRow = 1
Dim iLastRow As Integer: iLastRow = 20
For iCol = iFirstCol To iLastCol
For iRow = iFirstRow To iLastRow
Cells(iRow, iCol).Value = iRow
Next iRow
Next iCol
End Sub

I am not sure what to put in the value part to get the next column to add 1 to the previous column's last value. If anyone can help that'd be great!

Adam

shrivallabha
03-06-2013, 09:48 AM
Since you are filling up range A1:J20, you can manipulate Excel's formula to do this in non-looping fashion:
Public Sub FillValues()
With Range("A1:J20")
.Formula = "=ROW()+((COLUMN()-1)*20)"
.Value = .Value
End With
End Sub

snb
03-06-2013, 10:14 AM
or

Sub M_snb()
[A1:J20] = [index(20*(column(A:J)-1)+row(1:20),)]
End Sub