how to find cells with “#VALUE!” in EXCEL 2010 VBA

ε祈祈猫儿з 提交于 2020-02-02 16:27:06

问题


I need to find the max value in a column by EXCEL 2012 VBA. Some cells have "#VALUE!". My code does not work.

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

I also need to print the cell location (mark it with a color) with the max value.

Any help would be appreciated.


回答1:


If your range has constants, use

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

if it has formulas, use

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

For the range where it's found, .Find should work fine:

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


来源:https://stackoverflow.com/questions/22673373/how-to-find-cells-with-value-in-excel-2010-vba

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!