Results 1 to 4 of 4

Thread: Problems in VB.net land with Excel charting!

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2006
    Posts
    7

    Problems in VB.net land with Excel charting!

    Howdy All,
    I've been tsked with producing an XY scatter Chart in Excel via a VB.net application.

    I have succesfully started Excel and imported the data to chart. I have also created a chrat and set it's type to xlXYScatterSmoothNoMarkers, which is what I want.

    I have having MAJOR problems setting the XValue Range and the Value Range of the chart via code... I've tried lokking at and imulating the macro capture of doing this, but when I get over into the .net land it does NOT seem to work!!!!

    VB Code:
    1. Dim oXL As Excel.Application
    2.       Dim oWB As Excel.Workbook
    3.       Dim oSheet As Excel.Worksheet
    4.       Dim oRng As Excel.Range
    5.  
    6.       ' Start Excel and get Application object.
    7.       oXL = CreateObject("Excel.Application")
    8.       oXL.Visible = True
    9.  
    10.       ' Get a new workbook.
    11.       'oWB = oXL.Workbooks.Add
    12.       oWB = oXL.Workbooks.OpenXML(job.Item("JobDir").InnerText & "\" & "vps.submitter." & job.Item("id_job").InnerText & ".xml")
    13.  
    14.  
    15.       oSheet = oWB.ActiveSheet
    16.       Dim rowIndex As Long
    17.       Dim DisplCol As Long
    18.       Dim ForceCol As Long
    19.  
    20.       Dim startRowIndex As Long
    21.       Dim endRowIndex As Long
    22.       Dim TLMAXRow As Long
    23.  
    24.       'Misc. code used here to set the column... (ie remove from this example...)
    25.        DisplCol = 2
    26.  
    27.       'Misc. code used here to set the row... (ie remove from this example...)
    28.       TLMAXRow = 33
    29.  
    30.       'Misc. code used here to set the row... (ie remove from this example...)
    31.       startRowIndex = 3
    32.  
    33.       'Misc. code used here to set the row... (ie remove from this example...)
    34.       endRowIndex = 83
    35.  
    36.       Dim NewCol As String
    37.       Dim NewColRange As String
    38.  
    39.       'the ColumnLetter function is one I wrote to convert a number to a Letter for the Column, ie. 3 = "C"
    40.       NewCol = ColumnLetter(DisplCol + 1)
    41.       NewColRange = NewCol & ":" & NewCol
    42.  
    43.       'MsgBox("NewCol : " & NewCol & vbCrLf & "NewColRange: " & NewColRange)
    44.  
    45.       oSheet.Range(NewColRange).Insert()
    46.  
    47.       'Misc. code used here to set the column... (ie remove from this example...)
    48.       ForceCol = 3
    49.  
    50.  
    51.       Dim NewChart As Excel.Chart
    52.  
    53.       NewChart = oWB.Charts.Add
    54.       NewChart.Activate()
    55.  
    56.       NewChart.ChartType = XlChartType.xlXYScatterSmoothNoMarkers
    57.  
    58.       Dim XValRange As Range
    59.       Dim ValRange As Range
    60.  
    61.       XValRange = oSheet.Range(NewCol & startRowIndex, NewCol & endRowIndex)
    62.       ValRange = oSheet.Range(ColumnLetter(ForceCol) & startRowIndex, ColumnLetter(ForceCol) & endRowIndex)
    63.  
    64.  
    65.       Dim serCollect As Excel.Series
    66.       serCollect = NewChart.SeriesCollection(1)
    67.  
    68.       Dim vrange As String
    69.       Dim xrange As String
    70.  
    71.       vrange = "=" & oSheet.Name & "!R" & startRowIndex & "C" & ForceCol & ":R" & endRowIndex & "C" & ForceCol
    72.       MessageBox.Show(vrange)
    73.  
    74.       xrange = "=" & oSheet.Name & "!$" & NewCol & "$" & startRowIndex & ":$" & NewCol & "$" & endRowIndex
    75.       MessageBox.Show(xrange)
    76.  
    77.  
    78.       'Heres where the "FUN" starts...  I can't get either of the following lines to work!!!
    79.       serCollect.Values = vrange
    80.       serCollect.XValues = xrange
    81.  
    82.       'I have also tried:
    83.       'serCollect.Values = ValRange
    84.       'serCollect.XValues = XValRange
    85.       ' To NO aval, that didn't work either...
    86.  
    87.       ' And I tried:
    88.       'serCollect.Values = ValRange.value
    89.       'serCollect.XValues = XValRange.value
    90.       '  Guess what.... that didn't work either...

    So I've tried a few different ways to do this with strings and ranges, neither WORK!!!

    Any Ideas out there on how to set the series XValues and Values out there????

  2. #2
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Problems in VB.net land with Excel charting!

    Quick questions
    1/ How many columns in your XML file?

    2/ If more than 2, then how do you determine which 2 columns contain the chart data?

    3/ Are all rows in the xml file used in the chart?

    4/ If not, then how do you determine which rows contain the chart data?
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  3. #3

    Thread Starter
    New Member
    Join Date
    Mar 2006
    Posts
    7

    Re: Problems in VB.net land with Excel charting!

    Ok,
    I was trying to save the forum at looking at misc. code... here's the full code to find the various colums and rows of where the data is...

    I do know that in row 2 are my column headings... and that in column "E" is where I will find the word "TLMAX"

    In my first post I said I had remove some of the code (For simplicity sakes!)
    Sorry I wasn't clear on that and for any confusion as a result!


    VB Code:
    1. foundResults = oSheet.Range("2:2").Find(what:="displacement", LookIn:=XlFindLookIn.xlValues, lookat:=XlLookAt.xlPart, MatchCase:=False)
    2.       If Not foundResults Is Nothing Then
    3.          DisplCol = foundResults.Column
    4.          'MsgBox("Displacement Column: " & DisplCol)
    5.       Else
    6.          MsgBox("Not Found")
    7.       End If
    8.  
    9.  
    10.       foundResults = oSheet.Range("E:E").Find(what:="TLMAX", LookIn:=XlFindLookIn.xlValues, lookat:=XlLookAt.xlWhole, MatchCase:=False)
    11.       If Not foundResults Is Nothing Then
    12.          TLMAXRow = foundResults.Row
    13.          'MsgBox("TLMAX Row: " & TLMAXRow)
    14.       Else
    15.          MsgBox("Not Found")
    16.       End If
    17.  
    18.       foundResults = oSheet.Range("A:A").Find(what:="0", LookIn:=XlFindLookIn.xlValues, lookat:=XlLookAt.xlWhole, MatchCase:=False)
    19.       If Not foundResults Is Nothing Then
    20.          startRowIndex = foundResults.Row
    21.          'MsgBox("Start Row Index  Row: " & startRowIndex)
    22.       Else
    23.          MsgBox("Not Found")
    24.       End If
    25.  
    26.  
    27.       foundResults = oSheet.Range("A:A").End(XlDirection.xlDown)
    28.       If Not foundResults Is Nothing Then
    29.          endRowIndex = foundResults.Row
    30.          'MsgBox("End Row Index  Row: " & endRowIndex)
    31.       Else
    32.          MsgBox("Not Found")
    33.       End If

    So that's the code to go out and find the extents of the data
    I want the displacement column to be my x values (in this case column 2)
    and the Y Values are in the "Force" column (in this case 3)

    The data starts on row 3 and ends on row 83 (in this case... the next file will be different and the code hould find the extents properly)

  4. #4
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Problems in VB.net land with Excel charting!

    OK
    Try this.
    You are doing way too much to find the range for the chart. All you really need are the Starting Column number, the starting row number and the ending row number.

    I have included an example file.

    VB Code:
    1. Sub BuildScatterChart()
    2. Dim oXL As Excel.Application
    3. Dim oWB As Excel.Workbook
    4. Dim oSheet As Excel.Worksheet
    5. Dim lStartCol As Long
    6. Dim lStartRow As Long
    7. Dim lEndRow As Long
    8. Dim rngData As Range
    9. Dim chtScat As Chart
    10.    
    11.     ' Start Excel and get Application object.
    12.     Set oXL = New Excel.Application
    13.     oXL.Visible = True
    14.    
    15.     ' Get a new workbook.
    16.     'oWB = oXL.Workbooks.Add
    17.     Set oWB = oXL.Workbooks.OpenXML(job.Item("JobDir").InnerText & "\" & "vps.submitter." & job.Item("id_job").InnerText & ".xml")
    18.    
    19.    
    20.     oSheet = oWB.ActiveSheet
    21.  
    22.     'Find the starting column number by looking for
    23.     ' "displacement" in Row 2
    24.     lStartCol = oSheet.Rows(2).Find(what:="displacement", _
    25.                                         LookIn:=XlFindLookIn.xlValues, _
    26.                                         lookat:=XlLookAt.xlPart, _
    27.                                         MatchCase:=False).Column
    28.                                        
    29.     'Find the starting Row number by looking for
    30.     ' a value of 0 in column A
    31.     lStartRow = oSheet.Columns(1).Find(what:="0", _
    32.                                         LookIn:=XlFindLookIn.xlValues, _
    33.                                         lookat:=XlLookAt.xlWhole, _
    34.                                         MatchCase:=False).Row
    35.    
    36.     'Find the ending Row number by looking for
    37.     ' a "TLMAX" in column E
    38.     lEndRow = oSheet.Columns(5).Find(what:="TLMAX", _
    39.                                         LookIn:=XlFindLookIn.xlValues, _
    40.                                         lookat:=XlLookAt.xlWhole, _
    41.                                         MatchCase:=False).Row
    42.    
    43.     'Set the Data range to the range between:
    44.         'The cell that is the intersection of the starting Row and Column
    45.     'and
    46.         'The cell that is the intersection of the ending Row and the next Column
    47.     With oSheet
    48.         Set rngData = .Range(.Cells(lStartRow, lStartCol), .Cells(lEndRow, lStartCol + 1))
    49.     End With
    50.    
    51.     'Add a chart
    52.     Set chtScat = ThisWorkbook.Charts.Add
    53.    
    54.     With chtScat
    55.         .ChartType = xlXYScatterSmoothNoMarkers
    56.        
    57.         'set the datasource for the chart to the range defined above
    58.         .SetSourceData rngData
    59.        
    60.         'Do what ever else you need with the chart
    61.         .HasLegend = False
    62.         .HasTitle = False
    63.     End With
    64. End Sub
    Attached Files Attached Files
    Last edited by DKenny; Mar 30th, 2006 at 04:51 PM. Reason: Added example file
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

Posting Permissions

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



Click Here to Expand Forum to Full Width