减去两个范围以在列表的一部分中添加公式

时间:2015-02-17 17:04:04

标签: excel-vba vba excel

我想将公式添加到一个表列中,除了同一列的第一行(其值需要修复)。 所以我试图从他的第一个单元格中减去列数据范围:

Dim rng1 As Range, rng2 As Range, rng3 As Range
rng1 = Sheets("MySheet").ListObjects("MyTable").ListColumns("Column2").DataBodyRange
rng2 = Sheets("MySheet").ListObjects("MyTable").ListColumns("Column2").DataBodyRange.Cells(1)
rng3 = rng1 - rng2
rng3.Formula = "=SUM(D179;G179)"

但这会给我一个执行错误'91'

1 个答案:

答案 0 :(得分:0)

正如加里所说,你需要设定范围, 例如:

 Dim rng1 As Range, rng2 As Range, rng3 As Range
    set rng1 = Sheets("MySheet").ListObjects("MyTable").ListColumns("Column2").DataBodyRange
   set rng2 = Sheets("MySheet").ListObjects("MyTable").ListColumns("Column2").DataBodyRange.Cells(1)
    'rng3 = rng1 - rng2 'you have not indicate what rng3 range is
    rng3.Formula = "=SUM(D179;G179)"'still no indication what rng3 is