【问题标题】:VBA not exiting function on errorVBA 出错时未退出函数
【发布时间】:2012-09-28 12:02:59
【问题描述】:

使用下面的代码,我试图推断存在已定义值的列右侧的列字母。例如,如果 D1 = 'Postcode',我希望 insert_col 等于 'E'。

如果title_range 中存在值“Postcode”,则此方法有效,但如果不存在,我会在最后一行出现错误(类型不匹配)。

根据 Match 的文档,如果查找值不存在于 renge 中,则返回 Excel 错误“N/A”,但检查 xlErrNA 以退出函数不起作用。

' Work out the column to insert
insert_col_pos = Application.Match("Postcode", title_range, 0)
If insert_col_pos = xlErrNA Then Exit Function ' Exit if the column to insert next to does not exist
insert_col = GetInsertCol(insert_col_pos + 1)

有没有人知道我做错了什么?谢谢。

【问题讨论】:

标签: excel error-handling excel-2007 vba


【解决方案1】:

你想要的是

If insert_col_pos = CVErr(xlErrNA) Then Exit Sub   

假设insert_col_posDim'ed 为Variant

【讨论】:

  • 好吧,如果产生了错误,它就可以工作,但如果没有产生错误(再次类型不匹配),现在就不行了。 insert_col_pos 确实是Variant,所以现在可能是这个错误,因为当我正在寻找的值被发现时,它实际上是String
  • 没关系,我已经添加了If IsError() Then 支票,而且我已经开始工作了。感谢您的帮助。
猜你喜欢
  • 2023-03-15
  • 2020-04-28
  • 1970-01-01
  • 1970-01-01
  • 2015-08-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-29
相关资源
最近更新 更多