Home |
Search |
Today's Posts |
#1
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
I'm not sure if I should be using a func or a sub, but it looks like my
values get passed from one sub to another, and all of the values are OK at the end of the called sub, but my variables "x" and "v" are being reset to 0 when the procedure continues after I call the 2nd sub. My other values are OK. If you want to try this out in Excel, set some values in B1:B4 and B6:B7. I have put in a couple of msgBox's just to see what values the variables hold. Those will be commented out when this is working. Option Explicit ' Sub Worksheet_Change(ByVal Target As Range) ' this will trigger if any part of the sheet is changed Sub One_D_Motion() Dim t As Single Dim t0 As Single Dim time1End As Single Dim time2End As Single Dim x As Single Dim x0 As Integer Dim v0 As Single Dim v As Single Dim acc As Single Dim acc1 As Single Dim acc2 As Single Dim row As Integer ' This will find the position and velcoity for the first 20 seconds ' The user must set the initial conditions and the time period for the accelerations ' Written 6/12/2008 by Jeff Ciaccio ' Clear out the range where the data will go Range("D2:g202").Value = "" ' Record the values the user has chosen x0 = Range("B1").Value v0 = Range("B2").Value acc1 = Range("b3").Value time1End = Range("b4").Value acc2 = Range("b6").Value time2End = Range("B7").Value ' Set the first row cells t = 0 row = 2 Range("D2").Value = 0 Range("E2").Value = x0 Range("f2").Value = v0 Range("G2").Value = acc1 Range("d3").Select Call calc(t, time1End, row, x0, v0, acc1) ' TROUBLE MsgBox "x = " & x & " v = " & v 'MsgBox prompt:="Time period 1: " & vbLf & "The ending position was " & x & " meters, and the starting position was " & x0 & ", so the displacement was " & (x - x0) & " meters", Title:="End of first period" 'MsgBox prompt:="Time period 1: " & vbLf & "The ending velocity was " & v & " m/s, and the starting velocity was " & v0 & ", so the Delta v was " & (v - v0) & " m/s", Title:="End of first period" 'MsgBox ("Time period 1: " & vbLf & "Since there was constant acceleration, the average velocity is:" & vbLf _ & "avg vel = (" & v0 & " + " & v & ") " & Chr(247) & "2 = " & (v0 + v) / 2) & " m/s", Title:="End of first period" Call calc(t, time2End, row, x, v, acc2) 'MsgBox prompt:="Time period 2: " & vbLf & "NOTE: To find the average velocity for the whole period, you CANNOT simply average the starting and ending velocities!" _ & vbLf & "Just find the displacement and divide by 20.0 seconds." & vbLf _ & "The average velocity was " & (x - x0) / 20 & "m/s^2", Title:="End of simulation" End Sub Sub calc(t, timeEnd, row, x, v, acc) Dim t0 As Single Dim x0 As Integer Dim v0 As Single MsgBox "Thanks for passing me t = " & t & " end time = " & timeEnd & "row = " & row & " x = " & x & " v = " & v & " and acc = " & acc t0 = t x0 = x v0 = v Do Until t = timeEnd t = Round(t + 0.1, 1) row = row + 1 x = x0 + v0 * (t - t0) + 1 / 2 * acc * (t - t0) ^ 2 v = v0 + (t - t0) * acc Range("d" & row).Value = t Range("e" & row).Value = x Range("f" & row).Value = v Range("g" & row).Value = acc Loop MsgBox "x = " & x & " v = " & v & " at the end of calc." End Sub -- Jeff Ciaccio Physics and AP Physics Teacher Sprayberry High School; Marietta, GA Blog: http://sprayberry.typepad.com/ciaccio |
Thread Tools | Search this Thread |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
Changing the range for averages with out changing the formula. | Excel Worksheet Functions | |||
Changing background colour when changing data in a cell | Excel Discussion (Misc queries) | |||
Changing footers on all worksheets without changing print set up | Excel Discussion (Misc queries) | |||
Call Center Management: How to calculate 'cost per call' | Excel Discussion (Misc queries) | |||
changing a cell to changing the link | Excel Worksheet Functions |