score:2

Accepted answer

yes, although i suspect not quite in the way you're thinking. use an array instead of separate variables rowa, rowb and rowc, so you'd replace

dim rowa as long, rowb as long, rowc as long
rowa = 1
rowb = 3
rowc = 7 'example figures

with -

dim row(1 to 3) as long
row(1) = 1
row(2) = 3
row(3) = 7

then your above code can be shortened to the following loop:

for a = lbound(row) to ubound(row)
    .range("r" & row(a)).formular1c1 = "=rc[-1]"
    .range("w" & row(a)).formular1c1 = "=(rc[-5]*rc[-7])+rc[-2]"
    .range("y" & row(a)).formular1c1 = "=rc[-12]*rc[-2]"
next

score:1

maybe something like this:

sub test()
dim letters, rows, i, letter, row

letters = array("r", "w", "y")
rows = array(1, 5, 17)          'rowa, rowb, and so on...
i = 0
j = 0
for each row in rows
    for each letter in letters
        debug.print letters(i) & rows(j)
    '    .range(letters(i) & rows(i)).formular1c1 = "=rc[-1]"
    '    .range(letters(i) & rows(i)).formular1c1 = "=(rc[-5]*rc[-7])+rc[-2]"
    '    .range(letters(i) & rows(i)).formular1c1 = "=rc[-12]*rc[-2]"
         
        i = i + 1
    next letter
    j = j + 1
    i = 0
next row

end sub

score:1

using 'helper' procedures

option explicit

sub usingrefrows()
    
    const rowa as long = 1
    const rowb as long = 3
    const rowc as long = 5
    
    dim ws as worksheet: set ws = sheet1
    
    dim rrg as range: set rrg = refrows(ws, rowa, rowb, rowc)
    
    intersect(rrg, ws.columns("r")).formular1c1 = "=rc[-1]"
    intersect(rrg, ws.columns("w")).formular1c1 = "=rc[-5]*rc[-7]+rc[-2]"
    intersect(rrg, ws.columns("y")).formular1c1 = "=rc[-12]*rc[-2]"

end sub

sub usingrefrowsandwriterowsr1c1()
    
    const rowa as long = 1
    const rowb as long = 3
    const rowc as long = 5
    
    const columnslist as string = "r,w,y"
    const formulaslist as string _
        = "=rc[-1]" & "," _
        & "=rc[-5]*rc[-7]+rc[-2]" & "," _
        & "=rc[-12]*rc[-2]"
    
    dim ws as worksheet: set ws = sheet1
    
    dim rrg as range: set rrg = refrows(ws, rowa, rowb, rowc)
    
    writerowsr1c1 rrg, columnslist, formulaslist
    
end sub


function refrows( _
    byval ws as worksheet, _
    paramarray datarows() as variant) _
as range
    dim rg as range
    dim n as long
    for n = 0 to ubound(datarows)
        if rg is nothing then
            set rg = ws.rows(datarows(n))
        else
            set rg = union(rg, ws.rows(datarows(n)))
        end if
    next n
    if not rg is nothing then
        set refrows = rg
    end if
end function

sub writerowsr1c1( _
        byval rowsrange as range, _
        byval columnslist as string, _
        byval formulaslist as string)
    
    dim cols() as string: cols = split(columnslist, ",")
    dim formulas() as string: formulas = split(formulaslist, ",")
    dim ws as worksheet: set ws = rowsrange.worksheet
    
    dim n as long
    for n = 0 to ubound(cols)
        intersect(rowsrange, ws.columns(cols(n))).formular1c1 = formulas(n)
    next n
    
end sub

score:2

i would store the rows in an array, then use union to collect all the rows in a variable and intersect that with each column.

this way you can access all defined rows of a specific column at once.

option explicit

public sub example()
    dim rowarr() as variant
    rowarr = array(1, 3, 17) 'define your rows here
    
    dim allrows as range
        
    with activesheet
        dim row as variant
        for each row in rowarr
            if allrows is nothing then
                set allrows = .rows(row)
            else
                set allrows = union(allrows, .rows(row))
            end if
        next row
        
        'write in all rows of a specific column
        intersect(.columns("r"), allrows).formular1c1 = "=rc[-1]"
        intersect(.columns("w"), allrows).formular1c1 = "=(rc[-5]*rc[-7])+rc[-2]"
        intersect(.columns("y"), allrows).formular1c1 = "=rc[-12]*rc[-2]"
    end with
end sub

instead of the loop you can also write:

set allrows = .range("1:1,3:3,17:17")

like

option explicit

public sub example()
    with activesheet
        dim allrows as range
        set allrows = .range("1:1,3:3,17:17")
        
        'write in all rows of a specific column
        intersect(.columns("r"), allrows).formular1c1 = "=rc[-1]"
        intersect(.columns("w"), allrows).formular1c1 = "=(rc[-5]*rc[-7])+rc[-2]"
        intersect(.columns("y"), allrows).formular1c1 = "=rc[-12]*rc[-2]"
    end with
end sub

but this works only for a smaller amount of rows. if you have more you need to use union


Related Query

More Query from same tag