string - VBA macro copies same data to each cell -
i have macro:
sub stringchecker() dim string_arr() variant dim k integer dim c range set c = activesheet.[a1] end_string = array(" &", _ " tr", _ " sr", _ " defen") substring = array(" sr ", _ " jr ") while c <> "end loop" c.offset(0, 1) = c k = 0 ubound(end_string) if right(c, len(end_string(k))) = end_string(k) cleaner_string = mid(c, 1, len(c) - len(end_string(k))) end if next k clean_string = cleaner_string l = 0 ubound(substring) clean_string = replace(clean_string, substring(l), " ") next l if clean_string = "" clean_string = c end if c.offset(0, 1) = clean_string set c = c.offset(1, 0) loop end sub
the problem when finds character in array matches current string iteration, ends copying string on every cell iteration thereafter.
here's example of output:
owner1 owner1 burdines 1225 llc burdines 1225 llc miami-dade county miami-dade county related group of florida & related group of florida burdines 1225 llc related group of florida city of miami dept of p & d related group of florida miami-dade county related group of florida fec r r co related group of florida bdg 200 south miami ave llc related group of florida state of florida dot related group of florida
it copies on related group of florida during each iteration after finds that cell ended & part of our end_string array. same thing may occuring substring array. looked @ closely, , not sure why behavior occurs. it's supposed removing characters in cell found in both end_string , substring array , copying them new cell. if indexes array not found in string, copies on original string column b. use string "end loop" because cannot check empty string since there may 4 or 5 empty cells in column before there cell content, dont want iteration stop because 1 cell empty.
thanks response
you need wipe cleanerstring @ beginning of every pass through loop. otherwise, retains value last time through, , if cell doesn't meet criteria cleanerstring edited, cleanstring becomes cleanerstring (which last iteration's clearnerstring), cleanstring never "". make sense?
edit
to make clearer:
if cell you're looking @ doesn't meet criteria editing, cleanerstring never set new. if given value last time through, still has value. thus, when say
cleanstring = cleanerstring
if cleanerstring never set new, cleanstring has cleanerstring value last time through. cleanstring gets cleaned in formatting code, , when check if
cleanstring = ""
you evaluate false, because cleanstring last time's cleanerstring, cleaned-up again. cleanstring not reset c,
c.offset(0, 1) = clean_string
sets cell cleaned-up text last time.
Comments
Post a Comment