Excel “正在解决运行时错误”;91“;在VBA崩溃后,在崩溃前找到工作
我希望有人能帮助我。几乎什么都做完了,我的excel崩溃了。坠机前一切都很顺利。现在我在这行“ActiveCell.Find(I).Select”上得到了一个运行时错误。下面是我的过程,它搜索一个文件中的特定值,找到它,然后转到包含该值的最后一行Excel “正在解决运行时错误”;91“;在VBA崩溃后,在崩溃前找到工作,excel,vba,Excel,Vba,我希望有人能帮助我。几乎什么都做完了,我的excel崩溃了。坠机前一切都很顺利。现在我在这行“ActiveCell.Find(I).Select”上得到了一个运行时错误。下面是我的过程,它搜索一个文件中的特定值,找到它,然后转到包含该值的最后一行 Sub FindValue_Raw() Dim Value As Variant Dim Sheetname As String Dim lastCell As String Dim x As Integer Dim iVal As Integer
Sub FindValue_Raw()
Dim Value As Variant
Dim Sheetname As String
Dim lastCell As String
Dim x As Integer
Dim iVal As Integer
Dim i As Variant
Value = InputBox("Enter Value:", "Input")
Sheetname = "Sheet2"
Dim Cell As Variant
'Search in Column A | MatchCase True = case sensitive of raw file
Set Cell = Sheets(Sheetname).Columns("C:C").Find(What:=Value, After:=Sheets(Sheetname).Range("C1"), LookIn:=xlFormulas, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=True, SearchFormat:=False)
If Not Cell Is Nothing Then
'Find how many rows have this value
iVal = Application.WorksheetFunction.CountIf(Range("C1:C480000"), Cell)
'find the last line with this value
Do While x < iVal
'Set where the curse is after it finds value
i = Cell
**'Select first instance of that value -THIS IS WHERE THE ERROR IS
**ActiveCell.Find(i).Select****
'Finds the last Reference address
lastCell = ActiveCell.Address(False, False)
'Highlights cell
' ActiveCell.Interior.ColorIndex = 4
x = x + 1
Loop
' Next
ActiveCell.Offset(1, 0).Select
'Value is found, Highlight Cell
'Sheets(Sheetname).Range(Cell.Address).Interior.ColorIndex = 4
Else
'Value Not found
MsgBox "Not Found"
End If
End Sub
Sub FindValue_Raw()
变暗值
将Sheetname设置为字符串
将最后一个单元格设置为字符串
作为整数的Dim x
作为整数的Dim-iVal
Dim i作为变体
值=输入框(“输入值:”,“输入”)
Sheetname=“Sheet2”
变暗细胞
'在A列中搜索| MatchCase True=原始文件区分大小写
设置单元格=工作表(Sheetname)。列(“C:C”)。查找(What:=值,After:=工作表(Sheetname)。范围(“C1”),查找:=xlFormulas_
查看:=xlother,搜索顺序:=xlByRows,搜索方向:=xlNext_
MatchCase:=True,SearchFormat:=False)
如果不是的话,那细胞就什么都不是了
'查找有多少行具有此值
iVal=Application.WorksheetFunction.CountIf(范围(“C1:C480000”),单元格)
'查找具有此值的最后一行
当x
您正在对范围的结果进行会员呼叫。查找。当Range.Find
没有找到它要查找的内容时,它会返回Nothing
-并且您不能调用。在Nothing
上选择
这正是你在这里要防范的:
Set Cell = Sheets(Sheetname).Columns("C:C").Find(...)
If Not Cell Is Nothing Then
'...
End If
您需要在此处执行相同的操作:
也就是说,声明一个局部变量并验证Range.Find
成功:
Dim一些有意义的东西,如范围
Set-SomethingMentaging=ActiveCell.Find(i)“ActiveCell
是运行时的正确单元格吗?还提供有关错误91的信息,包括原因。Yes-ActiveCell是出错的位置发生错误时的ActiveCell
是什么?ActiveCell.Find(i)。选择-cell-是“1703”,这是我在输入框中输入的。那很好。我也通过了1703年的考试。然后就完了。
ActiveCell.Find(i).Select
Dim somethingMeaningful As Range
Set somethingMeaningful = ActiveCell.Find(i) '<~ TODO recondiser whether ActiveCell is really needed
If Not somethingMeaningful Is Nothing Then
somethingMeaningful.Select '<~ TODO reconsider whether this is really needed
End If