Set formula to a range of cells

萝らか妹 提交于 2020-01-09 09:24:06

问题


this is simple demo of what i want to do. I want to set a formula to a range of cells(eg. C1 to C10).

Range("C1").Formula = "=A1+B1"

but how to make formula use dynamic cells like this:

Range("C1:C10").Formula = "=Ax+Bx"

so in reality it is like,

C1 = A1 + B1
C2 = A2 + B2
C3 = A3 + B3
C4 = A4 + B4
C5 = A5 + B5
...
C10 = A10 + B10

how to change RHS of this formula to make above working: Range("C1:C10").Formula = "=Ax+Bx"


回答1:


I would update the formula in C1. Then copy the formula from C1 and paste it till C10...

Not sure about a more elegant solution

Range("C1").Formula = "=A1+B1"
Range("C1").Copy
Range("C1:C10").Pastespecial(XlPasteall)



回答2:


Range("C1:C10").Formula = "=A1+B1"

Simple as that.

It autofills (FillDown) the range with the formula.




回答3:


I think this is the simplest answer possible: 2 lines and very comprehensible. It emulates the functionality of dragging a formula written in a cell across a range of cells.

Range("C1").Formula = "=A1+B1"
Range("C1:C10").FillDown



回答4:


Use FormulaR1C1:

Cells((1,3),(10,3)).FormulaR1C1 = "=RC[-2]+RC[-1]"

Unlike Formula, FormulaR1C1 has relative referencing.




回答5:


Use this

            Sub calc()


            Range("C1:C10").FormulaR1C1 = "=(R10C1+R10C2)"


            End Sub



回答6:


If you're trying to fill a column, you can do this on Excel 2010 with a single keyboard command:

  1. Set the formula of the upper-left-most cell
  2. Select the entire range
  3. Hit Ctrl-d

Ctrl-D will either fill-across a selected row, or fill-down a selected array or column. Relative referencing is applied.



来源:https://stackoverflow.com/questions/15522538/set-formula-to-a-range-of-cells

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!