我需要根据我在VBA中存储在字典中的值(Set dict = CreateObject(“Scripting.Dictionary”))创建一个柱形图。 x轴是键,y轴是值。有没有办法做到这一点?
答案 0 :(得分:0)
试试这个:
Public Sub WriteDictionary()
Dim dict As Variant
Dim currRow As Integer
Set dict = CreateObject("Scripting.Dictionary")
currRow = 1
dict.Add "key 1", "data 1"
dict.Add "key 2", "data 2"
For Each Key In dict.Keys
Range("A" & currRow).Formula = Key
Range("B" & currRow).Formula = dict(Key)
currRow = currRow + 1
Next Key
Set dict = Nothing
End Sub
答案 1 :(得分:0)
我回答这个问题是因为我有同样的问题,而且我在这里找不到答案。
我使用数组来存储脚本字典中的数据。然后,数组提供图形的源数据。
Sub DictToChart()
'Arrays for graph
Dim xinput As Variant
Dim yinput As Variant
'Example dictionary
Dim dict As Scripting.Dictionary
Set dict = New Scripting.Dictionary
'Populate dictionary
dict.Add 0, 0
dict.Add 10, 1
dict.Add 20, 2
dict.Add 30, 3
dict.Add 40, 4
'Populate arrays for graph
ReDim xinput(0 To dict.Count - 1)
ReDim yinput(0 To dict.Count - 1)
For i = 0 To dict.Count - 1
xinput(i) = dict.Keys(i)
yinput(i) = dict.Items(i)
Next i
'Create graph
ActiveSheet.Shapes.AddChart(xlColumnClustered).Select
ActiveChart.SeriesCollection.NewSeries
With ActiveChart
.SeriesCollection(1).xvalues = xinput
.SeriesCollection(1).Values = yinput
End With
End Sub