Question

Whats wrong with this VBA? Sub problem2() Dim x0 As Integer, v0 As Integer, theta As...

Whats wrong with this VBA?

Sub problem2()
Dim x0 As Integer, v0 As Integer, theta As Integer, g As Integer, t As Double, y0 As Integer
Dim x As Double, y As Double
Sheets("Sheet2").Select
x0 = Range("F2").Value
y0 = Range("F3").Value
v0 = Range("F4").Value
theta = Range("F5").Value
g = -9.81
x = x0 + v0 * Cos(theta * (3.14159 / 180)) * t

y = y0 + v0 * Sin(theta * (3.14159 / 180)) * t + 0.5 * g * t ^ 2
Cells(2, 1) = 0
Cells(2, 2) = x
Cells(2, 3) = y

Do While (y > 0)
y = y0 + v0 * Sin(theta * (3.14159 / 180)) * t + (0.5 * g * t ^ 2)
t = t + 0.1
ThisWorkbook.Worksheets("Sheet2").Range("A") = t
ThisWorkbook.Worksheets("Sheet2").Range("C") = y

If y > 0 Then
x = x0 + v0 * Cos(theta * (3.14159 / 180)) * t
ThisWorkbook.Worksheets("Sheet2").Range("B") = x

End If

Loop

End Sub

Homework Answers

Answer #1

Please check that you have created a sheet 2 in your excel file. I tried this code and it seems to be running fine.

As you have not mentioned on what you wanted to achieve I assumed you had some error in running code which can be resolved by creating a sheet2.

If that is not the case please post full query.

Sub problem2()
Dim x0 As Integer, v0 As Integer, theta As Integer, g As Integer, t As Double, y0 As Integer
Dim x As Double, y As Double
Sheets("Sheet2").Select
x0 = Range("F2").Value
y0 = Range("F3").Value
v0 = Range("F4").Value
theta = Range("F5").Value
g = -9.81
x = x0 + v0 * Cos(theta * (3.14159 / 180)) * t
y = y0 + v0 * Sin(theta * (3.14159 / 180)) * t + 0.5 * g * t ^ 2
Cells(2, 1) = 0
Cells(2, 2) = x
Cells(2, 3) = y
Do While (y > 0)
y = y0 + v0 * Sin(theta * (3.14159 / 180)) * t + (0.5 * g * t ^ 2)
t = t + 0.1
ThisWorkbook.Worksheets("Sheet2").Range("A") = t
ThisWorkbook.Worksheets("Sheet2").Range("C") = y
If y > 0 Then
x = x0 + v0 * Cos(theta * (3.14159 / 180)) * t
ThisWorkbook.Worksheets("Sheet2").Range("B") = x
End If
Loop
End Sub

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Whats wrong with my vba? Option Explicit Sub problem2() Sheet("Sheet2").Select Dim p As Double, V As...
Whats wrong with my vba? Option Explicit Sub problem2() Sheet("Sheet2").Select Dim p As Double, V As Double, d As Double, u As Double Dim rey As Double Range(“B1”).Select p = ActiveCell.Value Range(“B2”).Select V = ActiveCell.Value Range(“B3”).Select d = ActiveCell.Value Range(“B4”).Select u = ActiveCell.Value Function rey(p, V, d, u) Range(“B6”).Value If rey >= 0 And rey <= 2100 Then MsgBox“Laminar Flow” ElseIf rey > 2100 And rey < 4000 Then MsgBox“Transitional Flow” ElseIf rey >= 4000 Then MsgBox“Turbulent Flow” ElseIf rey...
Using MATLAB The range of an object shot at an angle θ (with respect to x-axis),...
Using MATLAB The range of an object shot at an angle θ (with respect to x-axis), with the initial velocity of V0 (in the absence of air resistance), is calculated by the following formula: range=(Vo^2/g)(sin(2theta)) where (0<=theta<=pi/2) And the trajectory of object is given by:     h=tan(theta).x-(g/2Vo^2*cos^2(theta)).x^2 .Where h is the height of the object at each x location and g = 9.81 m/s2. a) Using π/8 increment size for the angle and V0 = 10 m/s, plot the trajectories of...
Homework 3 As you know very well from physics, if you hit a ball forward with...
Homework 3 As you know very well from physics, if you hit a ball forward with a certain angle and a certain speed, it makes an orbital movement. The height of the ball is determined by the equation given below. y(t)=y_0+v_y0 t-1/2 gt^2 Where y0 is the first location of the ball, vy0 is the initial vertical velocity of the ball and g is the gravitational acceleration. After the ball is thrown, the horizontal distance is determined by the following...
Problem 2 (explain each codes please) If more information if needed please let me know 1)...
Problem 2 (explain each codes please) If more information if needed please let me know 1) % time vector t=linspace(0,1,1000); x = zeros(size(t)); y = zeros(size(t)); Meaning: 3)% loop to find the value of x and y for different values of time for i=1:length(t) x(i) = v*cos(angle_theta)*t(i); y(i) = h+v*sin(angle_theta)*t(i)-(0.5*g*(t(i).^2)); end Meaning: 4)% find the first index where y<=0 i.e the ball hits the ground I = find(y<=0,1); fprintf('The ball hits the ground at distance of %f meters\n',x(I)); Meaning: 5)...
VPython Physics 1211k 1. Here is a somewhat more sophisticated program that recreates one of your...
VPython Physics 1211k 1. Here is a somewhat more sophisticated program that recreates one of your experiments – a cart rolling up a ramp and back down. Particularly pay attention to the components of the acceleration. from visual import * #control the dimensions of the animation window. Turn off autoscaling. Then set the window to show 20 meters #in each direction. Then set the actual physical size of the animation window on the screen scene.autoscale = False scene.range = 20...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT