vba - Excel macro select cell that contains a string and replace a character -


i have worksheet more 200 cells. each cell contains formula below:

=average('worksheetname'!range) 

i want run macro changes formula following formula:

=iferror(average('worksheetname'!range),100%) 

i have worked out can change =average example &average , search & replace &average &iferror. allow me search cells contains &iferror , add missing parenthesis @ end of formula )

i want build macro have few problems:

  • how search & replace once each cell
  • macro gives me mismatch error

below code macro:

    sub aaaa()  ' ' iferror macro '  '     dim myrange range     dim mycell range     dim integer     set myrange = range("e4:bb120")       sheets("zones").select      cells.replace what:="=average(", replacement:="&iferror(average(", lookat _         :=xlpart, searchorder:=xlbyrows, matchcase:=false, searchformat:=false, _         replaceformat:=false      each mycell in myrange     if mycell "*&iferror*"     mycell.select      = 1     while < 2     selection.replace what:=")", replacement:="),100%)", lookat _         :=xlpart, searchorder:=xlbyrows, matchcase:=false, searchformat:=false, _         replaceformat:=false     = + 1     loop     end if     next mycell      cells.replace what:="&iferror(average(", replacement:="=iferror(average(", lookat _         :=xlpart, searchorder:=xlbyrows, matchcase:=false, searchformat:=false, _         replaceformat:=false      end sub 

you might find easier replacement "manually" in code instead of using replace :

sub aaaa()       dim myrange range     dim c range     dim f string, long       on error resume next     set myrange = sheets("zones").range("e4:bb120").specialcells( _                                                 xlcelltypeformulas)     on error goto haveerror      if myrange nothing exit sub      application.calculation = xlcalculationmanual      each c in myrange.cells         f = c.formula         if f "=average(*)"             c.formula = "=iferror(" & right(f, len(f) - 1) & ",100%)"             = + 1         end if     next c      msgbox "replaced " & & " formulas"  haveerror:      if err.number <> 0 msgbox err.description     application.calculation = xlcalculationmanual end sub 

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -