错误91;未设置对象变量或带块变量

时间:2018-10-09 06:02:09

标签: excel vba excel-vba

我的问题是,为什么每当我尝试调试该错误时都是Runtime error 91;未设置对象变量或带块变量。我尝试查看另一个示例,并尝试在论坛中找到解决方案,但仍然找不到。

顺便说一句,当我调试时,它将在

突出显示
findvalue.EntireRow.Delete

我可以知道这是什么错误吗?希望有人可以向我解释。

谢谢。

Private Sub cmdDelete_Click()

Dim findvalue As Range
Dim cDelete As VbMsgBoxResult
Dim cNum As Integer
Dim DataSH As Worksheet
Set DataSH = Sheet1
Dim x As Integer


Application.ScreenUpdating = False

If Emp1.Value = "" Or Emp2.Value = "" Then
MsgBox "There is not data to delete"
Exit Sub
End If

cDelete = MsgBox("Are you sure that you want to delete this training", _
vbYesNo + vbDefaultButton2, "Are you sure????")
If cDelete = vbYes Then

Set findvalue = DataSH.Range("B:B").Find(What:=Me.Emp1.Value, _
LookIn:=xlValues, LookAt:=xlWhole)

findvalue.EntireRow.Delete
End If

cNum = 7
For x = 1 To cNum
Me.Controls("Emp" & x).Value = ""
Next

DataSH.Range("A2").CurrentRegion.AdvancedFilter Action:=xlFilterCopy, _
CriteriaRange:=Range("Data!$L$8:$L$9"), CopyToRange:=Range("Data!$N$8:$T$8"), _
Unique:=False

If DataSH.Range("N9").Value = "" Then
lstEmployee.RowSource = ""
Else

lstEmployee.RowSource = DataSH.Range("outdata").Address(external:=True)
End If

DataSH.Select
With DataSH
.Range("A2:G10000").Sort Key1:=Range("E2"), Order1:=xlAscending, Header:=xlGuess
End With

Sheet1.Select

On Error GoTo 0
Exit Sub

End Sub

1 个答案:

答案 0 :(得分:0)

您只是没有从Data工作表的B列的Me.Emp1.Value中找到值,所以没有要删除的内容。也许扩展Find参数; matchcase:= false浮现在脑海。

错误控制:

Set findvalue = DataSH.Range("B:B").Find(What:=Me.Emp1.Value, _
LookIn:=xlValues, LookAt:=xlWhole)

if not findvalue is nothing then findvalue.EntireRow.Delete

备用:

dim m as string
m = application.match(Me.Emp1.Value, DataSH.Range("B:B"), 0)
if not iserror(m) then DataSH.rows(m).entirerow.delete

其他AdvancedFilter范围定义:

with DataSH
    .Range("A2").CurrentRegion.AdvancedFilter Action:=xlFilterCopy, _
            CriteriaRange:=.Range("L8:L9"), CopyToRange:=.Range("N8:T8"), _
            Unique:=False
end with