毎月の売上高を別々のシートに保存しています。 複数の製品の月ごとの売上をプロットしたいと思います。 各製品は、各月をX軸にとった同じグラフ上に、異なる色の線で表されます。
複数のシートの同じ相対セルから引き出される単一の折れ線グラフを作成する最良の方法は何でしょうか?
チャートウィザードを使用します。
ステップ2の4で、"Series"と書かれたタブがあります。このタブには、3つのフィールドとリスト・ボックスがあります。リスト・ボックスには、チャートにすでに含まれているさまざまな系列が表示されます。各系列には、その系列に固有の "Name"フィールドと "Values"フィールドの両方があります。最後のフィールドは、すべての系列に共通する「"Category (X) axis labels"」フィールドです。
リストボックスの下にある「"Add"」ボタンをクリックします。これにより、リストボックスに空白の系列が追加されます。リストボックスで系列をハイライトすると、"Name" と "Values" の値が変化することに注意してください。
新しいシリーズを選択します。
右側の各フィールドには、アイコンがあります。このアイコンは、データを取得するワークブックのセルを選択するためのものです。これをクリックすると、ウィザードが一時的に非表示になり(作業中のフィールドを除く)、ワークブックと対話できるようになります。
ワークブック内の適切なシートを選択し、チャートに表示したいデータのあるフィールドを選択します。フィールドの右側にあるボタンをクリックすると、ウィザードの非表示を解除することができます。
お役に立てれば幸いです。
EDIT: 上記は2003以前のものに適用されます。2007では、チャートを選択した状態で、リボンの[デザイン]タブにある[データの選択]オプションを使用して同様の操作を行うことができるはずです。これにより、チャートの系列をリストアップするダイアログボックスが表示されます。Excel 2003と同様に系列を選択することができますが、カスタム系列を定義するには、"Add"と"Edit"ボタンを使用する必要があります。
Excel 2010のコードで、使えるかもしれないものがあります。 このコードにはいくつかの特殊な点(タイトルに含まれるエンコード不良文字のフィルタリングなど)がありますが、絶対値およびパーセントベースのデータを持つ4次元データから複数の多列グラフを作成するために設計されたものです。 お好きなように修正してください。
Sub createAllGraphs()
Const chartWidth As Integer = 260
Const chartHeight As Integer = 200
If Sheets.Count = 1 Then
Sheets.Add , Sheets(1)
Sheets(2).Name = "AllCharts"
ElseIf Sheets("AllCharts").ChartObjects.Count > 0 Then
Sheets("AllCharts").ChartObjects.Delete
End If
Dim c As Variant
Dim c2 As Variant
Dim cs As Object
Set cs = Sheets("AllCharts")
Dim s As Object
Set s = Sheets(1)
Dim i As Integer
Dim chartX As Integer
Dim chartY As Integer
Dim r As Integer
r = 2
Dim curA As String
curA = s.Range("A" & r)
Dim curB As String
Dim curC As String
Dim startR As Integer
startR = 2
Dim lastTime As Boolean
lastTime = False
Do While s.Range("A" & r) <> ""
If curC <> s.Range("C" & r) Then
If r <> 2 Then
seriesAdd:
c.SeriesCollection.Add s.Range("D" & startR & ":E" & (r - 1)), , False, True
c.SeriesCollection(c.SeriesCollection.Count).Name = Replace(s.Range("C" & startR), "Â", "")
c.SeriesCollection(c.SeriesCollection.Count).XValues = "='" & s.Name & "'!$D$" & startR & ":$D$" & (r - 1)
c.SeriesCollection(c.SeriesCollection.Count).Values = "='" & s.Name & "'!$E$" & startR & ":$E$" & (r - 1)
c.SeriesCollection(c.SeriesCollection.Count).HasErrorBars = True
c.SeriesCollection(c.SeriesCollection.Count).ErrorBars.Select
c.SeriesCollection(c.SeriesCollection.Count).ErrorBar Direction:=xlY, Include:=xlBoth, Type:=xlCustom, Amount:="='" & s.Name & "'!$F$" & startR & ":$F$" & (r - 1), minusvalues:="='" & s.Name & "'!$F$" & startR & ":$F$" & (r - 1)
c.SeriesCollection(c.SeriesCollection.Count).ErrorBar Direction:=xlX, Include:=xlBoth, Type:=xlFixedValue, Amount:=0
c2.SeriesCollection.Add s.Range("D" & startR & ":D" & (r - 1) & ",G" & startR & ":G" & (r - 1)), , False, True
c2.SeriesCollection(c2.SeriesCollection.Count).Name = Replace(s.Range("C" & startR), "Â", "")
c2.SeriesCollection(c2.SeriesCollection.Count).XValues = "='" & s.Name & "'!$D$" & startR & ":$D$" & (r - 1)
c2.SeriesCollection(c2.SeriesCollection.Count).Values = "='" & s.Name & "'!$G$" & startR & ":$G$" & (r - 1)
c2.SeriesCollection(c2.SeriesCollection.Count).HasErrorBars = True
c2.SeriesCollection(c2.SeriesCollection.Count).ErrorBars.Select
c2.SeriesCollection(c2.SeriesCollection.Count).ErrorBar Direction:=xlY, Include:=xlBoth, Type:=xlCustom, Amount:="='" & s.Name & "'!$H$" & startR & ":$H$" & (r - 1), minusvalues:="='" & s.Name & "'!$H$" & startR & ":$H$" & (r - 1)
c2.SeriesCollection(c2.SeriesCollection.Count).ErrorBar Direction:=xlX, Include:=xlBoth, Type:=xlFixedValue, Amount:=0
If lastTime = True Then GoTo postLoop
End If
If curB <> s.Range("B" & r).Value Then
If curA <> s.Range("A" & r).Value Then
chartX = chartX + chartWidth * 2
chartY = 0
curA = s.Range("A" & r)
End If
Set c = cs.ChartObjects.Add(chartX, chartY, chartWidth, chartHeight)
Set c = c.Chart
c.ChartWizard , xlXYScatterSmooth, , , , , True, Replace(s.Range("B" & r), "Â", "") & " " & s.Range("A" & r), s.Range("D1"), s.Range("E1")
Set c2 = cs.ChartObjects.Add(chartX + chartWidth, chartY, chartWidth, chartHeight)
Set c2 = c2.Chart
c2.ChartWizard , xlXYScatterSmooth, , , , , True, Replace(s.Range("B" & r), "Â", "") & " " & s.Range("A" & r) & " (%)", s.Range("D1"), s.Range("G1")
chartY = chartY + chartHeight
curB = s.Range("B" & r)
curC = s.Range("C" & r)
End If
curC = s.Range("C" & r)
startR = r
End If
If s.Range("A" & r) <> "" Then oneMoreTime = False ' end the loop for real this time
r = r + 1
Loop
lastTime = True
GoTo seriesAdd
postLoop:
cs.Activate
End Sub