【问题标题】:how to find cells with "#VALUE!" in EXCEL 2010 VBA如何找到带有“#VALUE!”的单元格在 EXCEL 2010 VBA 中
【发布时间】:2014-03-26 21:24:03
【问题描述】:

我需要在 EXCEL 2012 VBA 的列中找到最大值。 有些单元格有“#VALUE!”。我的代码不起作用。

Sub find_max()
   Dim rng As Range
   Dim dblMax As Double
  dblMax = 0
  Set rng = Range("A2:A11")
  For Each cell In rng
     If (cell.Value <> "#VALUE!") Then    // error ! type mismatch
         If dblMax < CDbl(cell.Value) Then
             dblMax = CDbl(cell.Value)
         End If
    End If
 Next cell
 MsgBox dblMax
End Sub

我还需要用最大值打印单元格位置(用颜色标记)。

任何帮助将不胜感激。

【问题讨论】:

  • If (cell.Value &lt;&gt; "#VALUE!") Then更改为If NOT ISERROR(cell) Then
  • 我还需要打印具有最大值的单元格位置。谢谢!

标签: excel windows-7 excel-2010 vba


【解决方案1】:

如果您的范围有常量,请使用

application.WorksheetFunction.Max(range("B6:C13").SpecialCells(xlcelltypeconstants,xlNumbers ))

如果有公式,请使用

application.WorksheetFunction.Max(range("B6:C13").SpecialCells(xlcelltypeformulas,xlNumbers ))

对于找到它的范围,.Find 应该可以正常工作:

Sub find_max()


 Dim rng As Range
   Dim dblMax As Double, rgMax As Range

    Set rng = Range("A2:A11")

    dblMax = Application.WorksheetFunction.Max(rng.SpecialCells(xlCellTypeFormulas, xlNumbers))

    Set rgMax = rng.Find(dblMax, , xlValues, xlWhole)

 MsgBox rgMax.Address & ": " & dblMax

End Sub

【讨论】:

  • dblMax = Application.WorksheetFunction.Max(rng.SpecialCells(xlCellTypeFormulas, xlNumbers) 出现错误。谢谢
  • 如果你所有的数字都是常数,把那行换成application.WorksheetFunction.Max(range("B6:C13").SpecialCells(xlcelltypeconstants,xlNumbers ))
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-12-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多