excel - Copy Common Columns to separate sheet -
i tried below code copy entire column 1 page other page.
i've common header june scattered in sheet, want copy columns, header name "june" in separate sheet 1 after other.
eg if col a, c,l,m has column header june, on next sheet these should copied a,b,c,d.
sheets("sheet1").select june = worksheetfunction.match("description", rows("1:1"), 0) sheets("sheet1").columns(june).copy destination:=sheets("sheet2").range("a1")
following might help:
option explicit sub demo() dim srcsht worksheet, destsht worksheet dim headerrng range, cel range, copyrng range dim lastcol long, col long set srcsht = thisworkbook.sheets("sheet1") 'sheet1 set destsht = thisworkbook.sheets("sheet2") 'sheet2 lastcol = srcsht.cells(1, srcsht.columns.count).end(xltoleft).column 'last column of sheet1 srcsht each cel in .range(.cells(1, 1), .cells(1, lastcol)) 'loop through each header name in sheet1 if cel = "june" 'check header "june" if copyrng nothing 'assign range copy set copyrng = .columns(cel.column) else set copyrng = union(copyrng, .columns(cel.column)) end if end if next cel end copyrng.copy destsht.range("a1") 'copy , paste desired columns end sub
Comments
Post a Comment