在excel中创建条形图

时间:2016-03-01 10:17:56

标签: vbscript

我正在尝试使用vb脚本创建条形图。 我的脚本是

Set objExcel = CreateObject("Excel.Application")
Set objReadWorkbook = objExcel.Workbooks.Open("D:\Excel_Macro_Proj\Create_Barchart.xlsx")
Set oExcelReadWorkSheet = objReadWorkbook.Worksheets(1)
objReadWorkbook.Sheets("Sheet1").Activate
objExcel.Visible = True

Sub GraphCreate ()
   ActiveSheet.Shapes.AddChart2(201, xlColumnClustered).Select
   ActiveSheet.Range("D11:D14,F11:F14,H11:H14")
End Sub
GraphCreate
objReadWorkbook.SaveAs("D:\Excel_Macro_Proj\barchart_create1.xls"),-4143
objExcel.Quit

当我运行上面的宏我得到一个错误,因为, "对象需要ActiveSheet"。我没有得到这个错误的原因。请帮助我。

1 个答案:

答案 0 :(得分:0)

vbscript无法直接识别ActiveSheet个对象,它可以在Excel.Application对象下使用..在这种情况下objExcel.ActiveSheetobjExcel.ActiveChart

试试此代码

Set objExcel = CreateObject("Excel.Application")
Set objReadWorkbook = objExcel.Workbooks.Open("D:\Excel_Macro_Proj\Create_Barchart.xlsx")
Set oExcelReadWorkSheet = objReadWorkbook.Worksheets(1)
objReadWorkbook.Sheets("Sheet1").Activate
objExcel.Visible = True

Sub GraphCreate ()
   objExcel.ActiveSheet.Shapes.AddChart2(201, xlColumnClustered).Select
   set xlRange = objExcel.ActiveSheet.Range("D11:D14,F11:F14,H11:H14")
   objExcel.ActiveChart.SetSourceData(xlRange)
End Sub

GraphCreate
objReadWorkbook.SaveAs("D:\Excel_Macro_Proj\barchart_create1.xls"),-4143
objExcel.Quit