【问题标题】:Word Macro to create a pie chart from numerous tablesWord 宏从众多表格创建饼图
【发布时间】:2015-11-03 00:45:52
【问题描述】:

我有一个 word 文档,里面有很多表格。每个表的第一行是标题,使用修改后的“标题1”样式,还有一个类别字段,使用修改后的标题4样式。

我想编写一个宏,将每个表按类别分组,并创建一个显示分布的图表。

这可能吗?如果可以,我在哪里可以找到有关如何开始的参考资料?

我不是 VBA 程序员,但我对编程语言有一定的了解,如果我能找到正确的参考资料,甚至可能的话,我希望我能一起破解一些东西。..

【问题讨论】:

    标签: vba ms-word


    【解决方案1】:

    这个问题有点太开放了,但也许这样的事情会有所帮助

    'Option Explicit
    
    Sub CreatePie()
    
        Dim shReport As Worksheet
        Dim pc As PivotCache
        Dim pt As PivotTable
        Dim oChart  As Shape
        Dim arrSources As Variant
    
        ' Remove the report sheet, this is just to remove the testng sheet.
        Application.DisplayAlerts = False
        On Error Resume Next
        Sheets("Report").Delete
        On Error GoTo 0
        Application.DisplayAlerts = True
    
        ' Get all the srouces. Asuming that they all start in Range("A")
        arrSources = GetSources
    
        Set shReport = Sheets.Add(After:=Sheets(Sheets.Count))
        shReport.Name = "Report"
        Set pc = ActiveWorkbook.PivotCaches.Create(xlConsolidation, arrSources)
        Set pt = pc.CreatePivotTable("Report!R1C1")
        Set oChart = shReport.Shapes.AddChart2(, xlPie)
        oChart.Chart.SetSourceData pt.TableRange1
    
    End Sub
    
    ' Function to get all the ranges to be consolitidated.
    Function GetSources() As Variant
    
        Dim sh As Worksheet
        Dim rng As Range
        Dim ret() As Variant
        Dim i As Long
    
        ReDim ret(Sheets.Count - 1)
    
        ' Fill all the ranges
        For i = 0 To Sheets.Count - 1
            Set sh = Sheets(i + 1)
            Set rng = sh.Cells(1, 1).CurrentRegion
    
            ret(i) = Array(sh.Name & "!" & rng.Address(True, True, xlR1C1), "Item" & i + 1)
        Next i
    
        Var = Array(Array("Sheet1!R1C1:R5C2", "Item1"), Array("Sheet2!R1C1:R5C2", "Item2"), Array("Sheet3!R1C1:R5C2", "Item3"))
    
        ' Return the value
        GetSources = ret
    
    End Function
    

    Here is my sample file.

    希望这会有所帮助:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-11
      • 1970-01-01
      相关资源
      最近更新 更多