Excel/VBA:跳过错误并继续执行代码
问题:我的宏不符合我的要求。我有一个包含多列的excel文件。我想要的是宏Excel/VBA:跳过错误并继续执行代码,vba,excel,Vba,Excel,问题:我的宏不符合我的要求。我有一个包含多列的excel文件。我想要的是宏 要查找特定的头文件(如果文件中存在),请 选择整个列,然后单击“确定” 按照脚本中的指定调整其大小。如果指定的头文件在文件中不存在,则代码应在不给出任何错误的情况下移动到下一个头文件 下面的代码将“问题描述”的大小从50更改为6,尽管6是“需要纠正措施?”标题的大小(在这种情况下不适用,因为该标题不存在,因此忽略了6 s/b的大小调整要求) 但那没有发生。相反,先前条件的大小(将“问题描述”的列大小更改为50)确实更改为
Sub Resize_specific_columns_OnErrResNxt()
'
' finds specific columns based on changed header names and resize them
On Error Resume Next
Cells.Find(what:="data domain", After:=ActiveCell, LookIn:= _
xlValues, lookat:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext _
, MatchCase:=False, SearchFormat:=False).Activate
ActiveCell.EntireColumn.Select
Selection.ColumnWidth = 8
On Error Resume Next
Cells.Find(what:="eDIM#", After:=ActiveCell, LookIn:= _
xlValues, lookat:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext _
, MatchCase:=False, SearchFormat:=False).Activate
ActiveCell.EntireColumn.Select
Selection.ColumnWidth = 6
On Error Resume Next
Cells.Find(what:="Problem Description", After:=ActiveCell, LookIn:= _
xlValues, lookat:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext _
, MatchCase:=False, SearchFormat:=False).Activate
ActiveCell.EntireColumn.Select
Selection.ColumnWidth = 50
On Error Resume Next
Cells.Find(what:="Corrective Action Required?", After:=ActiveCell, LookIn:= _
xlValues, lookat:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext _
, MatchCase:=False, SearchFormat:=False).Activate
ActiveCell.EntireColumn.Select
Selection.ColumnWidth = 6
下面是一个
查找
的示例,您可以复制>>将此方法粘贴到其他方法
使用Find
的推荐方法是为Find
结果设置一个范围,然后您可以检查Range=是否为Nothing
,这意味着Find
无法找到您要查找的文本/号码
代码
' finds specific columns based on changed header names and resize them
Dim FndRng As Range
Set FndRng = Cells.Find(what:="data domain", After:=ActiveCell, LookIn:= _
xlValues, lookat:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext _
, MatchCase:=False, SearchFormat:=False)
If Not FndRng Is Nothing Then '<-- find was successful
FndRng.EntireColumn.ColumnWidth = 8
End If
Set FndRng = Nothing '<-- clear Range before next Find
”根据更改的标题名称查找特定列并调整其大小
变暗FndRng As范围
设置FndRng=Cells.Find(what:=“data domain”,After:=ActiveCell,LookIn:=_
xlValues,lookat:=xlother,SearchOrder:=xlByRows,SearchDirection:=xlNext_
,MatchCase:=False,SearchFormat:=False)
如果不是FndRng什么都不是,那么“这里是一个查找
的示例,您可以复制>>粘贴此方法到其他方法
使用Find
的推荐方法是为Find
结果设置一个范围,然后您可以检查Range=是否为Nothing
,这意味着Find
无法找到您要查找的文本/号码
代码
' finds specific columns based on changed header names and resize them
Dim FndRng As Range
Set FndRng = Cells.Find(what:="data domain", After:=ActiveCell, LookIn:= _
xlValues, lookat:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext _
, MatchCase:=False, SearchFormat:=False)
If Not FndRng Is Nothing Then '<-- find was successful
FndRng.EntireColumn.ColumnWidth = 8
End If
Set FndRng = Nothing '<-- clear Range before next Find
”根据更改的标题名称查找特定列并调整其大小
变暗FndRng As范围
设置FndRng=Cells.Find(what:=“data domain”,After:=ActiveCell,LookIn:=_
xlValues,lookat:=xlother,SearchOrder:=xlByRows,SearchDirection:=xlNext_
,MatchCase:=False,SearchFormat:=False)
如果Not FndRng为Nothing,则“在出错时继续下一行”
继续下一行,但这3行可以合并为1行:
On Error Resume Next
Cells.Find("data domain").EntireColumn.ColumnWidth = 8
Cells.Find("eDIM#").EntireColumn.ColumnWidth = 6
Cells.Find("Problem Description").EntireColumn.ColumnWidth = 50
Cells.Find("Corrective Action Required?").EntireColumn.ColumnWidth = 6
On Error Goto 0 ' optional if there is more code after that should not ignore errors
出错时继续下一行
继续下一行,但这3行可以合并为1行:
On Error Resume Next
Cells.Find("data domain").EntireColumn.ColumnWidth = 8
Cells.Find("eDIM#").EntireColumn.ColumnWidth = 6
Cells.Find("Problem Description").EntireColumn.ColumnWidth = 50
Cells.Find("Corrective Action Required?").EntireColumn.ColumnWidth = 6
On Error Goto 0 ' optional if there is more code after that should not ignore errors
这太酷了!工作得很有魅力!非常感谢您的及时回复!这太酷了!工作得很有魅力!非常感谢您的及时回复!