VBA - Copy/Paste Shape - Issue

Spaztic

New Member
Joined
Jul 27, 2023
Messages
48
Office Version
  1. 365
Platform
  1. Windows
Hoping there is an easy solution. I want the shape (arrow) to be copied into any cell that gets selected.
  • Selected cell B2
  • Click on the arrow in B1 (macro)
  • Code is run to copy the arrow in B1 and place in B2 (the selected cell)
1716739390283.png


This seems to work ok UNLESS I select a cell that contains a dropdown BEFORE I select B2. Clicking Debug shows .OnAction = "" is an issue in my code
1716739088743.png


I noticed that in the selection pane, when I click from A2 (dropdown cell) to B2...it adds 'Drop Down 20'. This seems to be an issue. I can click SAVE and this 'Drop Down 20' goes away and everything works again.
1716739176018.png


Is there anything that can be added/modified in the code below so that 'Drop Down 20' (or whatever cell with a drop down was selected at some point) goes away when clicking on the 'arrow' (macro)?

VBA Code:
Sub Arrow()
    Dim Sh As Shape
  
    With ActiveSheet
        ActiveCell.Activate
   
        .Shapes("Arrow").Copy
        .Paste 'inital position at selected cell
        Set Sh = .Shapes(.Shapes.Count) 'the newest shape
    End With
     
    With Sh
        Sh.Name = "Arrow2"
                      
        .OnAction = ""
       
    End With
End Sub
 

Attachments

  • 1716738603789.png
    1716738603789.png
    2.8 KB · Views: 10
  • 1716738634472.png
    1716738634472.png
    3 KB · Views: 14

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
OR,
If anyone knows any (different) code that would work, I am all ears.
 
Upvote 0
I cannot reproduct it consistently, but somehow your drop down (which contains a type 8 shape) get's added as the last element of the shapes collection so that this statement

VBA Code:
 Set Sh = .Shapes(.Shapes.Count)               'the newest shape

Gets assigned to the drop down instead of the new arrow. This workaround might be an alternative:

VBA Code:
Sub Arrow()
    Dim Sh As Shape, ShSrc As Shape
    Dim I As Long

    With ActiveSheet
        Set ShSrc = .Shapes("Arrow")
        ShSrc.Copy
        .Paste                                        'inital position at selected cell

        Set Sh = .Shapes(.Shapes.Count)               'the newest shape

        If Sh.Type <> ShSrc.Type Then                 'fallback logic for when last element is not an arrow
            For I = .Shapes.Count To 1 Step -1
                Set Sh = .Shapes(I)
                If Sh.Type = ShSrc.Type Then
                    If (Sh.Name = ShSrc.Name) And (Sh.Top + Sh.Left) <> (ShSrc.Top + ShSrc.Left) Then
                        Exit For
                    End If
                End If
                Set Sh = Nothing
            Next I
        End If
    End With

    If Not Sh Is Nothing Then
        With Sh
            Sh.Name = "Arrow2"
            .OnAction = ""
        End With
    End If
End Sub
 
Last edited:
Upvote 0
Solution
You could also delete this line. It's redundant.

VBA Code:
ActiveCell.Activate

ActiveCell is by definition already active.
 
Upvote 0

Forum statistics

Threads
1,223,884
Messages
6,175,177
Members
452,615
Latest member
bogeys2birdies

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top