Excel VBA iterate over all used cells and substitute their values -


this first time ever working vba , excel , problem caused big lack of knowledge on part.

i have 1 column list of city names (containing 800 items, hence i'm looking automatically replace them) , chunks of text in term "cityname" occurs multiple times , needs replaced correct city name, in setup value of first column cell in same row.

so found this: how iterate through cells in excel vba or vsto 2005

and found instr , substitute functions looking through excel vba reference.

using iteration works fine:

sub mysubstitute()     dim cell range         each cell in activesheet.usedrange.cells             if instr(cell.value, "cityname") > 0                 msgbox "hello world!"         end if     next cell end sub 

i message box every "cityname" in sheet (a test sheet 2 rows).

however when add want achieve runtime error (1004):

sub mysubstitute()     dim cell range     each cell in activesheet.usedrange.cells         if instr(cell.value, "cityname") > 0             cell.value = worksheetfunction.substitute(cell.value, "cityname", cells(cell.row, a))         end if     next cell end sub 

so guess in line wrong can't figure out what. hints mistake appreciated, :)

you should change:

 cell.value = worksheetfunction.substitute(cell.value, "cityname", cells(cell.row, a)) 

by

 cell.value = worksheetfunction.substitute(cell.value, "cityname", cells(cell.row, 1)) 

Comments

Popular posts from this blog

Hatching array of circles in AutoCAD using c# -

ios - UITEXTFIELD InputView Uipicker not working in swift -

Python Pig Latin Translator -