excel - Application defined or Object defined error. Copy from worksheet to sheet in another workbook -
i have piece of code checks if file open or not , based on criteria copies , pastes cell information. when program runs error message says "application defined or object defined error". line error thrown commented.
' check if file open ret = isworkbookopen("\\showdog\service\service jobs.xlsm") if ret = false ' open file set wkbdest = workbooks.open("\\showdog\service\service jobs.xlsm") set destsheet = wkbdest.sheets("customer information") 'perform copy set shttocopy = wkbsource.sheets("report") shttocopy.range("a11:j11").resize(xldown).copy 'error on line destsheet.range("a4:j4").cells(xldown).pastespecial application.displayalerts = false wkbdest.save wkbdest.close application.displayalerts = true
what doing wrong?
here's code started:
sub test() dim lastrow integer, lastcol integer, startrow integer dim ws worksheet, newws worksheet set ws = activesheet startrow = 11 'starting in row 11 - can change this. 'create , name new worksheet, paste into. sheets.add after:=sheets(sheets.count) 'add end of workbook activesheet.name = "new sheet" set newws = sheets("new sheet") ' starting in cell a11, what's last row in group of cells? ws if isempty(.cells(startrow + 1, 1)) ' check see if there's 1 row of data. lastrow = startrow ' if a12 blank, don't want last row 66000, set 11 last row else lastrow = .cells(startrow, 1).end(xldown).row end if ' starting in cell a11, how many columns right there? lastcol = .cells(startrow, 1).end(xltoright).column ' now, let's copy range - note: don't need copy, commented out. copy if didn't. ' .range(.cells(startrow, 1), .cells(lastrow, lastcol)).copy end ' paste data new sheet dim newlastrow integer 'what row starting row info? newlastrow = newws.cells(66000, 1).end(xlup).row + 1 'starting in a66000, what's next row info? add 1 because want start on next blank row newws.range(newws.cells(startrow, 1), newws.cells(lastrow, lastcol)) = ws.range(ws.cells(startrow, 1), ws.cells(lastrow, lastcol)).value application.cutcopymode = false end sub
you don't need copy/paste - can set 1 range's value equal of another.
without idea why want paste row 4 on new sheet, have copying data same range original sheet. if let me know why row 4 (is "next" open row, starting @ row 1?), can make tweaks.
i hope code makes sense, tried comment would. naturally, need feather in variables, should going.
as resize(), i'm pretty sure have declare both column , row resizing. if you're not resizing width (columns) or height (rows), put "1" resize number.