-1

今日の日付のみの範囲を選択しようとしていますが、Cells.find(TodayLast).Activate 行でランタイム エラー 91 が発生します。何が問題なのか理解できません。

 Sub Escalation()

Dim rng As Range
Dim rngsend As Range
Dim OutApp As Object
Dim OutMail As Object
Dim cell As Range
Dim strto As String
Dim rngHeader As Range
Dim TodayFirst As Range
Dim TodayLast As Range
Dim LastDate As String

' Finds the area of today's range
LastRow = Sheets("Escal").Range("A65536").End(xlUp).Row
Cells(LastRow, 1).Activate
Set TodayLast = ActiveCell
Cells.find(TodayLast).Activate
Set TodayFirst = ActiveCell
Range(TodayFirst, TodayLast.Offset(0, 6)).Select

'Sorterar breacharna - Sorts the breaches
Selection.Sort Key1:=Range("G1"), Key2:=Range("B1"), Key3:=Range("D1")

'A loop that divides the various comps and enters a GoSub formula that prepares mails
Cells(TodayFirst.Row, 7).Activate
Set CompanyFirst = ActiveCell
Do Until IsEmpty(CompanyFirst)
Cells.find(What:=CompanyFirst, LookIn:=xlValues, SearchDirection:=xlPrevious).Activate
Set CompanyLast = ActiveCell
GoSub PrepareMail
Cells(CompanyLast.Row + 1, 7).Activate
Set CompanyFirst = ActiveCell

Loop

Cells(LastRow, 1).Select

Exit Sub
4

2 に答える 2

0

「Escal」がアクティブシートではない可能性があります。最後の行を見つけるときに範囲を完全に修飾しますが、後続のコードでは修飾しません。また、不要なアクティブ化と選択がたくさんあり、まさにあなたが経験している種類のバグにつながります. オブジェクト変数を宣言し、代わりにそれらを使用します。正しいオブジェクトがアクティブであることを確認するオーバーヘッドがなくなり、将来この種のバグを回避できます。

また、常に変数宣言が必要です。(ツール --> オプション...)

次のようにコードを書き直してみてください。

Sub AvoidRTE91()
    Dim ws As Worksheet
    Dim lastRow As Long
    Dim todayLast As Range
    Dim todayFirst As Range
    Dim todayRange As Range

    Set ws = ThisWorkbook.Sheets("Escal")
    lastRow = ws.Range("A65536").End(xlUp).Row

    ' added message for debugging purposes. Remove once you figure out what is wrong.
    MsgBox ws.Cells(lastRow, 1) & " is in cell " & ws.Cells(lastRow, 1).Address
    Set todayLast = ws.Cells(lastRow, 1)
    Set todayFirst = ws.Cells.Find(todayLast)
    ' you don't have to select the range to work with it
    Set todayRange = ws.Range(todayFirst, todayLast.Offset(0, 6))
    ' now instead of using selection, use todayRange...

End Sub
于 2013-07-24T13:58:39.250 に答える