excel vba - RT-1004 when copying data from destination to Source workbooks -
i use code copy data workbook import report. however, month progresses , amount of data grows, amount of time run sub (in last week of january took 3 minutes process 900 rows of data):
sub extract_sort_1602_february() dim ans long ans = msgbox("is february 2016 swivel master file checked out of sharepoint , open on desktop?", vbyesno + vbquestion + vbdefaultbutton1, "master file open") if ans = vbno or iswbopen("swivel - master - february 2016") = false msgbox "the required workbook not open. procedure terminate.", vbokonly + vbexclamation, "terminate procedure" exit sub end if application.screenupdating = false ' line autofits columns c, d, o, , p range("c:c,d:d,o:o,p:p").columns.autofit ' unhides hidden rows cells.entirerow.hidden = false dim lr long lr = range("b" & rows.count).end(xlup).row 2 step -1 if range("b" & lr).value <> "2" rows(lr).entirerow.delete end if next lr application.run "'swivel - master - february 2016.xlsm'!unfilter" activeworkbook.worksheets("extract").sort .sortfields .clear .add key:=range("b2:b2000"), sorton:=xlsortonvalues, order:=xlascending, dataoption:=xlsortnormal .add key:=range("d2:d2000"), sorton:=xlsortonvalues, order:=xlascending, dataoption:=xlsortnormal .add key:=range("o2:o2000"), sorton:=xlsortonvalues, order:=xlascending, dataoption:=xlsortnormal .add key:=range("j2:j2000"), sorton:=xlsortonvalues, order:=xlascending, dataoption:=xlsortnormal .add key:=range("k2:k2000"), sorton:=xlsortonvalues, order:=xlascending, dataoption:=xlsortnormal .add key:=range("l2:l2000"), sorton:=xlsortonvalues, order:=xlascending, dataoption:=xlsortnormal end .setrange range("a2:ae2000") .apply end cells.wraptext = false sheets("extract").range("a2").select dim lastrow integer, integer, erow integer lastrow = activesheet.range("a" & rows.count).end(xlup).row = 2 lastrow if cells(i, 2) = "2" ' opposed selecting cells, copy them directly range(cells(i, 1), cells(i, 31)).copy ' opposed "activating" workbook, , selecting sheet, paste cells directly workbooks("swivel - master - february 2016.xlsm").sheets("swivel") erow = .cells(.rows.count, 1).end(xlup).offset(1, 0).row .cells(erow, 1).pastespecial xlpasteall end application.cutcopymode = false end if next application.screenupdating = true end sub i asked in code review more efficient way achieve intended results , came this:
sub extract_sort_1602_february() dim ans long ans = msgbox("is february 2016 swivel master file checked out of sharepoint , open on desktop?", vbyesno + vbquestion + vbdefaultbutton1, "master file open") if ans = vbno or iswbopen("swivel - master - february 2016") = false msgbox "the required workbook not open. procedure terminate.", vbokonly + vbexclamation, "terminate procedure" exit sub end if application.screenupdating = false ' line autofits columns c, d, o, , p range("c:c,d:d,o:o,p:p").columns.autofit ' unhides hidden rows cells.entirerow.hidden = false dim lr long lr = range("b" & rows.count).end(xlup).row 2 step -1 if range("b" & lr).value <> "2" rows(lr).entirerow.delete end if next lr application.run "'swivel - master - february 2016.xlsm'!unfilter" sourceworksheet.sort .sortfields .clear .add key:=range("b2:b2000"), sorton:=xlsortonvalues, order:=xlascending, dataoption:=xlsortnormal .add key:=range("d2:d2000"), sorton:=xlsortonvalues, order:=xlascending, dataoption:=xlsortnormal .add key:=range("o2:o2000"), sorton:=xlsortonvalues, order:=xlascending, dataoption:=xlsortnormal .add key:=range("j2:j2000"), sorton:=xlsortonvalues, order:=xlascending, dataoption:=xlsortnormal .add key:=range("k2:k2000"), sorton:=xlsortonvalues, order:=xlascending, dataoption:=xlsortnormal .add key:=range("l2:l2000"), sorton:=xlsortonvalues, order:=xlascending, dataoption:=xlsortnormal end .setrange range("a2:ae2000") .apply end cells.wraptext = false sheets("extract").range("a2").select dim sourceworkbook workbook set sourceworkbook = workbooks("tempimport.xlsx") dim destinationworkbook workbook set destinationworkbook = workbooks("swivel - master - february 2016.xlsm") dim sourceworksheet worksheet set sourceworksheet = sourceworkbook.sheets("extract") dim destinationworksheet worksheet set destinationworksheet = destinationworkbook.sheets("swivel") dim lastrow integer lastrow = sourceworksheet.range("a" & rows.count).end(xlup).row dim sourcerow integer dim destinationrow integer destinationrow = destinationworksheet.cells(rows.count, 1).end(xlup) + 1 sourcerow = 2 lastrow if cells(sourcerow, 2) = "2" destinationworksheet.rows(destinationrow) = sourceworksheet.rows(sourcerow) ' run-time error occurs destinationrow = destinationrow + 1 end if next sourcerow call extractsave application.screenupdating = true end sub but there a
run-time error '1004': application-defined or object-defined error
for line:
destinationworksheet.rows(destinationrow) = sourceworksheet.rows(sourcerow) i have included 2 snapshots of source data , target workbook.
this sub used clear filters prior copy/paste.
sub unfilter() dim variant each in thisworkbook.worksheets if she.filtermode she.showalldata next end sub
try code (on temp copy of workbooks):
sub extract_sort_1602_february() dim ans long dim lr long dim urng range dim worksheet ans = msgbox("is february 2016 swivel master file checked out of sharepoint , open on desktop?", vbyesno + vbquestion + vbdefaultbutton1, "master file open") if ans = vbno or iswbopen("swivel - master - february 2016") = false msgbox "the required workbook not open. procedure terminate.", vbokonly + vbexclamation, "terminate procedure" exit sub end if dim sourceworkbook workbook set sourceworkbook = workbooks("tempimport.xlsx") dim destinationworkbook workbook set destinationworkbook = workbooks("swivel - master - february 2016.xlsm") dim sourceworksheet worksheet set sourceworksheet = sourceworkbook.sheets("extract") dim destinationworksheet worksheet set destinationworksheet = destinationworkbook.sheets("swivel") application.screenupdating = false application.enableevents = false application.calculation = xlcalculationmanual ' line autofits columns c, d, o, , p sourceworksheet.range("c:c,d:d,o:o,p:p").columns.autofit ' unhides hidden rows sourceworksheet.cells.entirerow.hidden = false lr = sourceworksheet.range("b" & rows.count).end(xlup).row 2 step -1 if sourceworksheet.range("b" & lr).value <> "2" if urng nothing set urng = sourceworksheet.rows(lr) else set urng = union(urng, sourceworksheet.rows(lr)) end if end if next lr if not urng nothing urng.delete 'application.run "'swivel - master - february 2016.xlsm'!unfilter" each in destinationworkbook.worksheets if she.filtermode she.showalldata next sourceworksheet.sort .sortfields .clear .add key:=range("b2:b2000"), sorton:=xlsortonvalues, order:=xlascending, dataoption:=xlsortnormal .add key:=range("d2:d2000"), sorton:=xlsortonvalues, order:=xlascending, dataoption:=xlsortnormal .add key:=range("o2:o2000"), sorton:=xlsortonvalues, order:=xlascending, dataoption:=xlsortnormal .add key:=range("j2:j2000"), sorton:=xlsortonvalues, order:=xlascending, dataoption:=xlsortnormal .add key:=range("k2:k2000"), sorton:=xlsortonvalues, order:=xlascending, dataoption:=xlsortnormal .add key:=range("l2:l2000"), sorton:=xlsortonvalues, order:=xlascending, dataoption:=xlsortnormal end .setrange range("a2:ae2000") .apply end sourceworksheet.cells.wraptext = false dim lastrow integer lastrow = sourceworksheet.range("a" & rows.count).end(xlup).row 'dim sourcerow integer dim destinationrow integer destinationrow = destinationworksheet.cells(rows.count, 1).end(xlup).row + 1 sourceworksheet.range("a2:aa" & lastrow).copy destinationworksheet.range("a" & destinationrow) 'for sourcerow = 2 lastrow ' if cells(sourcerow, 2) = "2" ' destinationworksheet.rows(destinationrow) = sourceworksheet.rows(sourcerow) ' run-time error occurs ' destinationrow = destinationrow + 1 ' end if 'next sourcerow call extractsave application.screenupdating = true application.enableevents = true application.calculation = xlcalculationautomatic end sub 
Comments
Post a Comment