VBA Powerpoint 2010 从 csv 数据创建图表
VBA Powerpoint 2010 create a chart from csv data
我正在尝试创建一个 makro 以根据存储在 csv 文件中的数据在 powerpoint 2010 中自动生成图表。我只生成了图表并读取了 csv 文件,但现在我仍然无法显示数据。
到目前为止,这是我的代码:
Sub CreateChart()
Dim myChart As Chart
Dim gChartData As ChartData
Dim gWorkBook As Excel.Workbook
Dim gWorkSheet As Excel.Worksheet
Dim strPath As String
Dim lngLastRow As Long
Dim mySystemFileObject
' Create the chart and set a reference to the chart data.
Set myChart = ActivePresentation.Slides(1).Shapes.AddChart.Chart
Set gChartData = myChart.ChartData
' read the csv file
strPath = "C:\path\to\my\data.csv"
' Set the Workbook and Worksheet references.
Set gWorkBook = gChartData.Workbook
Set gWorkSheet = gWorkBook.Worksheets(1)
With gWorkSheet.QueryTables.Add(Connection:="TEXT;" & strPath, Destination:=gWorkSheet.Range("A1"))
.TextFileDecimalSeparator = "."
.TextFileThousandsSeparator = " "
.TextFileParseType = xlDelimited
.TextFileCommaDelimiter = True
.Refresh
End With
gWorkBook.RefreshAll
' Does not work, returns Runtime Error 1004
'gWorkSheet.ListObjects("Tabelle1").Resize gWorkSheet.Range("A1:G5")
myChart.SetSourceData ("='Tabelle1'!A7:G74")
myChart.Refresh
'gWorkSheet.Range("A7:G74").Select
' Clean up the references.
Set gWorkSheet = Nothing
' gWorkBook.Application.Quit
gWorkBook.Close
Set gChartData = Nothing
Set myChart = Nothing
End Sub
现在的问题是 select 范围 A1:G74 和 VBA 并在图表中显示数据。可能我只是缺少正确的功能。
任何想法或帮助表示赞赏!
此致,
吉姆
解决办法是,一次只能改变一个方向。
所以我通过将 csv 数据读取为文本文件解决了这个问题,而不是调整图表的范围,然后用 csv 文件中的数据填充图表
strRange = "A1:D" + CStr(UBound(tmpData.XValues))
gWorkSheet.ListObjects("Tabelle1").Resize gWorkSheet.Range(strRange)
' change dim 2
If ((UBound(myData, 2) * 2) + 1 > 4) Then
strRange = "A1:" + HelperFunctions.ConvertToLetter2((UBound(myData, 2) * 2) + 1) + CStr(UBound(tmpData.XValues))
gWorkSheet.ListObjects("Tabelle1").Resize gWorkSheet.Range(strRange)
End If
之后通过以下方式将数据写入单元格:
For l = 1 To UBound(tmpData.XValues)
gWorkSheet.Cells(l, k + 1).Value = tmpData.XValues(l - 1)
gWorkSheet.Cells(l, k + 2).Value = tmpData.YValues(l - 1)
Next
我正在尝试创建一个 makro 以根据存储在 csv 文件中的数据在 powerpoint 2010 中自动生成图表。我只生成了图表并读取了 csv 文件,但现在我仍然无法显示数据。
到目前为止,这是我的代码:
Sub CreateChart()
Dim myChart As Chart
Dim gChartData As ChartData
Dim gWorkBook As Excel.Workbook
Dim gWorkSheet As Excel.Worksheet
Dim strPath As String
Dim lngLastRow As Long
Dim mySystemFileObject
' Create the chart and set a reference to the chart data.
Set myChart = ActivePresentation.Slides(1).Shapes.AddChart.Chart
Set gChartData = myChart.ChartData
' read the csv file
strPath = "C:\path\to\my\data.csv"
' Set the Workbook and Worksheet references.
Set gWorkBook = gChartData.Workbook
Set gWorkSheet = gWorkBook.Worksheets(1)
With gWorkSheet.QueryTables.Add(Connection:="TEXT;" & strPath, Destination:=gWorkSheet.Range("A1"))
.TextFileDecimalSeparator = "."
.TextFileThousandsSeparator = " "
.TextFileParseType = xlDelimited
.TextFileCommaDelimiter = True
.Refresh
End With
gWorkBook.RefreshAll
' Does not work, returns Runtime Error 1004
'gWorkSheet.ListObjects("Tabelle1").Resize gWorkSheet.Range("A1:G5")
myChart.SetSourceData ("='Tabelle1'!A7:G74")
myChart.Refresh
'gWorkSheet.Range("A7:G74").Select
' Clean up the references.
Set gWorkSheet = Nothing
' gWorkBook.Application.Quit
gWorkBook.Close
Set gChartData = Nothing
Set myChart = Nothing
End Sub
现在的问题是 select 范围 A1:G74 和 VBA 并在图表中显示数据。可能我只是缺少正确的功能。 任何想法或帮助表示赞赏!
此致, 吉姆
解决办法是,一次只能改变一个方向。
所以我通过将 csv 数据读取为文本文件解决了这个问题,而不是调整图表的范围,然后用 csv 文件中的数据填充图表
strRange = "A1:D" + CStr(UBound(tmpData.XValues))
gWorkSheet.ListObjects("Tabelle1").Resize gWorkSheet.Range(strRange)
' change dim 2
If ((UBound(myData, 2) * 2) + 1 > 4) Then
strRange = "A1:" + HelperFunctions.ConvertToLetter2((UBound(myData, 2) * 2) + 1) + CStr(UBound(tmpData.XValues))
gWorkSheet.ListObjects("Tabelle1").Resize gWorkSheet.Range(strRange)
End If
之后通过以下方式将数据写入单元格:
For l = 1 To UBound(tmpData.XValues)
gWorkSheet.Cells(l, k + 1).Value = tmpData.XValues(l - 1)
gWorkSheet.Cells(l, k + 2).Value = tmpData.YValues(l - 1)
Next