Visual basic 2005 and excel

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • na00007
    New Member
    • Nov 2006
    • 5

    #1

    Visual basic 2005 and excel

    Hallo,

    I have a problem, I do not know how to convert some data from my visual basic 2005 program into a excel file. Can you please tell me what to do?
  • willakawill
    Top Contributor
    • Oct 2006
    • 1646

    #2
    Hi. The first thing to do is post the code you have already and point out where it is that you need help

    Comment

    • na00007
      New Member
      • Nov 2006
      • 5

      #3
      Originally posted by willakawill
      Hi. The first thing to do is post the code you have already and point out where it is that you need help

      Code:
      Public Class Form1
          Dim g As Double = -9.81  'Gravity
      
          Function Find_Verical_Velocity(ByVal Vy As Double, ByVal mass As Double, ByVal drag_y As Double) As Double
      
              If Vy > 0 Then
                  Vy = Vy + ((g - drag_y / mass) * 0.01) 'The equation that gives the velocity is V=Vo-gt
              Else
                  Vy = Vy + ((g + drag_y / mass) * 0.01)
              End If
      
      
              Return (Vy) 'Returns the vertical velocity
      
          End Function
          Function Find_Horizontal_Velocity(ByVal Vx As Double, ByVal mass As Double, ByVal drag_x As Double) As Double
      
              Vx = Vx + (-(drag_x / mass) * 0.01)
      
              Return (Vx)
      
          End Function
      
          Function Find_Distance(ByVal Vx As Double) As Double
      
              Static Distance As Double = 0
      
              Distance = Distance + (Vx * 0.01)
      
              Return (Distance)
      
          End Function
      
          Function Find_Height(ByVal Vy As Double) As Double
              Static Height As Double = 0
      
              Height = Height + (Vy * 0.01)
      
              Return (Height)
      
          End Function
          Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
      
              Dim mass As Double = CInt(TextBox1.Text)
              Dim angle As Double = CInt(TextBox2.Text)
              Dim initial_velocity As Double = CInt(TextBox3.Text) 'Given initial velocity
              Dim density As Double = CInt(TextBox4.Text)
              Dim Height As Double = 0.0
              Dim time As Double = 0.0
              Dim Distance As Double = 0.0
              Dim Vx As Double = 0.0 'Horizontal velocity component
              Dim Vy As Double = 0.0 'Vertical velocity  component
              Dim rad As Double
              Dim new_velocity As Double
              Dim Cd As Double = 0.47 'This is the drag coefficient and we assume that is constant
              Dim A As Double = Math.PI * 0.05 ^ 2 'This is the cross-sectional area of the ball
              Dim drag As Double
              Dim drag_x As Double 'The horizontal drag component
              Dim drag_y As Double 'The vertical drag component
      
              Dim ITEMS As ListViewItem
      
              If angle > 180 Then
                  MsgBox("PLEASE ENTER A SMALLER ANGLE")
      
              ElseIf angle < 0 Then
                  MsgBox("PLEASE ENTER A POSITIVE ANGLE")
      
              ElseIf mass < 0 Then
                  MsgBox("PLEASE ENTER A POSITIVE MASS")
              Else
      
      
      
                  rad = (angle / 180) * Math.PI  'Calculate the angle in rads
                  Vx = initial_velocity * Math.Cos(rad)
                  Vy = initial_velocity * Math.Sin(rad)
      
      
                  Do
      
                      time = time + 0.01
      
      
                      Vy = Find_Verical_Velocity(Vy, mass, drag_y)
                      Vx = Find_Horizontal_Velocity(Vx, mass, drag_x)
      
                      rad = Math.Atan(Vy / Vx) 'Because the angle changes during the flihgt it calculates the angle in different points
                      new_velocity = Math.Sqrt((Vy ^ 2) + (Vx ^ 2))
      
      
                      drag = 0.5 * Cd * density * new_velocity ^ 2 * A
      
                      drag_x = drag * Math.Cos(rad)
                      drag_y = drag * Math.Sin(rad)
      
      
                      angle = (rad * 180) / Math.PI
      
                      Distance = Find_Distance(Vx)
      
                      Height = Find_Height(Vy)
      
                      ITEMS = New ListViewItem(Math.Round(time, 4))
                      ITEMS.SubItems.Add(Math.Round(Distance, 4))
                      ITEMS.SubItems.Add(Math.Round(Height, 4))
                      ITEMS.SubItems.Add(Math.Round(angle, 4))
                      ITEMS.SubItems.Add(Math.Round(new_velocity, 4))
                      ITEMS.SubItems.Add(Math.Round(Vy, 4))
                      ITEMS.SubItems.Add(Math.Round(Vx, 4))
                      ListView1.Items.Add(ITEMS)
      
                  Loop Until (Height < 0)
      
              End If
          End Sub
      End Class
      Hallo, in the above program we put as inputs the angle, the initial velocity, the mass and the density of a rocket, and then it calculates the distance, the time the height, the velocity and the initial velocity of the rocket until it comes down to the ground. I would like to import the results of the distance and the height into the excel in order to draw a graph.

      Please help me.

      Thank you
      Last edited by willakawill; Feb 25 '07, 06:29 AM. Reason: please use code tags when posting code

      Comment

      • willakawill
        Top Contributor
        • Oct 2006
        • 1646

        #4
        Ok. The simplest way that I know how to do this is to create an excel file with graph and named ranges for the data cells. Add excel as a reference in your project and use ole automation to open the graph and change the data in the named ranges.

        There is a vba tutorial here that should help you get started

        Comment

        • na00007
          New Member
          • Nov 2006
          • 5

          #5
          Originally posted by willakawill
          Ok. The simplest way that I know how to do this is to create an excel file with graph and named ranges for the data cells. Add excel as a reference in your project and use ole automation to open the graph and change the data in the named ranges.

          There is a vba tutorial here that should help you get started

          Thank You very much

          Comment

          • willakawill
            Top Contributor
            • Oct 2006
            • 1646

            #6
            Originally posted by na00007
            Thank You very much
            You are very welcome. Good luck

            Comment

            Working...