Results 1 to 5 of 5

Thread: Get Selected Object Name

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1

    Get Selected Object Name

    Hello everyone,

    I'm currently working on a smaller project in VBA. To set up an "Edit" function I need to pass the name of a selected object. So far the output works great for the name of cells. But when I select my (grouped) shape there is no output. Although the name of the object is displayed in the small window at the top left. How could I solve this problem?

    To explain: The application is a project management tool. Operations planning takes place on a kind of Kanban chart. A form (grouped) is generated for each project and named with a unique continuous ID. If I now click on one of the Kanban cards I have to be able to pass the name of the object in order to set up an "edit" function. So that the form that opens knows which line it should pull its data from and where it should then write the changes. Right now it only outputs a "no object selected" message if i click a merged cell and the "Selected Object Name: " & selectedObjectName when I click on a cell. however not if i click on the kanban card.

    Here is the code. This will then be triggered later in the sheet using the function below when the selection is changed:

    Sub GetSelectedObjectName()
        Dim selectedObjectName As String
        Dim obj As Object
        ' Check if something is selected
        If Not Selection Is Nothing Then
            If Selection.Count = 1 Then
                ' If a single object is selected, determine its type
                Set obj = Selection
                If TypeOf obj Is Range Then
                    ' If it's a cell or range
                    selectedObjectName = obj.Address
                ElseIf TypeOf obj Is shape Then
                    ' If it's an individual shape
                        selectedObjectName = obj.Name
                    ElseIf TypeOf obj Is ChartObject Then
                        ' If it's a chart
                            selectedObjectName = obj.Name
                        End If
                    ElseIf Selection.Count > 1 Then
                        ' Check if a grouped shape is selected
                        If TypeName(Selection(1)) = "GroupObject" Then
                            ' If it's a grouped shape
                                selectedObjectName = "Grouped Shape"
                            End If
                        End If
                    End If
                    ' Display the name in a message box
                    If selectedObjectName <> "" Then
                        MsgBox "Selected Object Name: " & selectedObjectName
                    Else
                        MsgBox "No object selected."
                    End If
    End Sub
    
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
        Call GetSelectedObjectName
    End Sub
    Last edited by Aussiebear; 04-20-2025 at 04:12 PM. Reason: Added code tags to supplied code

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •