VBA - 价值转换#REF!例外

时间:2017-09-14 06:55:27

标签: excel vba excel-vba

除了#REF之外,指定区域应转换为值是一个问题!是错误。

Sub keplet_helyett_ertek()
   Range("C3:J65").Select

   For Each akt_range In Selection.Areas
     If akt_range.Value <> CVErr(xlErrRef) Then
       akt_range.Formula = akt_range.Value
     End If
   Next
End Sub

然后,运行时间&#39; 13&#39;错误

enter image description here

4 个答案:

答案 0 :(得分:2)

另一个问题是如果一个单元格不包含错误值,你也会得到类型不匹配。要进行的测试是:

  For Each akt_range In Range("C3:J65")
     If Not IsError(akt_Range.Value) Then
       akt_range.Formula = akt_range.Value
     End If
   Next

答案 1 :(得分:0)

For Each akt_range In Selection.Areas

应该是

For Each akt_range In Selection

更有效率替换

Range("C3:J65").Select
For Each akt_range In Selection.Areas

Dim akt_range As Range
For Each akt_range In Range("C3:J65")

答案 2 :(得分:0)

Sub keplet_helyett_ertek()
Dim akt_range As Range, ok As Boolean
  Range("C3:J65").Select
  For Each akt_range In Selection
    ok = Not IsError(akt_range.Value)
    If Not ok Then ok = (akt_range.Value <> CVErr(xlErrRef))
    If ok Then akt_range.Formula = akt_range.Value
Next
End Sub

答案 3 :(得分:0)

如果像问题所述,您只想排除#REF!错误, 是正确的检查:

  For Each akt_range In Range("C3:J65")
    If CStr(akt_range.Value2) <> CStr(CVErr(xlErrRef)) Then
      akt_range.Value = akt_range.Value2
    End If
  Next

从技术上讲,上述情况将在公式的特殊情况下失败,该公式完全返回#REF!的错误代码字符串,例如="Error 2023" 绝对防弹 检查是这样的:

  For Each akt_range In Range("C3:J65")
    If Not IsError(akt_range.Value2) Or CStr(akt_range.Value2) <> CStr(CVErr(xlErrRef)) Then
      akt_range.Value = akt_range.Value2
    End If
  Next

如果您希望排除所有错误,更好的解决方案是在循环之前使用.SpecialCells()消除范围内的错误:

  For Each akt_range In Range("C3:J65").SpecialCells(xlCellTypeFormulas, xlLogical + xlNumbers + xlTextValues)
    akt_range.Value = akt_range.Value2
  Next