Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/25.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运行时错误9_Excel_Vba_Error Handling_Runtime Error - Fatal编程技术网

Excel 工作表名称与目标工作表名称不匹配时出现VBA运行时错误9

Excel 工作表名称与目标工作表名称不匹配时出现VBA运行时错误9,excel,vba,error-handling,runtime-error,Excel,Vba,Error Handling,Runtime Error,我有以下代码,它将具有特定工作表名称的Excel工作表转换为PDF,然后在文件夹中循环。用户可以在工具中输入图纸名称,以便代码循环。但是,如果图纸名称不正确或不存在,则会显示运行时错误9:下标超出范围。与此错误不同的是,我希望获得一个MsgBox,然后退出Sub。我尝试使用On error GoTo方法,当代码与工作表名称不匹配并显示相应的消息时,该方法会起作用。但是,当插入正确的图纸名称时,它会显示该消息,并且不会继续执行代码 我如何解决这个问题,使我只在代码找不到工作表名称时收到消息,如果找

我有以下代码,它将具有特定工作表名称的Excel工作表转换为PDF,然后在文件夹中循环。用户可以在工具中输入图纸名称,以便代码循环。但是,如果图纸名称不正确或不存在,则会显示运行时错误9:下标超出范围。与此错误不同的是,我希望获得一个MsgBox,然后退出Sub。我尝试使用On error GoTo方法,当代码与工作表名称不匹配并显示相应的消息时,该方法会起作用。但是,当插入正确的图纸名称时,它会显示该消息,并且不会继续执行代码

我如何解决这个问题,使我只在代码找不到工作表名称时收到消息,如果找到了,则完成代码

这就是我面临的问题

On Error GoTo ErrorExit

'Even when the cell value matches the sheet's name, I still get an error and it exist the sub
Set reportSheet = Sheets(reportSheetName)   

ErrorExit:
MsgBox "Incorrect Sheet Name or It Does Not Exist"
Exit Sub

您可以这样做:

On Error Resume Next  'ignore errors
Set reportSheet = Sheets(reportSheetName) 
On Error Goto 0       'stop ignoring errors 

If reportSheet is nothing then
     Msgbox "no sheet named '" & reportSheetName & "' in this workbook!"
Else
     'all good
End If

您可以这样做:

On Error Resume Next  'ignore errors
Set reportSheet = Sheets(reportSheetName) 
On Error Goto 0       'stop ignoring errors 

If reportSheet is nothing then
     Msgbox "no sheet named '" & reportSheetName & "' in this workbook!"
Else
     'all good
End If

设置报告表…
之后,在错误转到0时添加一行
。另外,在
错误退出之前:
添加
退出子项
。否则它将始终显示消息框。嗯,它不起作用!我正试图根据下面的答案想出一些办法。谢谢你,蝙蝠侠。在
设置报告表…
之后,在错误转到0时添加一行
。另外,在
错误退出之前:
添加
退出子项
。否则它将始终显示消息框。嗯,它不起作用!我正试图根据下面的答案想出一些办法。谢谢你,蝙蝠侠。谢谢你,蒂姆,我最终使用了这段代码来满足我的需要。错误时继续下一集reportSheet=Sheets(reportSheetName)错误时转到0如果reportSheet为空,则MsgBox“此工作簿中没有名为的工作表”&“reportSheetName&”!如果退出子结尾,谢谢蒂姆,我最终使用此代码来满足我的需要。
On Error Resume Next Set reportSheet=Sheets(reportSheetName)On Error GoTo 0如果reportSheet为空,那么MsgBox“此工作簿中没有命名的工作表”&“reportSheetName&”!如果