ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   how can vba detect what is currently selected? (https://www.excelbanter.com/excel-programming/366255-how-can-vba-detect-what-currently-selected.html)

Tim Marsh

how can vba detect what is currently selected?
 
hi,

i have a userform which i use to create the formulae for dynamic ranges.
when it initializes, it bases itself on the currently selected cell, which
works fine. however, i noticed the other day that if some other object is
selected (in this case, a chart) the VBA returns an error.

how can i determine what sort of thing is selected when the form is
initialized? the user form uses a RefEdit control (I know, I know!), so i
wouldn't really want the form to close, as the user would have the chance to
select a range whilst the form is still open.

tia,

Tim



Robert Bruce[_2_]

how can vba detect what is currently selected?
 
Roedd <<Tim Marsh wedi ysgrifennu:

hi,

i have a userform which i use to create the formulae for dynamic
ranges. when it initializes, it bases itself on the currently
selected cell, which works fine. however, i noticed the other day
that if some other object is selected (in this case, a chart) the VBA
returns an error.

how can i determine what sort of thing is selected when the form is
initialized? the user form uses a RefEdit control (I know, I know!),
so i wouldn't really want the form to close, as the user would have
the chance to select a range whilst the form is still open.


if typeof selection is Excel.Range then
' Load address into refedit
else
' Do whatever
end if

HTH

Rob



Tim Marsh

how can vba detect what is currently selected?
 
Thanks Rob - never used 'typeof' before, but it seems to do the trick
nicely!

cheers

tim

"Robert Bruce" <rob@analytical-dynamicsdotcodotukay wrote in message
...
Roedd <<Tim Marsh wedi ysgrifennu:

hi,

i have a userform which i use to create the formulae for dynamic
ranges. when it initializes, it bases itself on the currently
selected cell, which works fine. however, i noticed the other day
that if some other object is selected (in this case, a chart) the VBA
returns an error.

how can i determine what sort of thing is selected when the form is
initialized? the user form uses a RefEdit control (I know, I know!),
so i wouldn't really want the form to close, as the user would have
the chance to select a range whilst the form is still open.


if typeof selection is Excel.Range then
' Load address into refedit
else
' Do whatever
end if

HTH

Rob






All times are GMT +1. The time now is 05:46 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com