VBA - open excel, find and replace, delete row, save as csv -
i trying write program in vba can remotely manipulate excel file sas (a statistical programming software). want program accomplish following:
- open specified excel file
- find , replace blanks in header row nothing (e.g., "test name" become "testname")
- delete second row if first cell in row (i.e., a2) blank
- save file csv
i not know vba, have dabbled little, know other programming languages, , have tried piece together. stole code make 1 , 4 work. cannot 2 , 3 work. have:
i put following in sas call vba program -
x 'cd c:\location';/*change location of vbs file*/ x "vbsprogram.vbs inputfile.xls outputfile.csv";
the vba program -
'1 - open specified excel file if wscript.arguments.count < 2 wscript.echo "error! please specify source path , destination. usage: xlstocsv sourcepath.xls destination.csv" wscript.quit end if dim oexcel set oexcel = createobject("excel.application") dim obook set obook = oexcel.workbooks.open(wscript.arguments.item(0)) '2 - find , replace obook.worksheets(1).range("a1:g1").select selection.replace what:="* *", replacement:="", lookat:=xlpart, _ searchorder:=xlbyrows, matchcase:=false '3 - delete second row if blank obook.cell("a2").specialcells(xlcelltypeblanks).entirerow.delete '4 - save csv obook.saveas wscript.arguments.item(1), 6 obook.close false oexcel.quit wscript.echo "done"
any assistance pointing me in right direction appreciated.
also, there way select data in row 1 range (in part 2) , not have specify set range of cells?
try:
'2: - find , replace obook.worksheets(1).cells(2,1).entirerow.replace " ", "" '3 - delete second row if blank if oexcel.counta(obook.worksheets(1).cells(2,1).entirerow) = 0 obook.worksheets(1).cells(2,1).entirerow.delete end if
Comments
Post a Comment