在其他工作簿中指定范围和types以图表

我需要从CSV文件创build图表。 这种情况每天都会发生,所以我把它自动化了。

当CSV文件被创build时,它们被存储在当天的文件夹中。

当我打开我的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 

上面的代码工作正常,但只绘制图表的父母。 例如,我find的所有选项都应该使其成为折线图

 ActiveChart.ChartType = xlLine 

和范围

 ActiveChart.SetSourceData Source:=Range(Cells(2, "A"), Cells(LastRow, LastColumn)) 

导致类似的错误

“没有设置”

要么

“此选项不适用于此对象”

您需要引用您使用AddChart创build的图表:

用你的代码:

 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)) 

##这里是你的完整的代码,我提出的实现:

 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 

以下是我想要在Excel中创build图表时使用的内容:

 Sub Graph() Dim Gr As Chart, _ Sr As Series, _ Src_Name As String Src_Name = "Sheet1" Set Gr = ActiveWorkbook.Charts.Add With Gr '----Source Data Definition .SetSourceData Source:=Range(Sheets(Src_Name).Cells(2, 1), Sheets(Src_Name).Cells(1197, 2)), PlotBy:=xlColumns '----Graph Type .ChartType = xlXYScatterSmoothNoMarkers '----Location/Placement .Location Where:=xlLocationAsNewSheet, Name:="NewSheetName" '----Title .HasTitle = True .ChartTitle.Characters.Text = "Chart Title" '----Data Series 1 Set Sr = .SeriesCollection.NewSeries With Sr .Values = Range(Sheets(Src_Name).Cells(2, 2), Sheets(Src_Name).Cells(20, 5)) .XValues = Range(Sheets(Src_Name).Cells(2, 1), Sheets(Src_Name).Cells(20, 1)) .AxisGroup = 1 .Name = "MTTF" End With '----Data Series 2 Set Sr = .SeriesCollection.NewSeries With Sr .Values = Range(Sheets(Src_Name).Cells(2, 2), Sheets(Src_Name).Cells(20, 5)) .XValues = Range(Sheets(Src_Name).Cells(2, 1), Sheets(Src_Name).Cells(20, 1)) .Name = "MTTR" '----Placing a Serie on Second axis .AxisGroup = 2 End With '----Series' formats '.SeriesCollection(i).Delete '----For a line type chart '.SeriesCollection(i).Format.Line.Weight = 1 '.SeriesCollection(i).Format.Line.ForeColor.RGB = RGB(int1 as integer, int1 as integer, int3 as integer) '----For an area type chart '.SeriesCollection(i).Format.Fill.ForeColor.RGB = RGB(int1 as integer, int1 as integer, int3 as integer) '----Axis parameters .Axes(xlCategory, xlPrimary).HasTitle = True .Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "Age" .Axes(xlValue, xlPrimary).HasTitle = True .Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "Hours" .PlotArea.Interior.ColorIndex = 2 .Axes(xlValue).MajorGridlines.Border.LineStyle = xlDot .ChartArea.Font.Size = 14 .Deselect '----Legend positioning With .Legend .Left = 350 .Top = 75 End With '----Drawing area positiong With .PlotArea .Width = 550 .Height = 350 End With End With '----Free memory Set Gr = Nothing Set Sr = Nothing End Sub