#1




VBA to change shape rotation
I have a simple test and calculation.
Two variables: * A2: Ship direction =rand()*360) * B2: Wind direction =rand()*360) One calculation: * C2: Apparent wind direction (relative to the ship) =B2A2+IF(A2>B2,360,0) One button activating a macro that recalculates the ship and wind directions. Pretty simple, but I'd like to fancy it up. I've put in three arrow shapes, one each for the ship, wind, and apparent directions. I'd like the macro to change the rotation of these based on the recalculated results. I'm so rusty in VBA. Any help would be appreciated. 
#2




You need to make some objects to achieve this. The Set word will do this after you dim it. The code below will do what you are looking for.
Code:
Sub Button1_Click() Dim ws As Worksheet, AngleShape(1 To 3) As Shape, x As Byte 'Set up the references Set ws = ThisWorkbook.ActiveSheet Set AngleShape(1) = ws.Shapes("Ship") Set AngleShape(2) = ws.Shapes("Wind") Set AngleShape(3) = ws.Shapes("Apparent") 'Perform the calculation Calculate 'Perform the rotation for the above shapes For x = 1 To 3 AngleShape(x).Rotation = ws.Cells(2, x) Next x End Sub Code:
Set AngleShape = (ws.Shapes("Ship"), ws.Shapes("Wind"), ws.Shapes("Apparent")) Code:
Set AngleShape = Array(ws.Shapes("Ship"), ws.Shapes("Wind"), ws.Shapes("Apparent")) Code:
Set AngleShape() = Array(ws.Shapes("Ship"), ws.Shapes("Wind"), ws.Shapes("Apparent")) Thanks Last edited by excelledsoftware; 09052014 at 12:48 PM. Reason: forgot content 
#3




Thanks. We forgot to account for the original 90 deg rotation but I took care of that easily. Nice clean code is easy to debug.
AngleShape(x).Rotation = ws.Cells(2, x)  90 Mille grazie!! 
#4




Glad it worked. I just noticed that I usually put
Code:
ws.Cells(2, x).Value  90 
#5




This was for a bet I had with my brother. He bet me $25 bucks I couldn't come up with a simpler solution than his or find out where his solution failed. On that second part, I gave up. Look at what his equation was:
=IF(A2<IF(A2+180>360,A2+180360,A2+180),IF(AND(B2>A2,K2<IF(A2+180>360,A2+180360,A2+180)),O2,O2),IF(OR(B2>A2,B2<IF(A2+180>360,A2+180360,A2+180)),O2,O2)) Even if it's right, it's wrong. A kludge is a kludge. A and B are the same, but what the heck is O? LOL...stupid is what stupid does. The above calculates the apparent wind direction on the ship if the ship is motionless. The next step of complexity is to then add speeds to both the wind and the ship. That requires some vector calculus. But I'm not doing anything until he posts the terms. 
Thread Tools  
Display Modes  

Similar Threads  
Thread  Thread Starter  Forum  Replies  Last Post 
Change accent theme color used as shape fill (not RGBs)  preetidb  PowerPoint  4  12302013 03:22 PM 
color change for same shape  ajkiran  PowerPoint  1  11102012 07:41 AM 
enter free rotation  excelledsoftware  PowerPoint  3  03012012 06:59 PM 
Rotation Animation?  shinyjellicent12321  PowerPoint  1  02192012 11:43 AM 
Motion Paths with Rotation  PBone  PowerPoint  4  01132011 11:59 AM 