excel vba - Macro to copy and paste 5 last columns problems -


i've written macro find last 5 columns data , copy , paste preceeding last column. built macro in dummy document not corrupt current file , got macro working fine below code:

sub copylastfiverows()  lastcolumn = sheets("sheet2").cells.find("*", [a1], , , xlbycolumns, xlprevious).column on error goto 0  sheets("sheet2").columns(lastcolumn - 4).resize(, 5).select selection.copy  '   enter rest of paste code here  sheets("sheet2").columns(lastcolumn + 1).select activesheet.paste  end sub 

when cut , paste above if in actual working file works fine if data tab name "sheet2", if im change tab name "nfg" , replace "sheet2" in macro above "run-time error '1004' select method of range class failed". bit confused. if me out that'd great. thanks.

note - other thing forgot mention im going assign macro button on front sheet of workbook. think might causing problem variable can think of between documents i've tested macro on.

use following code copy rows of last 5 columns

sub copylastfivecolumns()      dim lnglastrow     long      thisworkbook.worksheets("nfg")          lnglastrow = .range("a" & .rows.count).end(xlup).row          .cells(1, .columns.count).end(xltoleft).offset(, -4).resize(lnglastrow, 5).copy .cells(1, .columns.count).end(xltoleft).offset(, 1)     end  end sub 

Comments

Popular posts from this blog

c# - Operator '==' incompatible with operand types 'Guid' and 'Guid' using DynamicExpression.ParseLambda<T, bool> -