使用.Find引用单元格时未设置对象变量或With块变量

时间:2018-10-08 16:50:46

标签: excel vba

此方法以前有效,但已停止工作:

Sub LeanCut()
Dim lrow As Long

Cells.Find(What:="~*~*~* Total*", After:=[A1], LookIn:=xlFormulas, _
  LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
  MatchCase:=False).Activate
Rows(ActiveCell.Row & ":" & Rows.Count).Delete

lrow = Cells(Cells.Rows.Count, "A").End(xlUp).Row
Range("A10:I" & lrow).Select

End Sub

我在行上收到运行时错误91

Cells.Find(What:="~*~*~* Total*", After:=[A1], LookIn:=xlFormulas, _
  LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
  MatchCase:=False).Activate

该脚本应该找到包含“ ~~~总计”的单元格,并将其及其下方的所有内容删除,然后选择其上方的范围。

1 个答案:

答案 0 :(得分:0)

更新工作表名称(????

Option Explicit

Sub LeanCut()

Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets("????")
Dim lrow As Long, Found As Range

lrow = ws.Range("A" & ws.Rows.Count).End(xlUp).Row
Set Found = ws.Cells.Find(What:="~*~*~* Total*", After:=ws.Range("A1"), LookIn:=xlFormulas, _
        LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False)

If Not Found Is Nothing Then
    ws.Range(ws.Cells(Found.Row, 1), ws.Cells(lrow, 9)).Delete
Else
    MsgBox "Value not found"
End If

End Sub