Consulting

Results 1 to 3 of 3

Thread: Macro to fill values of row numbers in a 10x20 grid

  1. #1

    Exclamation Macro to fill values of row numbers in a 10x20 grid

    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

  2. #2
    VBAX Expert shrivallabha's Avatar
    Joined
    Jan 2010
    Location
    Mumbai
    Posts
    750
    Location
    Since you are filling up range A1:J20, you can manipulate Excel's formula to do this in non-looping fashion:
    [VBA]Public Sub FillValues()
    With Range("A1:J20")
    .Formula = "=ROW()+((COLUMN()-1)*20)"
    .Value = .Value
    End With
    End Sub
    [/VBA]
    Regards,
    --------------------------------------------------------------------------------------------------------
    Shrivallabha
    --------------------------------------------------------------------------------------------------------
    Using Excel 2016 in Home / 2010 in Office
    --------------------------------------------------------------------------------------------------------

  3. #3
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,638
    or
    [vba]
    Sub M_snb()
    [A1:J20] = [index(20*(column(A:J)-1)+row(1:20),)]
    End Sub
    [/vba]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •