我需要从CSV文件创建图表。这种情况每天发生在很多桌子上,所以我自动化了。
创建CSV文件后,它们将从当天存储在文件夹中。
当我打开我的VBA脚本时,它将读取该文件夹中的所有CSV文件,并将所有表格(显然每个表格都粘贴在一个新工作簿中)。
With NewBook
Set sv = .Sheets.Add(After:=.Sheets(i))
sv.Name = SvName
< Add CSV-files to just created sheet >
With sv
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
LastColumn = .Cells(2, .Columns.Count).End(xlToLeft).Column
Debug.Print SvName & ":" & vbTab & "Cells(" & LastRow; ", " & LastColumn & ")"
End With
Set svChart = .Sheets.Add(After:=.Sheets(SvName))
svChart.Name = SvName & " Chart"
With svChart
.Shapes.AddChart.Name = SvName & "-cht"
With .Shapes(SvName & "-cht")
.Left = Range("A1").Left
.Top = Range("A1").Top
.Width = Range("A1:AC56").Width
.Height = Range("A1:AC56").Height
End With
End With
End With
上面的代码工作正常,但只绘制图表的父代。我发现的所有选项应该使它成为折线图,例如
ActiveChart.ChartType = xlLine
和范围
ActiveChart.SetSourceData Source:=Range(Cells(2, "A"), Cells(LastRow, LastColumn))
导致错误,如
“未设置”
或
“此选项不适用于此对象”
答案 0 :(得分:0)
您需要引用使用 File mydir = context.getDir("mydir", Context.MODE_PRIVATE); //Creating an internal directory/folder;
File imageFile1 = new File(mydir, "myfile"); //Getting a file within the dir.
FileOutputStream out = new FileOutputStream(imageFile1);
// to get the path of the file you can call the getPath() or getAbsolutePath() methods
String path = imageFile1.getAbsolutePath();
创建的图表:
使用您的代码:
AddChart
使用我的代码(没有Dim oChart As Chart
Set oChart = .Shapes.AddChart
oChart.Name = SvName & "-cht"
oChart.ChartType = xlLine
oChart.SetSourceData Source:=Range(Cells(2, "A"), Cells(LastRow, LastColumn))
):
Shapes
## 以下是我提出的实现的完整代码:
Dim oChart As Chart
Set oChart = ActiveWorkbook.Charts.Add
oChart.Name = SvName & "-cht"
oChart.ChartType = xlLine
oChart.SetSourceData Source:=Range(Cells(2, "A"), Cells(LastRow, LastColumn))
以下是我想在Excel中创建图表时使用的内容:
Dim oChart As Chart
With NewBook
Set sv = .Sheets.Add(After:=.Sheets(i))
sv.Name = SvName
'< Add CSV-files to just created sheet >
With sv
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
LastColumn = .Cells(2, .Columns.Count).End(xlToLeft).Column
Debug.Print SvName & ":" & vbTab & "Cells(" & LastRow; ", " & LastColumn & ")"
End With
Set svChart = .Sheets.Add(After:=.Sheets(SvName))
svChart.Name = SvName & " Chart"
With svChart
Set oChart = ActiveWorkbook.Charts.Add
'oChart.Select
oChart.Name = SvName & "-cht"
oChart.ChartType = xlLine
oChart.SetSourceData Source:=Range(Cells(2, "A"), Cells(LastRow, LastColumn))
With .Shapes(SvName & "-cht")
.Left = Range("A1").Left
.Top = Range("A1").Top
.Width = Range("A1:AC56").Width
.Height = Range("A1:AC56").Height
End With
End With
End With