excel - Modifying VBA Workbook Consolidation Code to Address Failure When a Column is Blank -
i having small problems modifying following code:
sub combine() lastcol = "g" folder = getfolder() filesavename = application.getsaveasfilename( _ title:="get saveas filename", _ filefilter:="excel files (*.xls*), *.xls*") if filesavename = false msgbox ("cannot save file - exiting macro") exit sub end if set newbk = workbooks.add set newsht = newbk.sheets(1) newsht .cells.clearcontents folder = folder & "\" fname = dir(folder & "*.xls*") while fname <> "" set bk = workbooks.open(filename:=folder & fname) each sht in bk.sheets thisbooklastrow = .range("a" & rows.count).end(xlup).row if thisbooklastrow = 1 newrow = 1 'copy header row sht.range("a1:" & lastcol & "1").copy _ destination:=.range("b1") 'put filename in cell a1 sht.range("a1") = "workbook" end if newrow = thisbooklastrow + 1 sht lastrow = .range("a" & rows.count).end(xlup).row datarows = lastrow - 1 set copyrange = .range("a2:" & lastcol & lastrow) end 'copy data old workbook workbook if datarows > 0 copyrange.copy _ destination:=.range("b" & newrow) 'put book name column .range("a" & newrow & ":a" & (newrow + datarows - 1)) = _ fname end if next sht bk.close savechanges:=false fname = dir() loop 'put totals in last row lastrow = .range("a" & rows.count).end(xlup).row newrow = lastrow + 1 .range("a" & newrow) = "total" lastcol = .cells(1, columns.count).end(xltoleft).column colcount = 4 lastcol set sumrange = .range(.cells(2, colcount), _ .cells(lastrow, colcount)) .cells(newrow, colcount).formula = _ "=sum(" & sumrange.address & ")" next colcount end newbk.saveas filename:=filesavename end sub function getfolder() 'declare variable filedialog object. dim fd filedialog 'create filedialog object folder picker dialog box. set fd = application.filedialog(msofiledialogfolderpicker) fd.title = "select excel workbook(s) folder" 'declare variable contain path 'of each selected item. though path string, 'the variable must variant because each...next 'routines work variants , objects. dim vrtselecteditem variant 'use with...end block reference folderdialog object. fd 'use show method display file picker dialog box , return user's action. 'the user pressed action button. if .show = -1 'step through each string in filedialogselecteditems collection. each vrtselecteditem in .selecteditems 'vrtselecteditem string contains path of each selected item. 'you can use file i/o functions want work path. 'this example displays path in message box. getfolder = vrtselecteditem next vrtselecteditem 'the user pressed cancel. else end if end 'set object variable nothing. set fd = nothing end function effectively code creating master book out of selected folder of excel files. great, each excel workbook in folder has 30 columns , 7 being transferred per file. suspect because 8th column, although has header, not have data in (it blank column). suspect above macro fails recognize , subsequently stops when hits column , moves onto next file. there way change code not happen? not sure changes in sectioned off area, or elsewhere in code, allow this.
the simplest change not major re-write change second line to:
lastcol = "ad"
that copy thirty columns rather seven.
Comments
Post a Comment