VB代码错误'vba中的运行时错误1004'对象“_global failed”的方法单元格。

时间:2013-05-06 20:03:42

标签: excel-vba vba excel

我收到了这个错误

runtime error 1004 in VB "method cells of object global_failed"

这些是我的代码:

Set oXL = EDOffice1.GetApplication()

Dim oWB As Excel.Workbook

Set oWB = EDOffice1.ActiveDocument()

Dim oSheet As Excel.Worksheet

Dim oRng As Excel.Range

Dim SUM As Long

Set oSheet = oWB.ActiveSheet


Range("F1").Formula = "=SUM(" & Range(Cells(2, 2), Cells(25, 2)).Address(False, False) & ")"

请帮忙。

2 个答案:

答案 0 :(得分:1)

也许您不在sheet代码内,无需参考表格即可致电Cells

尝试识别您要执行此操作的工作表并使用:

Sheet.Range("F1").Formula = "=SUM(" & Sheet.Range(Sheet.Cells(2, 2), Sheet.Cells(25, 2)).Address(False, False) & ")"

答案 1 :(得分:0)

试试这个(UNTESTED)

将第Range("F1").Formula = "=SUM(" & Range(Cells(2, 2), Cells(25, 2)).Address(False, False) & ")"行更改为

With oSheet 
    .Range("F1").Formula = "=SUM(" & _
                           .Range(.Cells(2, 2), .Cells(25, 2)).Address(False, False) & _
                           ")"
End With