Consulting

Results 1 to 2 of 2

Thread: Get Selected Object Name

  1. #1

    Question 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; 11-01-2023 at 05:08 PM. Reason: Added code tags to supplied code

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,738
    Location
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

Posting Permissions

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