excel - VBA - Select method failed -
i pretty new vba, sorry if problem obvious more experienced users. tried read couple of answers similar question, , fix problem still facing same problem.
my code is:
workbooks("xxx.xls").activate ' setting column width workbooks("xxx.xls").worksheets("xxx").cells.select selection.columnwidth = 10 ' filtering xxx funds workbooks("xxx.xls").worksheets("xxx").rows("1:1").select selection.autofilter field:=3, criteria1:="=*xxx*" ' add new sheet , rename sheets.add after:=worksheets(worksheets.count) activesheet.name = "xxx_f" 'copying needed information workbooks("xxx.xls").worksheets("xxx").range("a1:c1").select range(selection, selection.end(xldown)).select selection.copy
the error getting "run time error '1004': select method of range class failed" on third line bottom. tried fix adding line (i.e. activating workbook want work first):
workbooks("xxx.xls").activate
also used explicit references in code, like:
workbooks("xxx.xls").worksheets("xxx").range("a1:c1").select
i understand better not use .select method in code @ all. not first time getting error, , want understand vba's logic. understand excel not understand sheet referring, not understand why - have activated workbook need (i have couple of workbooks opened) , using explicit references. if please explain in detail why error occurs (i.e in point in time excel confuses), grateful!
thanks in advance!
the comments on question have sufficiently described nature of 1004
error, , seem have enough understanding of that.
see great discussion more detail:
how avoid using select in excel vba macros
here code refactored avoid select
method.
dim wb workbook dim ws worksheet dim newws worksheet dim rngcopy range set wb = workbooks("xxx.xls") set ws = wb.worksheets("xxx") ws ' setting column width .cells.columnwidth = 10 ' filtering xxx funds .rows(1).autofilter field:=3, criteria1:="=*xxx*" set rngcopy = .range(.range("a1:c1"), .range("a1:c1").end(xldown)) end ' add new sheet , rename wb set newws = .sheets.add(after:=.worksheets(.worksheets.count)) end newws.name = "xxx_f" ' paste data in new worksheet rngcopy destination:=newws.cells(1,1)
Comments
Post a Comment