vba - Excel - activating a previous workbook after leaving it -
i working on current workbook in excel. in vba, have done following :
sheets("upload file").select cells.select selection.copy workbooks.add selection.pastespecial paste:=xlpastevalues, operation:=xlnone, skipblanks _ :=false, transpose:=false
so now, "book1" new workbook. once i've done need on there, need activate original workbook, 1 have vba etc.
i close "book1" :
activewindow.close
but in next sub in process, can't call of sheets within active workbook :
sheets("upload file").range("a1:ab65536").clearcontents
as "subscript out of range" error due workbook "upload file" sits in not being activated again.
thank taking time @ this.
the best way deal work object model: declare objects workbooks need keep track of , assign workbooks them. such objects don't depend on what's "active" or "selected". example:
dim wkboriginal excel.workbook dim wkbnew excel.workbook set wkboriginal = activeworkbook 'do things here set wkbnew = workbooks.add 'do more things wkbnew.close 'wkboriginal still accessible , can used in code
Comments
Post a Comment