excel - Trouble pulling out any number from string -
i having trouble stripping numbers string. in excel, have many string fields may contain numbers. care number(s), rest of characters unwanted , discarded. number may in position, not set location.
for example: '2 catch basins' or 'catch basins x2'
i based code on so answer, can't seem work. error message 'application-defined or object-defined error'.
option explicit function onlydigits(s string) string ' variables needed (remember use "option explicit"). ' dim retval string ' return string. ' dim integer ' counter character position. ' ' initialise return string empty ' retval = "" ' every character in input string, copy digits ' ' return string. ' = 1 len(s) if mid(s, i, 1) >= "0" , mid(s, i, 1) <= "9" retval = retval + mid(s, i, 1) end if next ' return return string. ' onlydigits = retval end function public sub commandbutton1_click() application.screenupdating = false ' ----------------------------------------------------------------------------------------- ' strip numbers descriptions basins, guy wires, water meters/valves & pull box ' ----------------------------------------------------------------------------------------- dim counter integer 'index while loop dim fcode string 'variable column e, feature code dim fdesc string 'variable column f, descriptor while cells(counter, 1).value <> "" 'while first column has data, keep looping fcode = cells(counter, 5).value 'populate feature code variable column e if (fcode = "xcb") or (fcode = "xgw") or (fcode = "xwv") or (fcode = "xwm") fdesc = cells(counter, 6).value cells(counter, 6).value = onlydigits(fdesc) else 'do nothing end if counter = counter + 1 loop 'finishes checking numbers within specific descriptors
can point me in right direction? appreciated!!
do while cells(counter, 1).value
here counter
0 range indexes start @ 1 hence error.
Comments
Post a Comment