excel - How to use pastespecial with End(xlUp) -


i having issue getting data 1 sheet paste special sheet, trying consolidate multiple files (same headers, differing number of rows) 1 master sheet containing rows. @ moment i'm doing opening files, pulling in tabs want, copy , pasting data, , deleting tabs. yes sure there easier way, i'm new vba , learning on fly..here's have far:

sub consolidatesheets()  ' open each file in folder dim folder string dim files string folder = "c:\users\212411103\documents\risk project tracker\risk project tracker monthly\monthly data" files = dir(folder & "\*.xls") while files <> "" workbooks.open filename:=folder & "\" & files files = dir loop  ' pull in risk project tracker tab each file new workbook  dim wkb workbook dim swksname string  swksname = "risk project tracker" each wkb in workbooks if wkb.name <> thisworkbook.name     wkb.worksheets(swksname).copy _       before:=thisworkbook.sheets(1) end if next set wkb = nothing  dim j integer  ' add new sheet combined data  on error resume next sheets(1).select worksheets.add sheets(1).name = "new month"  ' paste headers first 2 rows new sheet "new month"  sheets(2).select range("a1:ah2").select selection.copy sheets("new month").select selection.pastespecial paste:=xlpastecolumnwidths, operation:=xlnone, _     skipblanks:=false, transpose:=false activesheet.paste range("a1").select  ' work through sheets j = 2 sheets.count ' sheet 2 last sheet sheets(j).activate ' make sheet active rows("1:2").select selection.delete shift:=xlup  range("a1:ah500").select selection.copy sheets("new month").select selection.pastespecial paste:=xlpastecolumnwidths, operation:=xlnone, _     skipblanks:=false, transpose:=false range("a1").select  next  ' delete tabs no longer needed i.e. tabs 17 files  ' each ws in sheets ' application.displayalerts=false ' if ws.name <> "new month" ws.delete ' next ' application.displayalerts=true  end sub 

it appears primary reason specifying range .pastespecial method carry-over of column widths done every tab. perhaps cycling through a:ah once , setting column widths should sufficient.

sub consolidatesheets2()     dim fldr string, fn string, swksname string, snewwksname string     dim ws worksheet, wkb workbook      on error goto bm_safe_exit     application.screenupdating = false     application.enableevents = false      swksname = "risk project tracker"     fldr = "c:\users\212411103\documents\risk project tracker\risk project tracker monthly\monthly data"     fn = dir(fldr & "\*.xls")     snewwksname = "new month"      thisworkbook         while fn <> ""             set wkb = workbooks.open(filename:=fldr & chr(92) & fn)             if isobject(wkb.worksheets(swksname))                 wkb.worksheets(swksname).copy _                     before:=thisworkbook.sheets(1 - cbool(sheets(1).name = snewwksname))                 on error goto bm_need_new_month_ws                 .worksheets(snewwksname)                     on error goto bm_safe_exit                     .parent.sheets(2).range("a3:ah502").copy _                         destination:=.cells(rows.count, 1).end(xlup).offset(1, 0)                 end             end if             wkb.close false             fn = dir         loop         application.displayalerts = false         while sheet.count > 1: sheets(2).delete: loop     end      goto bm_safe_exit  bm_need_new_month_ws:     if err.number = 9         thisworkbook.worksheets.add(before:=thisworkbook.sheets(1))             .name = snewwksname             .move before:=sheets(1)             .parent.sheets(2).range("a1:ah2").copy _                 destination:=.range("a1")             c = .columns("ah:ah").column 1 step -1                 .columns(c).columnwidth = _                     .parent.sheets(2).columns(c).columnwidth             next c         end         resume     end if  bm_safe_exit:     application.screenupdating = true     application.enableevents = true  end sub 

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -