c# - Excel Interop Range.Replace method not working -


i trying write c# method remove instances of collection of characters excel spreadsheet. using range object's replace method seems efficient way this, rather iterating through each cell. here i'm working with:

range extension method:

 public static void replacechars(this range me, string toreplace, string replacement = "") {          //app static reference active excel instance.         //alerts must suppressed characters not present in data.         app.displayalerts = false;           (int32 = 0; < toreplace.length; i++) {             me.replace(                  //i'm using "substring(i,1)" rather "foreach (char c in toreplace)"                  //because excel maps char values differently .net.                 what: toreplace.substring(i, 1),                  replacement: replacement,                 lookat: xllookat.xlpart,                 searchorder: xlsearchorder.xlbyrows,                 matchcase: true,                 matchbyte: false, //i'm not 100% arg                 searchformat: false, //or 1                 replaceformat: false); //or 1         }          app.displayalerts = true;     } 

call main program this, example, leave punctuation:

  app.activesheet.cells.replacechars(         "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz1234567890"); 

sometimes 'u' not replaced, 't', digits. can't predict it. if comment out setting displayalerts , put breakpoint in loop, alerts on characters not replaced.

has had issues range.replace this? not assigning arguments correctly?

i think has numberformat property of range in question. added before replacements , worked:

ws.cells.numberformat = "@" //set format text foreach (range col in ws.cells){       //iterate through columns rather doing entire range reduce memory overhead      col.value = col.value //flatten lingering dates text values } 

Comments