Excel Conditional Formatting Loop -



Excel Conditional Formatting Loop -

i have code:

selection.formatconditions.add type:=xlexpression, formula1:= _ "=if($b5=""arc"",1,0)" selection.formatconditions(selection.formatconditions.count).setfirstpriority selection.formatconditions(1).interior .patterncolorindex = xlautomatic .color = colorsheet.range("arc_color").interior.color .tintandshade = 0 end selection.formatconditions(1).stopiftrue = false selection.formatconditions.add type:=xlexpression, formula1:= _ "=if($b5=""all"",1,0)" selection.formatconditions(selection.formatconditions.count).setfirstpriority selection.formatconditions(1).interior .patterncolorindex = xlautomatic .color = colorsheet.range("all_color").interior.color .tintandshade = 0 end selection.formatconditions(1).stopiftrue = false

there more blocks , changes named ranges referenced within conditional formatting conditions.

=if($b5=""all"",1,0) colorsheet.range("all_color").interior.color

what best way loop this? don't have 10 blocks of code same?

can alter read named ranges off of list on separate sheet?

thanks

create procedure this, loops through range on separate worksheet, called "variables" in example. range contains 10 changing values:

edit: modfified per @killersnail's prepare in comments, , added parameter pass colorsheet.

sub loopcf() dim wsvariables excel.worksheet dim colorsheet excel.worksheet dim cell excel.range set wsvariables = thisworkbook.worksheets("variables") set colorsheet = activesheet ' alter suit needs 'this range contains "all", "arc", etc. each cell in wsvariables.range("a2:a11") applycf cell.value2 next cell end sub

then phone call existing code loop, passing "arc", "all" , other variables.

sub applycf(wscolorsheet excel.worksheet, conditiontocheck string) selection.formatconditions.add type:=xlexpression, formula1:= _ "=if($b5=" & chr(34) & conditiontocheck & chr(34) & ",1,0)" selection.formatconditions(selection.formatconditions.count).setfirstpriority selection.formatconditions(1).interior .patterncolorindex = xlautomatic .color = wscolorsheet.range(conditiontocheck & "_color").interior.color .tintandshade = 0 end selection.formatconditions(1).stopiftrue = false end sub

loops excel-vba conditional-formatting

Comments

Popular posts from this blog

web services - java.lang.NoClassDefFoundError: Could not initialize class net.sf.cglib.proxy.Enhancer -

Accessing MATLAB's unicode strings from C -

javascript - mongodb won't find my schema method in nested container -