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

混江龙づ霸主 提交于 2021-02-11 13:05:53

问题


Given a certain range rngRange e.g. 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 (e.g. 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)?


回答1:


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


来源:https://stackoverflow.com/questions/63374319/select-referenced-range-from-non-active-table-via-vba-in-excel

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!