如何在EXCEL中插入饼图或柱状图(高分相送,急!!!)

schollc 2003-08-26 08:54:20
如何在EXCEL中插入饼图或柱状图,要vba的代码,具体功能是把一个sheet中的数据反映到另一个sheet中的图。
...全文
7166 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
schollc 2003-09-02
  • 打赏
  • 举报
回复
那这个属性是什么用呀,有没有什么可以代替的?
fongzl 2003-09-01
  • 打赏
  • 举报
回复
这个问题怎么讨论了这么久?
录个宏看看代码
我就是这样实现的
LeeZi 2003-09-01
  • 打赏
  • 举报
回复
可能是版本的问题。我用的是OFFICE XP。
我没有OFFICE2000的环境。
schollc 2003-09-01
  • 打赏
  • 举报
回复
With oTable.DataPivotField
.Orientation = xlColumnField
.Position = 1
End With

这两句好像有点问题,报的错是这个属性不可用。
LeeZi 2003-08-29
  • 打赏
  • 举报
回复
还是上面的数据:

Sub CreatePivotChart()
On Error GoTo ErrHandle

Dim oCache As PivotCache
Set oCache = ThisWorkbook.PivotCaches.Add(xlDatabase, "Sheet1!R1C1:R5C3")

Dim oSheet As Worksheet
Set oSheet = ThisWorkbook.Worksheets.Add
Dim oRange As Range
Set oRange = oSheet.Range("A1")
Dim oTable As PivotTable
Set oTable = oCache.CreatePivotTable(oRange, "PivotTable")

oTable.ColumnGrand = False
oTable.RowGrand = False

Dim oRowField As PivotField
Set oRowField = oTable.PivotFields("Task")
oRowField.Orientation = xlRowField
oRowField.Position = 1

Dim oDataField As PivotField
Set oDataField = oTable.PivotFields("B")
oDataField.Orientation = xlDataField
oDataField.Position = 1
oDataField.Function = xlSum
oDataField.Name = "SB"

Set oDataField = oTable.PivotFields("A")
oDataField.Orientation = xlDataField
oDataField.Position = 1
oDataField.Function = xlSum
oDataField.Name = "SA"

With oTable.DataPivotField
.Orientation = xlColumnField
.Position = 1
End With

ThisWorkbook.ShowPivotTableFieldList = False

Dim oChart As Chart
Set oChart = Charts.Add
oChart.Location xlLocationAsNewSheet
oChart.ChartType = xlColumnClustered
oChart.HasPivotFields = False

Application.DisplayAlerts = False
oSheet.Delete
Application.DisplayAlerts = True
Exit Sub
ErrHandle:
Debug.Print Err.Description
Stop
End Sub
schollc 2003-08-26
  • 打赏
  • 举报
回复
我倒是使用你那个方法了,但是看上去没有使用PivotTables方便。如果有这方面的建议最好了,谢谢。
LeeZi 2003-08-26
  • 打赏
  • 举报
回复
原本就不是PivotTables,直接加入TABLE就可以了。

Task A B
1 12
2 6 6
3 12
4 7 5

用Task做X轴,用A、B做Series就可以了。
schollc 2003-08-26
  • 打赏
  • 举报
回复
如果我要用PivotTables这个东西呢,因为你给我的那段代码实现的图表相对来说比较简单。我要实现的功能如下(可能叙述的不太好,要是能贴图就好了):
由两个人(a,b),在一个月里共计完成了10件工作(可能a做了6件,b做了7件,他们之间有交叉),我要做一个柱状图,实现横轴是工作(10件),纵轴是每个人在每个工作中花的时间,每个人用不同颜色的柱表示,可能在工作1只有a的柱4.5小时,但工作2有a2小时,旁边有b3小时。
我现在已经录了一段宏,如下:
ActiveWorkbook.PivotCaches.add(SourceType:=xlDatabase, SourceData:= _
"sheet1!R2C1:R75C3").CreatePivotTable TableDestination:=Range("E3"), _
TableName:="pivottable11"

ActiveSheet.PivotTables("pivottable11").SmallGrid = False
Charts.add
ActiveChart.SetSourceData Source:=Sheets("sheet1").Range("E3")
ActiveChart.Location Where:=xlLocationAsNewSheet
With ActiveChart.PivotLayout.PivotFields("工作")
.Orientation = xlRowField
.Position = 1
End With
With ActiveChart.PivotLayout.PivotFields("工作时间")
.Orientation = xlDataField
.Position = 1
End With
With ActiveChart.PivotLayout.PivotFields("负责人")
.Orientation = xlColumnField
.Position = 1
End With

请问你知道代码上段的参数如何运用吗?
本人在线等待,非常急,研究一天了也没有头绪,万分感谢了。
icansaymyabc 2003-08-26
  • 打赏
  • 举报
回复
Sub Macro1()
Range("A1").Select
ActiveCell.FormulaR1C1 = "1"
Range("A2").Select
ActiveCell.FormulaR1C1 = "2"
Range("A3").Select
ActiveCell.FormulaR1C1 = "3"
Range("A4").Select
ActiveCell.FormulaR1C1 = "4"
Range("A5").Select
ActiveCell.FormulaR1C1 = "5"
Range("B1").Select
ActiveCell.FormulaR1C1 = "546"
Range("B2").Select
ActiveCell.FormulaR1C1 = "6546"
Range("B3").Select
ActiveCell.FormulaR1C1 = "5446"
Range("B4").Select
ActiveCell.FormulaR1C1 = "5456"
Range("B5").Select
ActiveCell.FormulaR1C1 = "789"
Range("B6").Select
ActiveCell.FormulaR1C1 = "1258"
Range("B6").Select
Selection.ClearContents
Range("A1:B5").Select
Charts.Add
ActiveChart.ChartType = xl3DPieExploded
ActiveChart.SetSourceData Source:=Sheets("Sheet1").Range("A1:B5"), PlotBy:= _
xlColumns
ActiveChart.Location Where:=xlLocationAsObject, Name:="Sheet1"
ActiveWindow.Visible = False
Windows("Book1").Activate
Range("I10").Select
End Sub
icansaymyabc 2003-08-26
  • 打赏
  • 举报
回复
Sub Macro1()
Range("A1").Select
ActiveCell.FormulaR1C1 = "1"
Range("A2").Select
ActiveCell.FormulaR1C1 = "2"
Range("A3").Select
ActiveCell.FormulaR1C1 = "3"
Range("A4").Select
ActiveCell.FormulaR1C1 = "4"
Range("A5").Select
ActiveCell.FormulaR1C1 = "5"
Range("B1").Select
ActiveCell.FormulaR1C1 = "546"
Range("B2").Select
ActiveCell.FormulaR1C1 = "6546"
Range("B3").Select
ActiveCell.FormulaR1C1 = "5446"
Range("B4").Select
ActiveCell.FormulaR1C1 = "5456"
Range("B5").Select
ActiveCell.FormulaR1C1 = "789"
Range("B6").Select
ActiveCell.FormulaR1C1 = "1258"
Range("B6").Select
Selection.ClearContents
Range("A1:B5").Select
Charts.Add
ActiveChart.ChartType = xl3DPieExploded
ActiveChart.SetSourceData Source:=Sheets("Sheet1").Range("A1:B5"), PlotBy:= _
xlColumns
ActiveChart.Location Where:=xlLocationAsObject, Name:="Sheet1"
ActiveWindow.Visible = False
Windows("Book1").Activate
Range("I10").Select
End Sub

5,139

社区成员

发帖
与我相关
我的任务
社区描述
其他开发语言 Office开发/ VBA
社区管理员
  • Office开发/ VBA社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧