+ Reply to Thread
Results 1 to 5 of 5

Excel Shape Rotation with VBA Code

Hybrid View

savetrees Excel Shape Rotation with VBA... 11-06-2014, 11:32 AM
Norie Re: Excel Shape Rotation with... 11-06-2014, 11:41 AM
savetrees Re: Excel Shape Rotation with... 11-06-2014, 11:53 AM
Norie Re: Excel Shape Rotation with... 11-06-2014, 12:08 PM
savetrees Re: Excel Shape Rotation with... 11-06-2014, 12:11 PM
  1. #1
    Registered User
    Join Date
    07-15-2014
    Location
    Netherlands
    MS-Off Ver
    2010
    Posts
    66

    Excel Shape Rotation with VBA Code

    Hi,

    The below code works perfectly to rotate my rectangle shape. However, i have multiple rectangle shapes, and i want to fit all those shape names into this code, please advise.

    Sub Rotate()
      
      ' Minimum & maximum angles in degrees
      Const MinAngle& = 51, MaxAngle& = 366
      
      ' Define the rotation ratio from 0 up to 1
      Dim phi&, Ratio#, t!
      
      ' Rotate shape
      With Sheets("Mobile").Shapes("Rectangle 1")
          
        ' Rotate clockwise
        For Ratio = 0.5 To 1 Step 0.02
          ' Calc the rotation angle in degrees
          phi = MinAngle + (MaxAngle - MinAngle) * Ratio
          ' Rotate shape
          .Rotation = phi
          ' Make pause
          t = Timer + 0.01: While Timer < t: DoEvents: Wend
        Next
        
      End With
      
    End Sub

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,646

    Re: Excel Shape Rotation with VBA Code

    You could add an array with the names and loop through it.
    Dim arrShapeNames As Variant
    Dim I As Long
    
        arrShpNames = Array("Rectangle 1", "Rectangle 2", "Rectangle 3")
    
        For I = LBound(arrShpNames) To UBound(arrShpNames)
            With Sheets("Mobile").Shapes(arrShpNames(I))
          
                ' Rotate clockwise
                For Ratio = 0.5 To 1 Step 0.02
                    ' Calc the rotation angle in degrees
                    phi = MinAngle + (MaxAngle - MinAngle) * Ratio
                    ' Rotate shape
                    .Rotation = phi
                    ' Make pause
                    t = Timer + 0.01: While Timer < t: DoEvents: Wend
                Next Ratio
        
            End With
        Next I
    If posting code please use code tags, see here.

  3. #3
    Registered User
    Join Date
    07-15-2014
    Location
    Netherlands
    MS-Off Ver
    2010
    Posts
    66

    Re: Excel Shape Rotation with VBA Code

    Thanks Norie,

    It works fine, i just made a small tweak. However, i would like to have this shape rotate on each shape click. Please advise

    Sub Rotate1()
    
    Dim arrShapeNames As Variant
    Dim I As Long
    
    Const MinAngle& = 51, MaxAngle& = 366
    
    Dim phi&, Ratio#, t!
    
        arrShpNames = Array("Rectangle 1", "Rectangle 2", "Rectangle 3", "Rectangle 4")
    
        For I = LBound(arrShpNames) To UBound(arrShpNames)
            With Sheets("Mobile").Shapes(arrShpNames(I))
          
                ' Rotate clockwise
                For Ratio = 0.5 To 1 Step 0.02
                    ' Calc the rotation angle in degrees
                    phi = MinAngle + (MaxAngle - MinAngle) * Ratio
                    ' Rotate shape
                    .Rotation = phi
                    ' Make pause
                    t = Timer + 0.01: While Timer < t: DoEvents: Wend
                Next Ratio
        
            End With
        Next I
    End Sub

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,646

    Re: Excel Shape Rotation with VBA Code

    Try assigning this macro to any shape on sheet 'Mobile' you want to rotate when clicked.
    Sub Rotate()
      
      ' Minimum & maximum angles in degrees
      Const MinAngle& = 51, MaxAngle& = 366
      
      ' Define the rotation ratio from 0 up to 1
      Dim phi&, Ratio#, t!
      
      ' Rotate shape
      With Sheets("Mobile").Shapes(Application.Caller)
          
        ' Rotate clockwise
        For Ratio = 0.5 To 1 Step 0.02
          ' Calc the rotation angle in degrees
          phi = MinAngle + (MaxAngle - MinAngle) * Ratio
          ' Rotate shape
          .Rotation = phi
          ' Make pause
          t = Timer + 0.01: While Timer < t: DoEvents: Wend
        Next
        
      End With
      
    End Sub

  5. #5
    Registered User
    Join Date
    07-15-2014
    Location
    Netherlands
    MS-Off Ver
    2010
    Posts
    66

    Re: Excel Shape Rotation with VBA Code

    Genius .. works perfectly. Thanks Norie.

+ 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. Automatically bringing a Shape into a Cell Using Shape Code
    By dineshtendulkar in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 12-16-2016, 02:25 PM
  2. Excel VBA code to automatically change colour shape based on time
    By afzal_u1 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 11-28-2013, 10:28 AM
  3. Replies: 1
    Last Post: 10-19-2013, 08:11 PM
  4. [SOLVED] Change Shape Rotation
    By Jakobshavn in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-01-2013, 01:58 PM
  5. 3D chart rotation buttons Excel 2007
    By Guru Meditation in forum Excel General
    Replies: 3
    Last Post: 01-05-2012, 10:22 AM

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