+ Reply to Thread
Results 1 to 4 of 4

Translate VB.net code into VBA

Hybrid View

  1. #1
    Registered User
    Join Date
    01-31-2012
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    7

    Translate VB.net code into VBA

    hello everyone,

    please need a big big favor

    I'm just starting with VBA and VB language and i have VB code to convert to VBA.
    I'll be very happy if someone could help me with this code.

    Of course I'm also trying to convert the code but because I'm learning it's taking ages
    and i need this code to be done very soon

    thx and regards

    karo

    this is the code:

    Form2
    
    Imports System.Windows.Forms.DataVisualization.Charting
    Imports System.Data.OleDb
    
    Public Class Form2
        Public objDataset1 As New DataSet
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            OpenFileDialog1.Title = "Please Select a File"
            OpenFileDialog1.InitialDirectory = "C:\data"
            OpenFileDialog1.Filter = "CSV files (*.csv)|*.CSV"
            OpenFileDialog1.ShowDialog()
        End Sub
    
        Private Sub OpenFileDialog1_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk
            Dim myFile As String = OpenFileDialog1.SafeFileName.ToString()
            Dim myPath As String = OpenFileDialog1.FileName.ToString()
            objDataset1.Clear()
    
       'upload data from file to table 1    
            Dim sConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & myPath.Substring(0, myPath.Length - myFile.Length) & ";Extended Properties=Text;"
            Dim objConn As New OleDbConnection(sConnectionString)
            objConn.Open()
            Dim objCmdSelect As New OleDbCommand("SELECT * FROM " & myFile, objConn)
            Dim objAdapter1 As New OleDbDataAdapter
            objAdapter1.SelectCommand = objCmdSelect
            objAdapter1.Fill(objDataset1, "test")
            objConn.Close()
    
    'fill in the data into table 1
            DataGridView1.DataSource = objDataset1.Tables(0).DefaultView
    
    'create new table 2 
             objDataset1.Tables.Add()
    
            If objDataset1.Tables(1).Columns.Count = 0 Then
                objDataset1.Tables(1).Columns.Add("Rok")
                objDataset1.Tables(1).Columns.Add("C/WK")
                objDataset1.Tables(1).Columns.Add("C/Z")
                objDataset1.Tables(1).Columns.Add("C/CF")
                objDataset1.Tables(1).Columns.Add("EPS")
                objDataset1.Tables(1).Columns.Add("BVS")
                objDataset1.Tables(1).Columns.Add("STOPA_DYWIDENDY")
            End If
    
    'calculate indicators
                For i = 0 To objDataset1.Tables(0).Rows.Count - 1
                objDataset1.Tables(1).Rows.Add()
                objDataset1.Tables(1).Rows(i)("Rok") = objDataset1.Tables(0).Rows(i)(0)
                objDataset1.Tables(1).Rows(i)("C/WK") = Math.Round(objDataset1.Tables(0).Rows(i)(11) / objDataset1.Tables(0).Rows(i)(14), 4)
                objDataset1.Tables(1).Rows(i)("C/Z") = Math.Round(objDataset1.Tables(0).Rows(i)(11) / objDataset1.Tables(0).Rows(i)(13), 4)
                objDataset1.Tables(1).Rows(i)("C/CF") = Math.Round((objDataset1.Tables(0).Rows(i)(11) /
                                                            (objDataset1.Tables(0).Rows(i)(13) +
                                                            (objDataset1.Tables(0).Rows(i)(3) / objDataset1.Tables(0).Rows(i)(12)))) / 1, 4)
                objDataset1.Tables(1).Rows(i)("EPS") = Math.Round(objDataset1.Tables(0).Rows(i)(6) / objDataset1.Tables(0).Rows(i)(12) / 1000, 4)
                objDataset1.Tables(1).Rows(i)("BVS") = Math.Round(objDataset1.Tables(0).Rows(i)(9) / objDataset1.Tables(0).Rows(i)(12) / 1000, 4)
                objDataset1.Tables(1).Rows(i)("STOPA_DYWIDENDY") = Math.Round(objDataset1.Tables(0).Rows(i)(10) / objDataset1.Tables(0).Rows(i)(12), 4)
            Next
    
    
    'fill in the data into table 2
                    DataGridView2.DataSource = objDataset1.Tables(1).DefaultView
    
    'Set the width of columns in Table 2
                    For i = 0 To objDataset1.Tables(1).Columns.Count - 2
                Dim column As DataGridViewColumn = DataGridView2.Columns(i)
                column.Width = 90
            Next
            Dim lastcolumn As DataGridViewColumn = DataGridView2.Columns(6)
            lastcolumn.Width = 120
    
            Chart1.DataSource = objDataset1.Tables(1).DefaultView
            Chart1.Legends.Clear()
    
        End Sub
    
    'create the graph of indicator 
        Private Sub RadioButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton1.Click
            Chart1.Series.Clear()
            Dim s As New Series
            s.ChartType = SeriesChartType.Line
            s.XValueMember = objDataset1.Tables(1).Columns(0).ToString()
            s.YValueMembers = objDataset1.Tables(1).Columns(1).ToString()
            Chart1.Series.Add(s)
            Chart1.DataBind()
        End Sub
    End Class
    Last edited by karo; 02-01-2012 at 04:02 AM. Reason: tags around code

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    re: Translate VB.net code into VBA

    Hello karo,

    FYI - In addition to the code tags, this code sample is VB.net and Visual Basic. They share a similar name but are nothing alike.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Registered User
    Join Date
    01-31-2012
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    7

    re: Translate VB.net code into VBA

    yes i know now it is VB.NET not VB.
    I also know that i can not open this application on every PC without installation NET 4.0 or Visual Studio.
    however i need to open this application on different pc's so i need to change the code for VBA

    karo

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Translate VB.net code into VBA

    Hello karo,

    My post was to inform other readers about your code. You need people who understand both languages to answer this post. I changed your title to make this more apparent.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1