问题
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