Monday 15 July 2013

How to import worksheet with desired columns? Excel VBA -



How to import worksheet with desired columns? Excel VBA -

i'm able import worksheets workbook. possible import columns want? info huge , don't want have problem go through every part of cells. below codes:

sub importsheet() dim wb workbook dim activewb workbook dim sheet worksheet dim filepath string dim ows string set activewb = application.activeworkbook filepath = "c:\report.xlsx" application.screenupdating = false application.displayalerts = false set wb = application.workbooks.open(filepath) wb.sheets("report").copy after:=activewb.sheets(activewb.sheets.count) activewb.activate wb.close false application.screenupdating = true application.displayalerts = true end sub

not sure if i'm breaking protocol here different approach , alternative add answer there. method uses 'copy new worksheet' approach should easier on limited resources.

sub importsheet() dim iwb workbook, awb workbook, ws worksheet dim filepath string, v long, vcols variant application.screenupdating = false application.displayalerts = false filepath = "c:\report.xlsx" vcols = array(1, 13, 6, 18, 4, 2) 'columns re-create in order set awb = application.activeworkbook awb .sheets.add after:=.sheets(.sheets.count) set ws = .sheets(.sheets.count) '.name = "report" 'you can name new ws not duplicate end set iwb = application.workbooks.open(filepath) iwb.sheets("report").cells(1, 1).currentregion .cells = .cells.value v = lbound(vcols) ubound(vcols) .columns(vcols(v)).copy destination:=ws.cells(1, v + 1) next v end iwb.close false set iwb = nil set ws = nil set awb = nil application.displayalerts = true application.screenupdating = true end sub

my primary concern here not knowing layout of 'report' worksheet. boundaries of .currentregion dictated first blank column right , first blank row down. block of info has worksheets called report do.

excel vba excel-vba import worksheet

No comments:

Post a Comment