Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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

ID: 3794309 • Letter: W

Question

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

Explanation / Answer

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

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote