简体   繁体   中英

Select referenced range from non-active table via VBA in Excel

Given a certain range rngRange eg set rngRange = Workbooks(2).Worksheets(5).Range("C4:P49") If I use rngRange.select it will throw an error message if the table containing the range is not the active window/workbook/table (eg by wbWorkbook.Activate and wsWorsheet.Activate ). Using rngRange.Activate by itself doesn't work (throws an error), probable a simple problem. But somehow I'm blind today.

Is it possible to "activate" the range directly without activating the workbook/worksheet first? And if not, can I get a workbook/worksheet reference from the range reference somehow (note, the whole thing is inside a function that only gets the range reference, I would like to avoid to add a wbWorkbook/worksheet reference since I have to change all function calls as well)?

rngRange.Parent 'gives you the sheet
rngRange.Parent.Parent 'gives you the workbook

So you can use them to .Activate them.

rngRange.Parent.Parent.Activate
rngRange.Parent.Activate
rngRange.Select

Alternatively as @Viktor mentioned:

Application.Goto rngRange

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM