/ / VB.Netを使用してExcelでシリーズのXValuesを設定する方法-vb.net、excel、vba、excel-vba

VB.Netを使用してExcelでシリーズのXValueを設定する方法-vb.net、excel、vba、excel-vba

2列のデータでXY散布図を作成しようとしています。 Excelシートの最初の列はセルA2からA30のX、2番目の列はセルB2からB30のYです。

グラフを作成できますが、プロットしています2つのデータのシリーズでは、col Xを1つのシリーズとして、col Yを別のシリーズとして使用しています。 vb.netで構文がどのように機能するかわからず、vb.netでこれを行う方法に関するドキュメントを見つけることができなかったため、vbaのドキュメントからいくつかのアイデアを得ました(vbaで次のように定義できます。

     Charts("Chart1").SeriesCollection(1).XValues =_Worksheets("Sheet1").Range("B1:B5")

そして、次の行を生成しました。

だから私はラインでシリーズのXValueを設定しようとしました

    xlApp.ActiveChart.SeriesCollection(1).XValues = xlWorkSheet.Range("$A$2", "$A$30")
xlApp.ActiveChart.SeriesCollection(1).Values = xlWorkSheet.Range("$B$2", "$B$30")

しかし、それは私にエラーを投げています:COMExceptionは上記の行で処理されませんでした。私が何を間違えたかわからないので、助けてください。

チャートを生成するコードブロックは次のとおりです。基本的には、「Excelファイルを読み込み、ファイル内のデータを使用してグラフを作成します。

Private Sub Create_Chart_Click(sender As Object, e As EventArgs) Handles Create_Chart.Click
Dim xlApp As Excel.Application
Dim xlWorkBook As Excel.Workbook
Dim xlWorkSheet As Excel.Worksheet
xlApp = New Excel.ApplicationClass

"~~> Add a New Workbook
xlWorkBook = xlApp.Workbooks.Open("C:Test_data.xlsx")

"Display Excel
xlApp.Visible = True

"~~> Set the relebant sheet that we want to work with
xlWorkSheet = xlWorkBook.Sheets("Sheet1")

With xlWorkSheet

"~~> Inserting a Graph
.Shapes.AddChart.Select()

"~~> Formatting the chart
With xlApp.ActiveChart
"~~> Make it a Line Chart
.ApplyCustomType(Excel.XlChartType.xlXYScatterSmoothNoMarkers)

"~~> Set the data range
xlApp.ActiveChart.SeriesCollection(1).Name = "X-Y"
xlApp.ActiveChart.SeriesCollection(1).XValues = xlWorkSheet.Range("$A$2", "$A$30")
xlApp.ActiveChart.SeriesCollection(1).Values = xlWorkSheet.Range("$B$2", "$B$30")

"~~> Fill the background of the chart
xlApp.ActiveChart.ChartArea.Format.Fill.ForeColor.ObjectThemeColor = _
Microsoft.Office.Core.MsoThemeColorIndex.msoThemeColorBackground1 "<~~ Grey
xlApp.ActiveChart.ChartArea.Format.Fill.ForeColor.TintAndShade = 0
xlApp.ActiveChart.ChartArea.Format.Fill.ForeColor.Brightness = -0.150000006
xlApp.ActiveChart.ChartArea.Format.Fill.Transparency = 0
xlApp.ActiveChart.ChartArea.Format.Fill.Solid()
xlApp.ActiveChart.SeriesCollection(1).Trendlines.Add()
xlApp.ActiveChart.SeriesCollection(1).Trendlines(1).Type = Microsoft.Office.Interop.Excel.XlTrendlineType.xlPolynomial
xlApp.ActiveChart.SeriesCollection(1).Trendlines(1).Order = 2
"xlApp.ActiveChart.SeriesCollection(1).Format.Line.Visible = Microsoft.Office.Core.MsoTriState.msoFalse





"~~> Make the corners of the Chart Rount
".Parent.RoundedCorners = True

"~~> Removing lines and the back color so plot area shows char"s background color
With .PlotArea
.Format.Fill.Visible = Microsoft.Office.Core.MsoTriState.msoFalse
.Format.Line.Visible = Microsoft.Office.Core.MsoTriState.msoFalse
End With

"~~> Removing the major gridlines
".Axes(Excel.XlAxisType.xlValue).MajorGridlines.Format.Line.Visible = Microsoft.Office.Core.MsoTriState.msoFalse

"~~> Making the series line smooth
".SeriesCollection(1).Smooth = True

"~~> Formatting the legend
With .Legend
With .Format.TextFrame2.TextRange.Font.Fill
.Visible = Microsoft.Office.Core.MsoTriState.msoTrue
.ForeColor.RGB = RGB(0, 0, 0)
.Transparency = 0
.Solid()
End With

With .Format.Fill
.Visible = Microsoft.Office.Core.MsoTriState.msoTrue
.ForeColor.ObjectThemeColor = Microsoft.Office.Core.MsoThemeColorIndex.msoThemeColorBackground1
.ForeColor.TintAndShade = 0
.ForeColor.Brightness = -0.25
.Transparency = 0
.Solid()
End With
End With

"~~> Change the format of Y axis to show $ signs
".Axes(Excel.XlAxisType.xlValue).TickLabels.NumberFormat = "#,##0.00"

"~~> Underline the Chart Title
" .ChartTitle.Format.TextFrame2.TextRange.Font.UnderlineStyle = _
" Microsoft.Office.Core.MsoLineStyle.msoLineSingle
End With
End With

End Sub

ありがとう

回答:

回答№1は0

このようにしてみてください

xlApp.ActiveChart.SeriesCollection(1).XValues = "={""tes1"",""Test2""}"

または

MyVal="{""" & xlWorkSheet.range("A2") & """"
MyVal=MyVal & ",""" & xlWorkSheet.range("A30") & """}"
xlApp.ActiveChart.SeriesCollection(1).XValues = MyVal

回答№2の場合は0

XValuesは実際には例えば配列にすることができます:

Dim MyXVal() as string={"123","345","567"}
xlApp.ActiveChart.SeriesCollection(1).XValues = MyXVal