vba - Refresh user-defined function when input values change -
vba - Refresh user-defined function when input values change -
hi have problem getting user-defined function created refresh when input values change. in fact, input values "governed" macro , why think function not triggered (in fact, getting refreshed delayed "1 step". , using application.volatile :) can please help me? :)
here function:
public function avcolor(byref myrange range) double activesheet.unprotect application.volatile dim sum integer sum = 0 dim count integer count = 0 each cell in myrange if cell.interior.colorindex = 3 sum = sum + 1 elseif cell.interior.colorindex = 44 sum = sum + 2 elseif cell.interior.colorindex = 6 sum = sum + 3 elseif cell.interior.colorindex = 43 sum = sum + 4 elseif cell.interior.colorindex = 33 sum = sum + 5 elseif cell.interior.colorindex = blank sum = sum end if count = count + 1 next cell avcolor = round(sum / count) activesheet.protect end function
and here macro used whole sheet:
private sub worksheet_change(byval target range) dim icolor integer activesheet.unprotect application.volatile true = 4 35 if not intersect(target, range(cells(i, 15), cells(i, 18))) nil each cell in range(cells(i, 15), cells(i, 18)) 'increase 5 if cells(i, 4).value = 1 if cell = blank icolor = blank elseif cell < cells(i, 5) icolor = 3 elseif cell < cells(i, 7) icolor = 44 elseif cell < cells(i, 9) icolor = 6 elseif cell <= cells(i, 11) icolor = 43 elseif cell > cells(i, 11) icolor = 33 else 'whatever end if ''''''''''''''''''''''''''''''''''' 'increase 3 elseif cells(i, 4).value = 2 if cell = blank icolor = blank elseif cell < cells(i, 5) icolor = 3 elseif cell <= cells(i, 7) icolor = 44 elseif cell > cells(i, 7) icolor = 6 'elseif cell < cells(i, 11) 'icolor = 43 'elseif cell >= cells(i, 13) 'icolor = 33 else 'whatever end if ''''''''''''''''''''''''''''''''''' 'decrease 5 elseif cells(i, 4).value = 3 if cell = blank icolor = blank elseif cell > cells(i, 5) icolor = 3 elseif cell > cells(i, 7) icolor = 44 elseif cell > cells(i, 9) icolor = 6 elseif cell > cells(i, 11) icolor = 43 elseif cell <= cells(i, 11) icolor = 33 else 'whatever end if ''''''''''''''''''''''''''''''''''' 'decrease 3 elseif cells(i, 4).value = 4 if cell = blank icolor = blank elseif cell > cells(i, 5) icolor = 3 elseif cell >= cells(i, 7) icolor = 44 elseif cell < cells(i, 7) icolor = 6 'elseif cell > cells(i, 11) 'icolor = 43 'elseif cell <= cells(i, 13) 'icolor = 33 else 'whatever end if ''''''''''''''''''''''''''''''''''' 'non-linear 5 elseif cells(i, 4).value = 5 if cell = blank icolor = blank elseif cell < cells(i, 5) icolor = 3 elseif cell > cells(i + 1, 5) icolor = 3 elseif cell < cells(i, 7) icolor = 44 elseif cell > cells(i + 1, 7) icolor = 44 elseif cell < cells(i, 9) icolor = 6 elseif cell > cells(i + 1, 9) icolor = 6 elseif cell < cells(i, 11) icolor = 43 elseif cell > cells(i + 1, 11) icolor = 43 elseif cell >= cells(i, 11) icolor = 33 elseif cell <= cells(i + 1, 11) icolor = 33 else 'whatever end if ''''''''''''''''''''''''''''''''''' 'non-linear 3 elseif cells(i, 4).value = 6 if cell = blank icolor = blank elseif cell < cells(i, 5) icolor = 3 elseif cell < cells(i, 7) icolor = 44 elseif cell <= cells(i, 9) icolor = 6 elseif cell <= cells(i, 11) icolor = 44 elseif cell > cells(i, 11) icolor = 3 else 'whatever end if else msg = "error" end if cell.interior.colorindex = icolor next cell end if next activesheet.protect end sub**
first remove unprotect , protect statements function. alter alter code this:
private sub worksheet_change(byval target range) dim icolor long dim long dim rcell excel.range activesheet.unprotect if not intersect(target, range(cells(4, 15), cells(35, 18))) nil = 4 35 each cell in range(cells(i, 15), cells(i, 18)) 'increase 5 if cells(i, 4).value = 1 if cell.value2 = vbnullstring icolor = xlcolorindexnone elseif cell.value2 < cells(i, 5).value2 icolor = 3 elseif cell.value2 < cells(i, 7).value2 icolor = 44 elseif cell.value2 < cells(i, 9).value2 icolor = 6 elseif cell.value2 <= cells(i, 11).value2 icolor = 43 elseif cell.value2 > cells(i, 11).value2 icolor = 33 end if ''''''''''''''''''''''''''''''''''' 'increase 3 elseif cells(i, 4).value = 2 if cell.value2 = vbnullstring icolor = xlcolorindexnone elseif cell < cells(i, 5) icolor = 3 elseif cell <= cells(i, 7) icolor = 44 elseif cell > cells(i, 7) icolor = 6 end if ''''''''''''''''''''''''''''''''''' 'decrease 5 elseif cells(i, 4).value = 3 if cell.value2 = vbnullstring icolor = xlcolorindexnone elseif cell > cells(i, 5) icolor = 3 elseif cell > cells(i, 7) icolor = 44 elseif cell > cells(i, 9) icolor = 6 elseif cell > cells(i, 11) icolor = 43 elseif cell <= cells(i, 11) icolor = 33 end if ''''''''''''''''''''''''''''''''''' 'decrease 3 elseif cells(i, 4).value = 4 if cell.value2 = vbnullstring icolor = xlcolorindexnone elseif cell > cells(i, 5) icolor = 3 elseif cell >= cells(i, 7) icolor = 44 elseif cell < cells(i, 7) icolor = 6 end if ''''''''''''''''''''''''''''''''''' 'non-linear 5 elseif cells(i, 4).value = 5 if cell.value2 = vbnullstring icolor = xlcolorindexnone elseif cell < cells(i, 5) icolor = 3 elseif cell > cells(i + 1, 5) icolor = 3 elseif cell < cells(i, 7) icolor = 44 elseif cell > cells(i + 1, 7) icolor = 44 elseif cell < cells(i, 9) icolor = 6 elseif cell > cells(i + 1, 9) icolor = 6 elseif cell < cells(i, 11) icolor = 43 elseif cell > cells(i + 1, 11) icolor = 43 elseif cell >= cells(i, 11) icolor = 33 elseif cell <= cells(i + 1, 11) icolor = 33 end if ''''''''''''''''''''''''''''''''''' 'non-linear 3 elseif cells(i, 4).value = 6 if cell.value2 = vbnullstring icolor = xlcolorindexnone elseif cell < cells(i, 5) icolor = 3 elseif cell < cells(i, 7) icolor = 44 elseif cell <= cells(i, 9) icolor = 6 elseif cell <= cells(i, 11) icolor = 44 elseif cell > cells(i, 11) icolor = 3 end if else msg = "error" end if cell.interior.colorindex = icolor next cell next end if application.calculate activesheet.protect end sub
vba excel-vba
Comments
Post a Comment