vba - Deleting columns in excel using visual basic? -
i have 750 excel files. need clean them contain same formatting - i.e. contain same number of columns.
some files (80%) contain columns containing labels asterisk e.g. "*1 subject".
is there way using visual basic go through of files in folder delete columns containing asterisk files don't have such columns? fact asterisk wild card in computer speak make difference?
write macro uses filesystemobjects loop through directory spreadsheets are. loop through each sheet , analyse column names.
here how loop through each sheet.
private sub commandbutton7_click() dim ws excel.worksheet dim icol integer dim strname string dim iindex integer 'loop through sheets. iindex = 1 application.worksheets.count set ws = application.worksheets(iindex) 'loop through columns. icol = 1 ws.usedrange.columns.count 'check row 1 of column first char of * if left(ws.cells(1, icol).value, 1) = "*" 'we have found column first char of * ws.columns(icol).entirecolumn.delete end if next icol next iindex activeworkbook.saveas filename:="c:\temp\newfiles\" & activeworkbook.name, fileformat:=xlworkbooknormal end sub
if want * anywhere in cell use instr()
private sub commandbutton7_click() dim ws excel.worksheet dim icol integer dim strname string dim iindex integer 'loop through sheets. iindex = 1 application.worksheets.count set ws = application.worksheets(iindex) 'loop through columns. icol = 1 ws.usedrange.columns.count 'check row 1 of column char of * if instr(ws.cells(1, icol).value, "*") > 0 'we have found column char of * ws.columns(icol).entirecolumn.delete end if next icol next iindex activeworkbook.saveas filename:="c:\temp\newfiles\" & activeworkbook.name, fileformat:=xlworkbooknormal end sub
here basic loop files in given directory. hope gets there.
private sub commandbutton7_click() dim wb workbook dim ws excel.worksheet dim icol integer dim strname string dim iindex integer dim strpath string dim strfile string strpath = "c:\temp\oldfiles\" strfile = dir(strpath & "*.xlsx") while strfile <> "" set wb = workbooks.open(filename:=strpath & strfile) 'loop through sheets. iindex = 1 application.worksheets.count set ws = application.worksheets(iindex) 'loop through columns. icol = 1 ws.usedrange.columns.count 'check row 1 of column char of * if instr(ws.cells(1, icol).value, "*") > 0 'we have found column char of * ws.columns(icol).entirecolumn.delete end if next icol next iindex wb.saveas filename:="c:\temp\newfiles\" & wb.name, fileformat:=xlopenxmlworkbook wb.close savechanges:=false strfile = dir loop end sub
Comments
Post a Comment