Excel 如果在列中找不到字符串,则VBA错误处理

Excel 如果在列中找不到字符串,则VBA错误处理,excel,vba,Excel,Vba,我有代码在excel中的一列中查找“模型”,但有时工作“模型”将不在那里。有没有办法处理这个错误?基本上我希望它说“如果模型不存在,那么做xxx” 我对什么都没有正确的用法 更新: 我最终使用了: lastPossibleRow = 1000000 lastRoww = ActiveSheet.Range("A" & lastPossibleRow).End(xlUp).Row j = 1 bNotModelType = True checking_type = "ModelType

我有代码在excel中的一列中查找“模型”,但有时工作“模型”将不在那里。有没有办法处理这个错误?基本上我希望它说“如果模型不存在,那么做xxx”

我对什么都没有正确的用法

更新:

我最终使用了:

lastPossibleRow = 1000000

lastRoww = ActiveSheet.Range("A" & lastPossibleRow).End(xlUp).Row
j = 1

bNotModelType = True
checking_type = "ModelType"
Do While bNotModelType And j < lastRoww

    If Cells(j, 1).Value = checking_type Then
        Model_Type_Row = j
        bNotModelType = False
    Else
    j = j + 1

    End If
Loop

    If Model_Type_Row = 0 Then
        'do something
    Else
        'code for if modeltype was found in the excel document
    End If
lastPossibleRow=1000000
lastRoww=ActiveSheet.Range(“A”&lastPossibleRow).End(xlUp).Row
j=1
bNotModelType=True
检查\u type=“ModelType”
当bNotModelType和j
我在查找最后一行时添加了,因为excel内存不足

您可以执行以下操作:

checking_type = 0
On Error Resume Next
checking_type = ThisWorkbook.Sheets("Sheet where you are looking").Cells.Find("ModelType").Column
On Error GoTo 0
If checking_type <> 0 Then
    'code if column is found
Else
    'code if column not found
End If
检查类型=0
出错时继续下一步
检查_type=ThisWorkbook.Sheets(“正在查找的工作表”).Cells.Find(“ModelType”).Column
错误转到0
如果检查\u类型0,则
'如果找到列,则编码
其他的
'如果找不到列,则编码
如果结束
您可以这样做:

checking_type = 0
On Error Resume Next
checking_type = ThisWorkbook.Sheets("Sheet where you are looking").Cells.Find("ModelType").Column
On Error GoTo 0
If checking_type <> 0 Then
    'code if column is found
Else
    'code if column not found
End If
检查类型=0
出错时继续下一步
检查_type=ThisWorkbook.Sheets(“正在查找的工作表”).Cells.Find(“ModelType”).Column
错误转到0
如果检查\u类型0,则
'如果找到列,则编码
其他的
'如果找不到列,则编码
如果结束

您可以使用布尔变量实现测试:

If bNotModelType = False Then
   Msgbox
End If

或者使用
Model\u Type\u行0
(然后在循环之前将其初始化为0)。

您可以使用布尔变量实现测试:

If bNotModelType = False Then
   Msgbox
End If

或者使用
Model\u Type\u行0
(然后在循环之前将其初始化为0)。

您的代码已经支持该功能。。。您只需要在if中添加else语句

bNotModelType = True
checking_type = "ModelType"
Do While bNotModelType
    'changing from value equals to instr so you can find partial matches
    If Instr(Cells(j, 1).Value,checking_type) Then 'InStr() this will do a boolean check if it's in the string
        Model_Type_Row = j
        bNotModelType = False
    Else 'added here
        'add your code when not met.
    End If
    j = j + 1
    'If Model_Type_Row = 0 Then MsgBox ("NADA") 'if you want within loop, do this
    'Model_Type_Row = 0
Loop
If Model_Type_Row = 0 Then MsgBox ("NADA") 'moved msgbox out of loop

Edit1:

问题。。。在列上使用
.Find()
有什么问题

if iserror(Columns(1).Find(What:=checking_type, LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:=False))
    'when there is an error, do this
    MsgBox ("NADA")
else
    'do something when the row is found
    Model_Type_Row = Columns(1).Find(What:=checking_type, LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:=False).Row
End if

Edit2:


根据lizard6的评论,显示了将msgbox移出循环。

您的代码已经支持这一点。。。您只需要在if中添加else语句

bNotModelType = True
checking_type = "ModelType"
Do While bNotModelType
    'changing from value equals to instr so you can find partial matches
    If Instr(Cells(j, 1).Value,checking_type) Then 'InStr() this will do a boolean check if it's in the string
        Model_Type_Row = j
        bNotModelType = False
    Else 'added here
        'add your code when not met.
    End If
    j = j + 1
    'If Model_Type_Row = 0 Then MsgBox ("NADA") 'if you want within loop, do this
    'Model_Type_Row = 0
Loop
If Model_Type_Row = 0 Then MsgBox ("NADA") 'moved msgbox out of loop

Edit1:

问题。。。在列上使用
.Find()
有什么问题

if iserror(Columns(1).Find(What:=checking_type, LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:=False))
    'when there is an error, do this
    MsgBox ("NADA")
else
    'do something when the row is found
    Model_Type_Row = Columns(1).Find(What:=checking_type, LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:=False).Row
End if

Edit2:


根据lizard6的评论,显示了将msgbox移出循环。

模型类型行是否为整数/长类型?我不认为你可以把它比作“无”,因为它不是一个物体。@RyanWildry是的;我看不到他的声明,但希望他能尽可能长的列出。。。当你发布时,我正在修正那一行(他在
之后有一个换行符,但如果“if Model_Type_Row=0 then…”需要在循环之外,则没有结束,否则消息框会在第一个循环之后弹出iteration@lizard6非常正确;根据OP希望如何使用它,这可能是有意的,类似于
FindNext
(反映了它在帖子中的显示方式)。就您的观点而言,
Model\u Type\u Row=j
需要在msgbox之后设置回
0
(编辑帖子以支持该设置)@Cyril非常正确,我没有想到。谢谢你。
Model\u Type\u Row
不是整型/长型吗?我不认为你可以将它与零进行比较,因为它不是一个对象。@RyanWildry是的;我没有看到他的声明,但希望他已经列出了那么长的内容……当你发布时,我只是在修改那行内容(他在
之后有一个换行符,然后
,但是如果“如果Model_Type_Row=0,那么…”需要在循环之外,则没有结束符,否则消息框会在第一个换行符之后弹出。)iteration@lizard6非常正确;根据OP希望如何使用它,这可能是有意的,类似于
FindNext
(反映了它在帖子中的出现方式)。就您的观点而言,
Model\u Type\u Row=j
需要在msgbox之后设置回
0
(编辑文章以支持该功能)@Cyril非常正确,我没有想到。谢谢!
Model\u-Type\u-Row
看起来它是一个整数类型。如果Model\u-Type\u-Row=0,你可以尝试
而不是尝试插入断点,检查
Model\u-Type\u-Row
的值,或者使用
Debug.Print
来显示值。我猜这是一个具有与
Nothing
不同的默认值(可能是
0
).
Model\u-Type\u Row
看起来像是一个整数类型。如果Model\u-Type\u Row=0,则可以尝试
而不是尝试插入断点,以检查
Model\u-Type\u Row
的值,或者使用
Debug.Print
来显示该值。我猜这是一种默认值与
Nothing
不同的数据类型>(可能是
0
)。