从VBA代码检查Excel单元格中的#N / A [英] Checking for #N/A in Excel cell from VBA code

查看:780
本文介绍了从VBA代码检查Excel单元格中的#N / A的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在迭代一个包含2位小数的数字的单元格范围。我需要检查这个单元格是否保持'#N / A',如果是,我需要跳过它。问题是,当一个单元格保存一个有效的数字时,如果下面的条件抛出类型不匹配错误。如何避免这种情况?

  If(ActiveWorkbook.Sheets(Publish)。Range(G4)。offset offsetCount,0).Value CVErr(xlErrNA))然后
'做某事
如果


解决方案

首先检查错误(N / A值),然后尝试与cvErr()进行比较。你正在比较两个不同的东西,一个价值和一个错误。这可能工作,但不总是。简单地将表达式转换为错误可能会导致类似的问题,因为它不是真正的错误,只是取决于表达式的错误的值。

 code>如果IsError(ActiveWorkbook.Sheets(Publish)。Range(G4)。offset(offsetCount,0).Value)Then 
If(ActiveWorkbook.Sheets(Publish)。Range (G4)。offset(offsetCount,0).Value CVErr(xlErrNA))Then
'do something
End If
End If


I'm iterating through a range of cells which hold numbers with 2 decimal places. I need to check if the cell holds '#N/A', and if it does, I need to skip it. The problem is, when a cell holds a valid number, my if condition below throws a 'Type mismatch error'. How can I avoid this?

If (ActiveWorkbook.Sheets("Publish").Range("G4").offset(offsetCount, 0).Value <> CVErr(xlErrNA)) Then
'do something
End If

解决方案

First check for an error (N/A value) and then try the comparisation against cvErr(). You are comparing two different things, a value and an error. This may work, but not always. Simply casting the expression to an error may result in similar problems because it is not a real error only the value of an error which depends on the expression.

If IsError(ActiveWorkbook.Sheets("Publish").Range("G4").offset(offsetCount, 0).Value) Then
  If (ActiveWorkbook.Sheets("Publish").Range("G4").offset(offsetCount, 0).Value <> CVErr(xlErrNA)) Then
    'do something
  End If
End If

这篇关于从VBA代码检查Excel单元格中的#N / A的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆