Activate Objects: Get Names and Values
You can use the Activate method to set focus on the following objects:
Chart
|
ChartObject
|
OLEObject
|
Pane
|
Range
|
Window
|
Workbook
|
Worksheet
|
Usually, you’ll use Activate in combination with one of the collection methods to set focus on a member of the collection. For example, the following line sets focus on the first sheet in a workbook:
Sheets(1).Activate
Similarly, the following line sets focus on the last sheet:
Sheets(Sheets.Count).Activate
As mentioned previously, not all objects have a Name property. In those cases, the Caption or Address property is sometimes equivalent. The following procedure uses exception handling to return the name of an object of any type:
Function GetName(obj) As String
Dim res As String
' Use exception handling in case object
' doesn't support Name property
.
On Error Resume Next
res = obj.Name
If Err Then res = obj.Address
If Err Then res = obj.Caption
If Err Then res = obj.Index
If Err Then res = "no name"
On Error GoTo 0
GetName = res
End FunctionSome objects (such as points within a chart series) don’t have any identifiers. In that case, GetName returns no name. The Range and Hyperlink objects use the Address property, and the following objects have Caption
properties:
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access