vba - EXCEL 2010 find max value cell and change its color -
i working on vba excel 2010.
i need find max value in column , highlight cell color.
sub findmax_1() dim c range dim max double dim maxcell string max = 0 each c in selection if c.value > max max = c.value maxcell = c.address end if next c activesheet.range("a10") = max activesheet.range(maxcell).color = vbblue end sub
it not work. runtime error 438.
any appreciated.
as simco mentioned in comments need change following line of code:
activesheet.range(maxcell).color = vbblue
to
activesheet.range(maxcell).interior.color = vbblue
the problem current code if have nothing selected end 1004 error. 1 way of overcoming simco mentioned check if have cells selected. method below method preferr. lets have data in column a:
use code below:
sub findmax_1() dim c range dim flag boolean dim integer dim max double dim maxcell string flag = true = 1 max = 0 while flag = true if cells(i, 1) <> "" if cells(i, 1) > max max = cells(i, 1) maxcell = range(cells(i, 1), cells(i, 1)).address end if = + 1 else flag = false end if wend activesheet.range("a10") = max activesheet.range(maxcell).interior.color = vbblue end sub
result:
also @ article on blog more information excel vba formatting cells , range
also simco mentioned use conditional formatting, select column data:
from home ribbon select conditional formatting>>top/bottom rules >> top 10 items ...>>
select "1" left text box , choose color drop down list on right:
Comments
Post a Comment