excel - VBA - Replacing a string in text file with a variable value from other file -
i'm new vba , stuck @ 1 point of code. appreciate if .
question: have excel file 2 columns, need search strings 1 one first column , if there match in other file, xml file, need replace string corresponding string in second column.
problem: wrote below code , working fine me hard-coded value, when try replace using variables, run's doesn't anything. changed code one. but, error.
can me ?
here code : (which working hard-coded value)
private sub commandbutton1_click() call exlfileread end sub sub exlfileread() dim exlfile string dim oldvalue string dim newvalue string dim x long dim countrow long dim y long dim z long y = 1 z = 2 exlfile = application.getopenfilename()` workbooks.open (exlfile)` open exlfile input #1 activesheet countrow = .cells(.rows.count, "a").end(xlup).row end x = 1 countrow call xmlfile(activesheet.cells(x, y).value, activesheet.cells(x, z).value) next x close end sub sub xmlfile(rowoldvalue string, rownewvalue string) const forreading = 1, forwriting = 2 dim fso, filein, fileout dim strtmp set fso = createobject("scripting.filesystemobject") set filein = fso.opentextfile("c:\users\ashisha\desktop\exp\note.xml", forreading) set fileout = fso.opentextfile("c:\users\ashisha\desktop\exp\note1.xml", forwriting, true) until filein.atendofstream strtmp = filein.readline if len(strtmp) > 0 if instr(1, strtmp, rowoldvalue, vbtextcompare) = 0 strrep = replace(strtmp, "abcd", "new text") fileout.writeline strrep end if end if loop filein.close fileout.close end sub enter code here ---------------------------------------------------------------------- -----this 1 executing, not replacing----------- -----i have old value in rowoldvalue , value be---------- -------------------replaced in rownewvalue---------------------------- ********************************************************************** private sub commandbutton1_click() call exlfileread end sub sub exlfileread() dim exlfile string dim oldvalue string dim newvalue string dim x long dim countrow long dim y long dim z long y = 1 z = 2 exlfile = application.getopenfilename()` workbooks.open (exlfile)` open exlfile input #1 activesheet countrow = .cells(.rows.count, "a").end(xlup).row end x = 1 countrow call xmlfile(activesheet.cells(x, y).value, activesheet.cells(x, z).value) next x close end sub sub xmlfile(rowoldvalue string, rownewvalue string) const forreading = 1, forwriting = 2 dim fso, filein, fileout dim strtmp set fso = createobject("scripting.filesystemobject") set filein = fso.opentextfile("c:\users\ashisha\desktop\exp\note.xml", forreading) set fileout = fso.opentextfile("c:\users\ashisha\desktop\exp\note1.xml", forwriting, true) until filein.atendofstream strtmp = filein.readline if len(strtmp) > 0 if instr(1, strtmp, rowoldvalue, vbtextcompare) = 0 strrep = replace(strtmp, rowoldvalue, rownewvalue) fileout.writeline strrep end if end if loop filein.close fileout.close end subi'm new vba , stuck @ 1 point of code. appreciate if . question: have excel file 2 columns, need search strings 1 one first column , if there match in other file, xml file, need replace string corresponding string in second column.
problem: wrote below code , working fine me hard-coded value, when try replace using variables, run's doesn't anything. changed code one. but, error.
can me ?
here code : (which working hard-coded value)
private sub commandbutton1_click() call exlfileread end sub sub exlfileread() dim exlfile string dim oldvalue string dim newvalue string dim x long dim countrow long dim y long dim z long y = 1 z = 2 exlfile = application.getopenfilename()` workbooks.open (exlfile)` open exlfile input #1 activesheet countrow = .cells(.rows.count, "a").end(xlup).row end x = 1 countrow call xmlfile(activesheet.cells(x, y).value, activesheet.cells(x, z).value) next x close end sub sub xmlfile(rowoldvalue string, rownewvalue string) const forreading = 1, forwriting = 2 dim fso, filein, fileout dim strtmp set fso = createobject("scripting.filesystemobject") set filein = fso.opentextfile("c:\users\ashisha\desktop\exp\note.xml", forreading) set fileout = fso.opentextfile("c:\users\ashisha\desktop\exp\note1.xml", forwriting, true) until filein.atendofstream strtmp = filein.readline if len(strtmp) > 0 if instr(1, strtmp, rowoldvalue, vbtextcompare) = 0 strrep = replace(strtmp, "abcd", "new text") fileout.writeline strrep end if end if loop filein.close fileout.close end sub
Comments
Post a Comment