+ Reply to Thread
Results 1 to 10 of 10

Event when shape is moved

Hybrid View

  1. #1
    Registered User
    Join Date
    02-24-2015
    Location
    Gent, Belgium
    MS-Off Ver
    2016
    Posts
    2

    Event when shape is moved

    Hi

    Is there anyone who knows a macro event that can be activated
    1. if a shape is selected (to show extra information about the meaning of that shape) BUT ALSO...
    2. if the user changes the sieze of that shape AND (to change the information based on it's new size)
    3. if the user moved that same shape (to change the information based on it's new position)

    I can not add the macro to the shape itself, because then you can not change the format of the shape or move the shape (without right clicking on it).

    Selectionchange or worksheetchange does not work for this.

    Thanks a lot!!!

  2. #2
    Forum Expert
    Join Date
    11-22-2016
    Location
    Cornwall,UK
    MS-Off Ver
    office 365
    Posts
    4,240

    Re: Event when shape is moved

    An active-x object can be used to trigger VBA (when object loses focus, mouse move etc) - what is the shape being used for?

    Why do you not want to use right-click to move and resize?

    What type of shape are you using?
    - attached workbook has a rectangle that can be moved and resized with right-click
    - when user clicks on shape latest information is presented in a message box
    - simple message box
    - T = TOP, = LEFT, W = WIDTH, H = HEIGHT


    .
    Pic1.jpg


    .

    Pic2.jpg

    .

    Sub myShape1_Click()
    Dim strDesc As String, dblH As Double, dblW As Double, dblT As Double, dblL As Double
    
    strDesc = "1 am a blue rectangle"
    With ActiveSheet.Shapes("MyShape1")
        dblH = Format(.Height, "0.00")
        dblW = Format(.Width, "0.00")
        dblT = Format(.Top, "0.00")
        dblL = Format(.Left, "0.00")
    End With
    
    MsgBox strDesc & vbCr & "H= " & dblH & " W= " & dblW & " T= " & dblT & " L= " & dblL
        
    End Sub
    Attached Files Attached Files
    Last edited by kev_; 03-17-2018 at 09:03 PM.
    Click *Add Reputation to thank those who helped you. Ask if anything is not clear

  3. #3
    Forum Expert
    Join Date
    11-22-2016
    Location
    Cornwall,UK
    MS-Off Ver
    office 365
    Posts
    4,240

    Re: Event when shape is moved

    Here the shape click event is used to move the shape (can also use to resize)
    - ie not using right-click

    The user is asked where to move the shape using an input box
    - the shape is then moved to selected cell
    - use a similar technique to ask user to resize the shape

    Sub myShape1_Click()
    Dim strDesc As String, dblH As Double, dblW As Double, dblT As Double, dblL As Double
    Dim cel As Range
    
    Set cel = Application.InputBox("Select where to move shape", , , , , , , 8)
    With ActiveSheet.Shapes("MyShape1")
        .Top = cel.Top
        .Left = cel.Left
    End With
    
    
    strDesc = "1 am a blue rectangle"
    With ActiveSheet.Shapes("MyShape1")
        dblH = Format(.Height, "0.00")
        dblW = Format(.Width, "0.00")
        dblT = Format(.Top, "0.00")
        dblL = Format(.Left, "0.00")
    End With
    
    
    MsgBox strDesc & vbCr & "H= " & dblH & " W= " & dblW & " T= " & dblT & " L= " & dblL
        
    End Sub
    Last edited by kev_; 03-17-2018 at 09:04 PM.

  4. #4
    Registered User
    Join Date
    02-24-2015
    Location
    Gent, Belgium
    MS-Off Ver
    2016
    Posts
    2

    Re: Event when shape is moved

    Hi kev_

    Thanks for your reply!! This kind of thing, I had already

    The question is if there is an event that I can write to do this without the right click (and so without the macro on the shape itself) to make it more user friendly.

    Thanks a lot!

  5. #5
    Forum Expert
    Join Date
    11-22-2016
    Location
    Cornwall,UK
    MS-Off Ver
    office 365
    Posts
    4,240

    Re: Event when shape is moved

    Is this what you want?
    Test in attached workbook

    MoveAndResizeShapeWithoutRightClick.jpg

    .

    Only click events apply to shapes.
    But here is a workaround using an active-x label:
    - label is placed directly above the shape
    - which allows label's MouseMove event to be used
    - could be adapted to deal with many shapes

    Const aShp = "myShape", aInf = "myInfo", aLbl = "myLabel"
    Dim shp As Shape, lbl As Object, inf As Object, ShowInfo As Boolean
    Dim L As Double, T As Double, W As Double, H As Double, TLC As String, BRC As String, myCaption As String
    
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
        On Error Resume Next
        Select Case ShowInfo
            Case True: Call Update: Target.Activate: ShowInfo = False
            Case Else: Me.OLEObjects("myInfo").Visible = False
        End Select
    End Sub
    
    Private Sub myInfo_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
        Me.OLEObjects("myInfo").Visible = False
    End Sub
    
    Private Sub myLabel_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
        Call Update
        shp.Select
        lbl.Height = 0:   lbl.Width = 0     'temporary make label very small
    End Sub
    Sub Update()
        Set shp = Me.Shapes(aShp)
        Set lbl = Me.OLEObjects(aLbl)
        Set inf = Me.OLEObjects(aInf)
        Call ReAlign
        inf.Visible = True
        ShowInfo = True
    End Sub
    Private Sub ReAlign()
    'shape details
        With shp
            L = .Left: T = .Top: H = .Height: W = .Width
            TLC = .TopLeftCell.Address(0, 0): BRC = .BottomRightCell.Address(0, 0)
        End With
    'label position
        With lbl
            .Left = L: .Top = T: .Height = H:   .Width = W
        End With
    'info position
        With inf
            .Left = L + W + 10: .Top = T
        End With
    'amend caption
        myCaption = "Name =  " & shp.Name & vbCr & "Cell Ref = " & TLC & vbCr & Format(H, "0.0") & " (h  X  w) " & Format(W, "0.0")
        inf.Object.Caption = myCaption
    End Sub
    Attached Files Attached Files
    Last edited by kev_; 03-18-2018 at 02:20 PM.

  6. #6
    Forum Expert
    Join Date
    11-22-2016
    Location
    Cornwall,UK
    MS-Off Ver
    office 365
    Posts
    4,240

    Re: Event when shape is moved

    1. if shape selected (to show extra information about the meaning of shape) - YES
    2. if the user changes the sieze of that shape AND (to change the information based on it's new size) - YES
    3. if the user moved that same shape (to change the information based on it's new position) - YES
    .

    Similar (and simpler!) without using the label
    - uses shape click event to achieve the same thing

    Test in attached workbook by clicking on the shape
    - after moving/resizing click on any cell

    In Standard module:
    Public Const aShp = "myShape", aInf = "myInfo"
    Public shp As Shape, inf As Object, ShowInfo As Boolean, ws As Worksheet
    Public L As Double, T As Double, W As Double, H As Double, TLC As String, BRC As String, myCaption As String
    
    Sub myShape_Click()
        Call Update
        shp.Select
    End Sub
    
    Private Sub Update()
        Set ws = ActiveSheet
        Set shp = ws.Shapes(aShp)
        Set inf = ws.OLEObjects(aInf)
        Call ReAlign
        inf.Visible = True
        ShowInfo = True
    End Sub
    
    Private Sub ReAlign()
    'shape details
        With shp
            L = .Left: T = .Top: H = .Height: W = .Width
            TLC = .TopLeftCell.Address(0, 0): BRC = .BottomRightCell.Address(0, 0)
        End With
    'info position
        With inf
            .Left = L + W + 10: .Top = T
        End With
    'amend caption
        myCaption = "Name =  " & shp.Name & vbCr & "Cell Ref = " & TLC & vbCr & Format(H, "0.0") & " (h  X  w) " & Format(W, "0.0")
        inf.Object.Caption = myCaption
    End Sub
    In Sheet module
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
        On Error Resume Next
        Select Case ShowInfo
            Case True: Application.Run "Module1.Update": Target.Activate: ShowInfo = False
            Case Else: Me.OLEObjects("myInfo").Visible = False
        End Select
    End Sub
    
    Private Sub myInfo_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
        Me.OLEObjects("myInfo").Visible = False
    End Sub
    Attached Files Attached Files
    Last edited by kev_; 03-19-2018 at 04:23 AM.

  7. #7
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Event when shape is moved

    Hi,

    There is an extremely complicated way to do this- https://www.mrexcel.com/forum/excel-...udo-event.html
    Don
    Please remember to mark your thread 'Solved' when appropriate.

  8. #8
    Forum Expert
    Join Date
    11-22-2016
    Location
    Cornwall,UK
    MS-Off Ver
    office 365
    Posts
    4,240

    Re: Event when shape is moved

    @xlnitwit

  9. #9
    Forum Guru sktneer's Avatar
    Join Date
    04-30-2011
    Location
    Kanpur, India
    MS-Off Ver
    Office 365
    Posts
    9,655

    Re: Event when shape is moved

    Quote Originally Posted by kev_ View Post
    @xlnitwit
    I liked your expression.


    Quote Originally Posted by xlnitwit View Post
    @kev_ Certainly not for the faint of heart.
    I liked your reply. Lol
    Regards
    sktneer


    Treat people the way you want to be treated. Talk to people the way you want to be talked to.
    Respect is earned NOT given.

  10. #10
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Event when shape is moved

    @kev_ Certainly not for the faint of heart.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Form Click Event firing unexpectedly when a sheet is copied/moved into workbook
    By neilshaw3 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 07-26-2016, 08:03 AM
  2. [SOLVED] How to run macro everytime a shape is moved
    By Awni in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-14-2016, 01:57 AM
  3. Trapping a Shape Change Event
    By yosemitejohn in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 04-25-2012, 04:45 AM
  4. [SOLVED] Event to trigger when a cell is moved
    By Marek in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-10-2006, 01:10 PM
  5. Trapping Add Shape Event
    By maso2000 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-15-2006, 10:11 AM
  6. [SOLVED] Worksheet Change event code moved to Worksheet Calculate event... and it's not working
    By KimberlyC in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 06-23-2005, 06:05 PM
  7. Shape Selection Event !!
    By RAFAAJ2000 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-29-2005, 04:05 PM

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1