search - Excel VBA - Find a set of characters and set as string -
i have set of descriptions contain id numbers arranged column. example:
column a
this description id number in id12345.
this description id66666 id number in it.
this id99999 description id number in it.
the id numbers in format "idxxxxx" i'd somehow trim away text in each of these cells , leave id number.
my thoughts: can somehow done finding string "id?????" , setting variable, replacing contents of cell variable? or erasing characters in cell -except- "id?????"
any appreciated, thanks.
this code wrote iterate through items in column a. split words in each cell array. if word 7 or 8 characters long potentially idxxxxx. perform few checks see if matches idxxxxx syntax. in case replace contents of cell id dropping remaining text.
sub replacecontentwithids() dim ws worksheet set ws = sheets("sheet1") ' or sheet name dim rng range dim i&, lr&, j& dim arr dim str$ lr = ws.range("a" & rows.count).end(xlup).row ' starting 1 - if have headers change 2 = 1 lr set rng = ws.range("a" & i) arr = split(cstr(rng.value), " ") j = lbound(arr) ubound(arr) str = arr(j) if (len(str) = 7) or (len(str) = 8) if (strcomp(left(str, 2), "id", vbtextcompare) = 0) , _ isnumeric(right(left(str, 7), 5)) ' found if len(str) = 8 rng.value = left(str, 7) elseif len(str) = 7 rng.value = str end if end if end if next j set rng = nothing next end sub
Comments
Post a Comment