使Excel公式在添加新行时自动添加
我已经添加了一个用户表单到Excel电子表格,询问用户一个问题,并将其添加到电子表格中的新行。
电子表格中的每一列都有自己的公式,可根据用户input进行计算。
有没有办法让我可以自动将列中连续的公式“拉”到添加的行中?
我想我可以在我的用户表单的代码中改变它,所以当一个人点击“提交”时,所有的单元格都会更新。 但我只知道如何更新价值,而不是添加公式。 这里是我有的代码的一部分:
.Cells(iRow, 6).Value = Me.TextBox1.Value .Cells(iRow, 7).Value = Me.TextBox2.Value .Cells(iRow, 8).Value = Me.TextBox3.Value .Cells(iRow, 9).Value = Me.TextBox4.Value .Cells(iRow, 10).Value = Me.TextBox5.Value .Cells(iRow, 11).Value = Me.TextBox6.Value .Cells(iRow, 12).Value = Me.TextBox7.Value .Cells(iRow, 13).Value = Me.TextBox8.Value .Cells(iRow, 14).Value = Me.TextBox9.Value .Cells(iRow, 15).Value = Me.TextBox10.Value
是否可以添加一些东西来改变单元格值1-4? 也许这样:
.Cells(iRow, 1).Value = *insert formula here*
您可以使用范围相对公式属性.FormulaR1C1
使公式引用相对调整:
.Cells(iRow, 1).FormulaR1C1 = .Cells(iRow - 1, 1).FormulaR1C1
您也可以使用常规的.Formula
属性,而不需要相对引用更新。
您也可以一次完成所有4列:
.Cells(iRow, 1).Resize(, 4).FormulaR1C1 = .Cells(iRow - 1, 1).Resize(, 4).FormulaR1C1
如果公式已经在上面的单元格中,则可以执行以下操作:
.Range(.Cells(iRow - 1, 1).Address,.Cells(iRow, 1).Address).Filldown
这将从上面的单元格填充公式到正在讨论的单元格。