Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 找不到VBA查找值_Excel_Vba_Find - Fatal编程技术网

Excel 找不到VBA查找值

Excel 找不到VBA查找值,excel,vba,find,Excel,Vba,Find,我需要一些帮助,因为我是VBA的初学者。我的代码在多个工作表中查找值“xstart”,以定义需要删除的单元格范围。当工作表中不存在值“xstart”时,我遇到了问题。我怎么办 Sub TestReset() YesNo = MsgBox("Are you sure you want to clear the data?", vbYesNo) Select Case YesNo Case vbYes Dim sht As Worksheet

我需要一些帮助,因为我是VBA的初学者。我的代码在多个工作表中查找值“xstart”,以定义需要删除的单元格范围。当工作表中不存在值“xstart”时,我遇到了问题。我怎么办

Sub TestReset()

YesNo = MsgBox("Are you sure you want to clear the data?", vbYesNo)

Select Case YesNo
    Case vbYes
        Dim sht As Worksheet
        For Each sht In ActiveWorkbook.Sheets
            If sht.Name <> "Sheet1" And sht.Name <> "Sheet2" Then
                Dim iRow As Long, iMax As Long
                iRow = sht.Cells.Find(What:="xstart", LookIn:=xlFormulas, _
                       LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
                       MatchCase:=False, SearchFormat:=False).Row

                iMax = sht.Cells(iRow, "A").End(xlDown).Row
                sht.Range("A" & iRow & ":AY" & iMax).ClearContents
            End If
        Next sht

    MsgBox ("Data has been cleared.")

Case vbNo

End Select

End Sub
子测试重置()
YesNo=MsgBox(“您确定要清除数据吗?”,vbYesNo)
选择“是”否
案例vbYes
将sht变暗为工作表
对于ActiveWorkbook.Sheets中的每个sht
如果sht.Name“Sheet1”和sht.Name“Sheet2”,则
暗淡的iRow和长的一样,iMax和长的一样
iRow=sht.Cells.Find(What:=“xstart”,LookIn:=xlFormulas_
查看:=xlPart,搜索顺序:=xlByRows,搜索方向:=xlNext_
MatchCase:=False,SearchFormat:=False)。行
iMax=短单元格(iRow,“A”)。结束(xlDown)。行
sht.Range(“A”&iRow&“:AY”&iMax).ClearContents
如果结束
下一步
MsgBox(“数据已被清除”)
案例vbNo
结束选择
端接头

只需检查存储
Find
对象的变量是否为空:

Sub TestReset()
Dim iRow As Range
Dim iMax As Long
Dim sht As Worksheet

YesNo = MsgBox("Are you sure you want to clear the data?", vbYesNo)

If YesNo = vbYes Then
    For Each sht In ActiveWorkbook.Sheets
        If sht.Name <> "Sheet1" And sht.Name <> "Sheet2" Then
            Set iRow = sht.Cells.Find(What:="xstart", LookIn:=xlFormulas, _
                   LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
                   MatchCase:=False, SearchFormat:=False)
            If Not iRow Is Nothing Then
                iMax = sht.Cells(iRow.Row, "A").End(xlDown).Row
                sht.Range("A" & iRow.Row & ":AY" & iMax).ClearContents
            End If
        End If
    Next sht

    MsgBox ("Data has been cleared.")

End If

End Sub
子测试重置()
暗暗暗暗暗暗暗暗暗暗暗暗暗暗暗暗暗暗暗暗暗暗暗暗暗暗暗暗暗暗暗暗暗暗暗暗暗暗暗暗暗暗暗暗暗暗暗暗暗暗暗暗暗暗暗暗
暗iMax与长iMax相同
将sht变暗为工作表
YesNo=MsgBox(“您确定要清除数据吗?”,vbYesNo)
如果是no=vbYes,则
对于ActiveWorkbook.Sheets中的每个sht
如果sht.Name“Sheet1”和sht.Name“Sheet2”,则
设置iRow=sht.Cells.Find(What:=“xstart”,LookIn:=xlFormulas_
查看:=xlPart,搜索顺序:=xlByRows,搜索方向:=xlNext_
MatchCase:=False,SearchFormat:=False)
如果不是,iRow什么都不是
iMax=短单元格(iRow.Row,“A”).End(xlDown).Row
sht.Range(“A”和iRow.Row&“:AY”和iMax).ClearContents
如果结束
如果结束
下一步
MsgBox(“数据已被清除”)
如果结束
端接头

如果找到了要搜索的短语,则
Range.Find
方法将返回一个范围对象,即找到短语的单元格。如果找不到短语,该方法将返回
Nothing

您可以在此处阅读更多内容:

这就是你遇到的问题。当找到“xstart”时,
sht.Cells.Find(…)
返回一个单元格,您可以引用该单元格的Row属性。未找到“xstart”时,Find方法返回
Nothing
Nothing
没有行属性,因此
sht.Cells.Find(…).Row
失败

为了防止程序错误,您需要添加一个检查
Find
方法是否返回
Nothing
,如下所示:

Set MyRange = sht.Cells.Find(What:="xstart", LookIn:=xlFormulas, _
    LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
    MatchCase:=False, SearchFormat:=False)

If Not MyRange Is Nothing Then
    iRow = MyRange.Row
    'etc.

请注意,您没有检查iRow(所需的行号)是否为
Nothing
。您可以检查Find是否返回范围或
不返回任何内容
。如果它返回一个范围,则可以读取该范围的Row属性。

Hi Tim,感谢您的贡献!然而,当我试图执行这段代码时,我收到了以下错误:与“iRow”编译错误相关,类型不匹配。知道怎么解决吗?@jan.f my bad,edited。
查找
对象是
范围
对象。您可以从中提取行,就像在单行程序中那样,但这只有在实际找到搜索查询时才可能。因此:首先搜索范围,如果找到,提取您需要的属性。很高兴听到它这样做!如果您找到的答案完全解决了您的问题,请不要忘记单击复选标记接受答案