ActiveCell.Formula运行时错误1004
我试图在“总服务费”单元格上列G:26到1的总和。 我遇到运行时错误“1004”。 应用程序定义或对象定义的错误。 有谁知道这可能是从什么?
Worksheets(1).Select Dim rng1 As Range Dim strSearch As String strSearch = "Total Service Fees" Set rng1 = Range("F15:F100").Find(strSearch, , xlValues, xlWhole) If Not rng1 Is Nothing Then rng1.Offset(0, 1).Select ActiveCell.Formula = "=SUM(G26:" & ActiveCell.Offset(-1, 0).Select & ")" Else MsgBox strSearch & " not found" End If
我得到正确的答案,但我仍然得到这个错误。
避免使用SELECT
。 你可能想看到这个
当你确定了具有search文本的单元格时,只需检索该单元格的行并使用它。 看到这个例子(UNTESTED)。
Dim rng1 As Range Dim strSearch As String Dim r As Long With Worksheets(1) strSearch = "Total Service Fees" Set rng1 = .Range("F15:F100").Find(strSearch, , xlValues, xlWhole) If Not rng1 Is Nothing Then r = rng1.Row .Range("G" & r).Formula = "=SUM(G26:G" & r & ")" Else MsgBox strSearch & " not found" End If End With
注意 :我没有做任何error handling。 你将不得不照顾这一点。 例如,如果在F26
findsearch文本呢?
Set rng1 = Range("F15:F100").Find(strSearch, , xlValues, xlWhole)
我认为你的问题在这里。 尝试声明工作表或将variables设置为适当的表格。
Dim WS as Worksheet Set WS = Activeworkbook.Sheets("Enter Sheet Name") Set rng1 = WS.Range("F15:F100").Find(strSearch, , xlValues, xlWhole)