簡體   English   中英

ActiveCell.Formula運行時錯誤1004

[英]ActiveCell.Formula Run-time error 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 您可能希望看到

確定了具有搜索文本的單元格后,只需檢索該單元格的行並使用它。 請參見此示例(未測試)。

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

注意 :我尚未執行任何錯誤處理。 您將不得不照顧它。 例如,如果在F26找到搜索文本怎么辦?

Set rng1 = Range("F15:F100").Find(strSearch, , xlValues, xlWhole)

我認為您的問題就在這里。 嘗試聲明工作表或將變量設置為適當的工作表。

Dim WS as Worksheet
Set WS = Activeworkbook.Sheets("Enter Sheet Name")
Set rng1 = WS.Range("F15:F100").Find(strSearch, , xlValues, xlWhole)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM