I have a list box (extended multi-select) that contains a list of possible variables that I can plot in Excel. The listbox contains only the variable names which will be found in the header row of my Excel worksheet. I want to select one or more variables from the listbox and plot the selected data.
I can manually set the range as shown in red below and the plot is generated as I want. The problem that I'm having is trying to set this range based on the listbox selection. I can get a numeric value that represents the column that I want but how do I get the A:A syntax? Do I need this format?
Code:Dim oCht As Excel.Chart Dim oSht As Excel.Worksheet Dim oWB As Excel.Workbook Dim oRng As Excel.Range Dim strRng As String = "A:A" 'The first column (time) will always be used Try oApp = New Excel.Application oWB = oApp.Workbooks.Open("c:\temp\test.xls") oSht = CType(oApp.Worksheets(1), Excel.Worksheet) oCht = CType(oWB.Charts.Add, Excel.Chart) 'For Each idx As Integer In lstAllVars.SelectedIndices ' strRng = lstAllVars.Items(idx).ToString ' oRng = CType(oSht.Cells(1, idx), Excel.Range).EntireColumn 'Next oRng = oSht.Range("A:A,B:B,D:D") With oCht .SetSourceData(oRng) .Name = "MyChartTab" .PlotBy = Excel.XlRowCol.xlColumns .HasTitle = True .ChartTitle.Text = "MyChart" End With oApp.Visible = True Catch ex As Exception MessageBox.Show(ex.Message.ToString) End Try




Reply With Quote