Variables not changing value in Loops

Bill_Biggs

Well-known Member
Joined
Feb 6, 2007
Messages
1,216
I have the following macro in place which creates an ellipse. There are four angles (indicated) where I have to bypass the Function callouts and enter predetermined values. The problem I am having is when the loop gets to the second angle and the fourth angle, X simply becomes 0 and Y does not change its value. But they do on the first and third angle. Can anyone explain that?

<font face=Courier New><SPAN style="color:#00007F">Sub</SPAN> Ellipse()<br><SPAN style="color:#00007F">Dim</SPAN> Ang <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Double</SPAN><br><SPAN style="color:#00007F">Dim</SPAN> a <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Double</SPAN>, b <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Double</SPAN>, z <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Double</SPAN>, X <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Double</SPAN>, Y <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Double</SPAN><br><SPAN style="color:#00007F">Dim</SPAN> j, R, S, C, G <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Integer</SPAN><br><br>G = 1<br><br>a = 277 <SPAN style="color:#007F00">'Width</SPAN><br>b = 157 <SPAN style="color:#007F00">'Hieght</SPAN><br><br>R = 337.5 <SPAN style="color:#007F00">'-->  Center along X</SPAN><br>S = 275 <SPAN style="color:#007F00">'V  Center along Y</SPAN><br><br><SPAN style="color:#00007F">For</SPAN> j = 1 <SPAN style="color:#00007F">To</SPAN> 360<br><br>Ang = j<br><br><SPAN style="color:#00007F">If</SPAN> Ang = 90 <SPAN style="color:#00007F">Then</SPAN><br>X = 0 And Y = S <SPAN style="color:#007F00">'1st Angle</SPAN><br><SPAN style="color:#00007F">GoTo</SPAN> Alpha<br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br><br><SPAN style="color:#00007F">If</SPAN> Ang = 180 <SPAN style="color:#00007F">Then</SPAN><br>X = R - a And Y = 0  <SPAN style="color:#007F00">'2nd Angle</SPAN><br><SPAN style="color:#00007F">GoTo</SPAN> Alpha<br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br><br><SPAN style="color:#00007F">If</SPAN> Ang = 270 <SPAN style="color:#00007F">Then</SPAN><br>X = 0 And Y = -S <SPAN style="color:#007F00">'3rd Angle</SPAN><br><SPAN style="color:#00007F">GoTo</SPAN> Alpha<br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br><br><SPAN style="color:#00007F">If</SPAN> Ang = 360 <SPAN style="color:#00007F">Then</SPAN><br>X = R + a And Y = 0 <SPAN style="color:#007F00">'4th Angle</SPAN><br><SPAN style="color:#00007F">GoTo</SPAN> Alpha<br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br> <br>X = MyFunc(a, b, Ang)<br>Y = MyY(X, Ang)<br><br><SPAN style="color:#00007F">If</SPAN> Ang >= 90 And Ang <= 179 <SPAN style="color:#00007F">Then</SPAN><br>X = X * -1<br>Y = Y * -1<br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br><br><SPAN style="color:#00007F">If</SPAN> Ang >= 180 And Ang <= 269 <SPAN style="color:#00007F">Then</SPAN><br>X = X * -1<br>Y = Y * -1<br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br><br><SPAN style="color:#00007F">If</SPAN> Ang >= 270 And Ang <= 360 <SPAN style="color:#00007F">Then</SPAN><br>X = X * 1<br>Y = Y * 1<br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br><br>Alpha:<br>    ActiveSheet.Shapes.AddShape(msoShapeOval, R + X, S - Y, 2, 2).Select<br><br><SPAN style="color:#00007F">Next</SPAN> j<br><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br></FONT>

And if anyone can suggest a way around the small bit of spaghetti code included, I am all ears. :)biggrin:)
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Hi Bill.

If

Code:
X = 0 And Y = S '1st Angle

is meant to set 2 variables in one line of code you actually need

Code:
X = 0: Y = S '1st Angle
 
Upvote 0
Try
Rich (BB code):
Sub Ellipse()
    Dim a As Double, b As Double, z As Double, X As Double, Y As Double
    Dim Ang As Double, j, R, S, C, G As Integer
    G = 1
    a = 277    'Width
    b = 157    'Hieght

    R = 337.5    '-->  Center along X
    S = 275    'V  Center along Y

    Application.ScreenUpdating = False
    For j = 1 To 360
        Ang = j
        Select Case Ang
            Case 90: X = 0: Y = S    '1st Angle
            Case 180: X = R - a: Y = 0    '2nd Angle
            Case 270: X = 0: Y = -S    '3rd Angle
            Case 360: X = R + a: Y = 0    '4th Angle
            Case Else:
                X = MyFunc(a, b, Ang): Y = MyY(X, Ang)
                If Ang >= 90 And Ang <= 179 Then X = X * -1: Y = Y * -1
                If Ang >= 180 And Ang <= 269 Then X = X * -1: Y = Y * -1
                If Ang >= 270 And Ang <= 360 Then X = X * 1: Y = Y * 1
        End Select
        ActiveSheet.Shapes.AddShape(msoShapeOval, R + X, S - Y, 2, 2).Select
    Next j
End Sub
 
Upvote 0
spa`sibo Master Yoda, and amazing! And Peter, it is always an honor to have you visit my little world of confusion. I will of course employ this, but why did the values change in the first and third angle anyway? Just wondering.
 
Upvote 0

Forum statistics

Threads
1,215,740
Messages
6,126,585
Members
449,319
Latest member
iaincmac

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