Looping the Array to be used in data point in Chart

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cten
    New Member
    • Mar 2014
    • 4

    Looping the Array to be used in data point in Chart

    I'm fairly new to VB.net, and I want to create a program that can display 3 different charts in one page. What I want is to open the Excel workbook with that have the data required. The program then read the data in the given range and store it as an array. After that, it will loop the array so that it can display each data point for the chart. I can get it to display the chart title. Other than that is nothing, just white blank screen. Where did I mess up? It didn't give me any error, so I don't know why nothing is shown. Here's the code I have so far:

    Code:
    Imports System.Reflection
    Imports Excel = Microsoft.Office.Interop.Excel
    'Add reference Assemblies, Framework, System.Windows.Forms.DataVisualization
    Imports System.Windows.Forms.DataVisualization.Charting
    
    
    Public Class Form1
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
            Dim excelApp As Excel.Application
            Dim excelWB As Excel.Workbook
            Dim excelWS As Excel.Worksheet
            Dim FNameRng As Excel.Range
            Dim AveRng As Excel.Range
            Dim AveCLRng As Excel.Range
            Dim AveUCLRng As Excel.Range
            Dim FNameArry As System.Array
            Dim AveArry As System.Array
            Dim AveCLArry As System.Array
            Dim AveUCLArry As System.Array
    
            excelApp = CreateObject("Excel.Application")
            excelApp.Visible = False
            'Open the Workbook
            excelWB = excelApp.Workbooks.Open("C:\Users\Joesph\Documents\Charts\Control Limit\18x17 - 10 mil stop.xlsx")
            excelWS = excelApp.Sheets("18x17 - 10 mil stop")
    
            'Set the Range for File Name
            FNameRng = excelWS.Range("A2", excelWS.Range("A2").End(Excel.XlDirection.xlDown))
            'Set the Range for Average Data
            AveRng = excelWS.Range("B2", excelWS.Range("B2").End(Excel.XlDirection.xlDown))
            AveCLRng = excelWS.Range("H2", excelWS.Range("H2").End(Excel.XlDirection.xlDown))
            AveUCLRng = excelWS.Range("I2", excelWS.Range("I2").End(Excel.XlDirection.xlDown))
    
            'Read in the values of a range of cells
            FNameArry = CType(FNameRng.Value, System.Array)
            AveArry = CType(AveRng.Value, System.Array)
            AveCLArry = CType(AveCLRng.Value, System.Array)
            AveUCLArry = CType(AveUCLRng.Value, System.Array)
    
            Me.CenterToScreen()
            Me.WindowState = FormWindowState.Maximized
    
            Chart1.Titles.Add("Title1")
            Chart1.Titles(0).Text = "Average"
            Chart1.Titles(0).Font = New Font("Garamond", 24, FontStyle.Bold)
    
            'Looping the Array
            For FNameEle As Integer = 0 To FNameArry.Length - 1
                Chart1.Series("Series1").XValueMember = "FNameEle"
            Next
            For AveEle As Integer = 0 To AveArry.Length - 1
                Chart1.Series("Series1").YValueMembers = "AveEle"
            Next
            For AveCLEle As Integer = 0 To AveCLArry.Length - 1
                Chart1.Series("Series1").YValueMembers = "AveEle"
            Next
            For AveUCLEle As Integer = 0 To AveUCLArry.Length - 1
                Chart1.Series("Series1").YValueMembers = "AveUCLEle"
            Next
    
            'excelWB.Close(SaveChanges:=False)
        End Sub
    End Class
  • mcupito
    Contributor
    • Aug 2013
    • 294

    #2
    Have you looked into Structures?

    Comment

    • cten
      New Member
      • Mar 2014
      • 4

      #3
      I got my answer already. My problem now is to get the last two loop to show only data with value. I can't get that to work. The points is either come all out or none at all.
      Here's my code:
      Code:
      'Set the Range
      A_GTRng = excelWS.Range("I2", excelWS.Range("I2").End(Excel.XlDirection.xlDown))
      
      'Read in the values of a range of cells
      A_GTArry = CType(A_GTRng.Value, System.Array)
      
      'Looping through the A_GTArry
              For x As Integer = 1 To A_GTArry.GetUpperBound(0)
                  For y As Integer = 1 To A_GTArry.GetUpperBound(1)
                      Dim A_GT As Object = A_GTArry(x, y)
                      For iPt = 1 To Chart1.Series("A_GT").Points.Count
                          If IsNumeric(A_GT) Then
                              Chart1.Series("A_GT").Points.Add(A_GT)
                          Else
                              Chart1.Series("A_GT").Points(iPt).MarkerStyle = MarkerStyle.None
                          End If
                      Next
                  Next y
              Next x
      In my workbook, there are some data that display as "#N/A" and the other as value (ex. 0.032). The code above display nothing. How can I make it to just display the one with value? Thanks!

      Comment

      • mcupito
        Contributor
        • Aug 2013
        • 294

        #4
        Are the #N/A fields actually containing data? It sounds like you would need to convert them to "" if #N/A in your worksheet is equivalent to Null. Excel to VB.NET handle null very differently.

        Comment

        • cten
          New Member
          • Mar 2014
          • 4

          #5
          In the worksheet, I have formula =IF($S2>=$T2,$S 2,NA()). It should be equivalent to null, but I don't know how to code it.

          Comment

          Working...