How to fill range with formula using VBA?
With xlapp.Workbooks(strXlsFile).Worksheets(sn(i))
lc = .Range("A1").SpecialCells(xlCellTypeLastCell).Colu mn
lr = .Range("A1").SpecialCells(xlCellTypeLastCell).Row
.Cells(1).EntireRow.HorizontalAlignment = xlCenter
.Cells(1).EntireRow.Font.Bold = True
.Range("B2",.Cells( lr,lc).Formula = "=AVERAGE(E2:AH2)"
End With
Actually, the range in which the formula is inserted is okay. It's the
range specified within the formula that needs to be made dynamic.
"=AVERAGE(E2:AH2)" specifies a fixed number of columns. I need a way to
dynamically adjust up to AI or down to AG, for example. I'm wondering if I
can use a different syntax within the formula - will the R1C1 syntax work?
That way I could substitute the lc variable for AH. I suppose I could write
a function that translates the lc value into a letter value... but I doubt
that's the best soution.
|