Consulting

Results 1 to 5 of 5

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,796
    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

  3. #3
    VBAX Newbie
    Joined
    Sep 2024
    Posts
    2
    Location
    What adjustments can be made to the VBA code to ensure that the names of grouped shapes are correctly identified and passed to the "Edit" function when selected on the Kanban chart?

  4. #4
    Administrator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,258
    Location
    Hi Keanuna,

    I have removed the spam from your signature but if you add back again you will be banned.
    Click here for a guide on how to add code tags
    Click here for a guide on how to mark a thread as solved
    Click here for a guide on how to upload a file with your post

    Excel 365, Version 2408, Build 17928.20080

  5. #5
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,931
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

Posting Permissions

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