excel - Debugging. Copy a variable number of cells in one sheet and paste into another sheet -


the objective of code copy n number of rows , 3 columns of cells sheet2 last empty row in sheet1. attempted using cell properties in range copy line giving me runtime error '1004' (application-defined or object-defined error).

how can rectified?

private sub commandbutton1_click()     dim sum integer     n = 7     sheets("sheet2").range(cells(11, 15), cells((11 + n), 18)).copy     sheets("sheet1").range("a500").end(xlup).offset(1, 0)         .pastespecial xlpasteformats         .pastespecial xlpastevalues     end end sub 

one [issue] catches people out when passing range objects arguments range property if need specify worksheet object (which practice), need specify of range/cells properties use.

(source/more reading: http://excelmatters.com/referring-to-ranges-in-vba/)

you use:

with sheets("sheet2")     .range(.cells(11, 15), .cells((11 + n), 18)).copy end 

or:

sheets("sheet2").range(sheets("sheet2").cells(11, 15), sheets("sheet2").cells((11 + n), 18)).copy 

instead of:

sheets("sheet2").range(cells(11, 15), cells((11 + n), 18)).copy 

or build range this:

sheets("sheet2").range("o11:r" & (11 + n)).copy 

edited code:

private sub commandbutton1_click() dim sum integer n = 7 sheets("sheet2").range("o11:r" & (11 + n)).copy 'edited line sheets("sheet1").range("a500").end(xlup).offset(1, 0)     .pastespecial xlpasteformats     .pastespecial xlpastevalues end end sub 

Comments

Popular posts from this blog

python - TypeError: start must be a integer -

c# - DevExpress RepositoryItemComboBox BackColor property ignored -

django - Creating multiple model instances in DRF3 -