vba - Copy and paste every 10 Excel rows into another workbook loop -
from excel workbook (original_workbook.xlsx) of 100 rows (or number of rows, preferably).
i macro copies , pastes every 10 rows new excel workbook named workbook_[date]_[random number].xlsx.
note: first row of original_workbook.xlsx header each new workbook. using recorded macro, have following vb code far:
workbooks.add activeworkbook.saveas filename:= _ "c:\path\workbook_[date]_[random number].xlsx", fileformat:= _ xlopenxmlworkbook, createbackup:=false windows("original_workbook.xlsx").activate rows("1:1").select selection.copy windows("workbook_[date]_[random number].xlsx").activate activesheet.paste windows("original_workbook.xlsx").activate rows("2:11").select application.cutcopymode = false selection.copy windows("workbook_[date]_[random number].xlsx").activate range("a2").select activesheet.paste range("a2").select application.cutcopymode = false activeworkbook.save windows("original_workbook.xlsx").activate windows("workbook_[date]_[random number].xlsx").activate
i above code loop until there no more rows left.
i suspect rows("2:11").select
need change. i've looked doing i, wasn't working. thank you.
recording macro great way idea of framework , individual commands involved in proves resulting code can verbose in areas , lacking in others.
your recorded macro lacking names of worksheets being copied , pasted , from. not part of recording active worksheets. purposes of code below, i've used sheet1 on both workbooks.
dim rw long, nwb workbook, owb workbook set owb = windows("original_workbook.xlsx") set nwb = workbooks.add nwb.saveas filename:= _ "c:\path\workbook_[date]_[random number].xlsx", fileformat:= _ xlopenxmlworkbook, createbackup:=false owb.sheets("sheet1") .rows("1:1").copy _ destination:=nwb.sheets("sheet1").range("a1") rw = 2 92 step 10 .cells(rw, 1).resize(10, 1).entirerow.copy _ destination:=nwb.sheets("sheet1").range("a2").offset(rw, 0) next rw nwb.save .activate end nwb.activate
i've set loop isn't clear want results. if stacked 1 on top of other infinitely easier copy , paste them @ once.
see how avoid using select in excel vba macros more methods on getting away relying on select , activate accomplish goals.
Comments
Post a Comment