excel - select method of range class failed -
i have same problem when executing excel vba script @ line rows(target.row).select tried select range , can failed.
function doone(rowindex integer) boolean dim key dim target dim success success = false if not isempty(cells(rowindex, 1).value) key = cells(rowindex, 1).value sheets("sheet1").select set target = columns(4).find(key, lookin:=xlvalues) if not target nothing rows(target.row).select [- here throws "select method of range class failed"-] selection.copy sheets("sheet2").select rows(rowindex + 1).select selection.insert shift:=xldown rows(rowindex + 2).select application.cutcopymode = false selection.insert shift:=xldown, copyorigin:=xlformatfromleftorabove cells(rowindex + 3, 1).select success = true end if end if doone = success end function
where code? in worksheet module? when call cells or rows , don't put sheet in front of it, it's called unqualified reference. unqualified references refer different sheets depending on code is. in standard module, refer activesheet. in worksheet's class module, refer sheet.
if code in sheet2's class module, unqualified rows.select statement try select row on sheet2 when sheet1 active.
the best practice use qualified references , select , activate sheets , ranges when need to. here's example:
function doone(rowindex long) boolean dim rtarget range dim bsuccess boolean dim sh1 worksheet, sh2 worksheet dim rkey range bsuccess = false set sh1 = thisworkbook.worksheets("sheet1") set sh2 = thisworkbook.worksheets("sheet2") set rkey = sh2.cells(rowindex, 1) if not isempty(rkey.value) set rtarget = sh1.columns(4).find(what:=rkey.value, lookin:=xlvalues) if not rtarget nothing rkey.offset(1, 0).entirerow.insert rtarget.entirerow.copy rkey.offset(1, 0).entirerow rkey.entirerow.copy rkey.offset(1, 0).entirerow.pastespecial xlpasteformats application.cutcopymode = false bsuccess = true end if end if doone = bsuccess end function
Comments
Post a Comment