Я узнал вашу проблему. Поскольку диаграммы сгруппированы, отображение легенды изменяется, поэтому маловероятно, что диаграмма и легенда будут связаны.
Поэтому я решил использовать ярлыки. Я создал две макрокарты с правильной легендой и измененной диаграммой и скопировал только часть легенды на первом графике, а также добавил макрос для вставки в виде рисунка в часть легенды на втором графике. В разрешении есть недостаток.
Рабочий процесс
Изображение результата
код
Sub ModifyCahrtLegend()
Dim Cht1 As Chart, Cht2 As Chart, Cht3 As Chart
Dim Shp As Shape, Shp1 As Shape, Shp2 As Shape, Shp3 As Shape
Dim Ws As Worksheet
Dim rngX As Range
Dim rngHeader As Range
Dim Srs As Series
Dim fn As String
Dim obj As ChartObject
Dim l, t, w, h
Dim cl, tb, ct
Dim i As Integer
Dim a As Variant
Set Ws = ActiveSheet
For Each obj In Ws.ChartObjects
obj.Delete
Next obj
With Ws
Set rngX = .Range("a2", .Range("a" & Rows.Count).End(xlUp))
Set rngHeader = .Range("b1")
End With
a = Array(1, 0, 2, 3)
r = rngX.Rows.Count
Set Shp1 = Ws.Shapes.AddChart(, Range("g1").Left, 100, 500, 300)
Set Cht1 = Shp1.Chart
With Cht1
For Each Srs In .SeriesCollection
Srs.Delete
Next Srs
.ChartType = xlLine
.HasLegend = True
.Legend.Position = xlLegendPositionBottom
For i = 0 To 3
Set Srs = .SeriesCollection.NewSeries
With Srs
.XValues = rngX
.Values = rngHeader.Offset(0, a(i)).Offset(1).Resize(r)
.Name = rngHeader.Offset(0, a(i))
End With
Next i
End With
Set Shp2 = Ws.Shapes.AddChart(, Range("g1").Left, 100, 500, 300)
Set Cht2 = Shp2.Chart
With Cht2
For Each Srs In .SeriesCollection
Srs.Delete
Next Srs
.ChartType = xlLine
.HasLegend = True
.Legend.Position = xlLegendPositionBottom
For i = 0 To 3
Set Srs = .SeriesCollection.NewSeries
With Srs
.XValues = rngX
.Values = rngHeader.Offset(0, a(i)).Offset(1).Resize(r)
.Name = rngHeader.Offset(0, a(i))
End With
Next i
Set Srs = .SeriesCollection(2)
With Srs
.ChartType = xlColumnClustered
End With
End With
With Cht1
t = .Legend.Top
l = .Legend.Left
h = .Legend.Height
w = .Legend.Width
' .CopyPicture
End With
'** picture editing
Cht1.CopyPicture
Range("C23").Select
Ws.Pictures.Paste
n = Ws.Shapes.Count
Set Shp = Ws.Shapes(n)
With Shp1
cl = (.Width - w) / 2
cb = .Height - t - h
ct = .Height - h - cb
End With
With Shp
.PictureFormat.CropLeft = cl
.PictureFormat.CropRight = cl
.PictureFormat.CropTop = ct
.PictureFormat.CropBottom = cb
End With
Set Cht3 = Ws.Shapes.AddChart.Chart
Set obj = Cht3.Parent
With obj
.Top = t
.Left = l
.Height = h
.Width = w
.ShapeRange.Line.ForeColor.RGB = RGB(255, 255, 255)
End With
Shp.CopyPicture
Cht3.Paste
fn = "legend.png"
Cht3.Export fn, "PNG"
Shp.Delete
obj.Delete
Shp1.Delete
Set Shp = Cht2.Shapes.AddPicture(fn, msoFalse, msoCTrue, l, t, w, h)
Kill fn
End Sub
Однако созданная таким образом диаграмма деформируется при изменении размера, поэтому было бы лучше создать диаграмму, задав ширину и высоту.
код 2
Sub testChart()
ModifyLegend 500, 200 '<~~ set width, Height
End Sub
Sub ModifyLegend(myW, myH)
Dim Cht1 As Chart, Cht2 As Chart, Cht3 As Chart
Dim Shp As Shape, Shp1 As Shape, Shp2 As Shape, Shp3 As Shape
Dim Ws As Worksheet
Dim rngX As Range
Dim rngHeader As Range
Dim Srs As Series
Dim fn As String
Dim obj As ChartObject
Dim l, t, w, h
Dim cl, tb, ct
Dim i As Integer
Dim a As Variant
Set Ws = ActiveSheet
For Each obj In Ws.ChartObjects
obj.Delete
Next obj
With Ws
Set rngX = .Range("a2", .Range("a" & Rows.Count).End(xlUp))
Set rngHeader = .Range("b1")
End With
a = Array(1, 0, 2, 3)
r = rngX.Rows.Count
Set Shp1 = Ws.Shapes.AddChart(, Range("g1").Left, 100, myW, myH)
Set Cht1 = Shp1.Chart
With Cht1
For Each Srs In .SeriesCollection
Srs.Delete
Next Srs
.ChartType = xlLine
.HasLegend = True
.Legend.Position = xlLegendPositionBottom
For i = 0 To 3
Set Srs = .SeriesCollection.NewSeries
With Srs
.XValues = rngX
.Values = rngHeader.Offset(0, a(i)).Offset(1).Resize(r)
.Name = rngHeader.Offset(0, a(i))
End With
Next i
End With
Set Shp2 = Ws.Shapes.AddChart(, Range("g1").Left, 100, myW, myH)
Set Cht2 = Shp2.Chart
With Cht2
For Each Srs In .SeriesCollection
Srs.Delete
Next Srs
.ChartType = xlLine
.HasLegend = True
.Legend.Position = xlLegendPositionBottom
For i = 0 To 3
Set Srs = .SeriesCollection.NewSeries
With Srs
.XValues = rngX
.Values = rngHeader.Offset(0, a(i)).Offset(1).Resize(r)
.Name = rngHeader.Offset(0, a(i))
End With
Next i
Set Srs = .SeriesCollection(2)
With Srs
.ChartType = xlColumnClustered
End With
End With
With Cht1
t = .Legend.Top
l = .Legend.Left
h = .Legend.Height
w = .Legend.Width
' .CopyPicture
End With
'** picture editing
Cht1.CopyPicture
Range("C23").Select
Ws.Pictures.Paste
n = Ws.Shapes.Count
Set Shp = Ws.Shapes(n)
With Shp1
cl = (.Width - w) / 2
cb = .Height - t - h
ct = .Height - h - cb
End With
With Shp
.PictureFormat.CropLeft = cl
.PictureFormat.CropRight = cl
.PictureFormat.CropTop = ct
.PictureFormat.CropBottom = cb
End With
Set Cht3 = Ws.Shapes.AddChart.Chart
Set obj = Cht3.Parent
With obj
.Top = t
.Left = l
.Height = h
.Width = w
.ShapeRange.Line.ForeColor.RGB = RGB(255, 255, 255)
End With
Shp.CopyPicture
Cht3.Paste
fn = "legend.png"
Cht3.Export fn, "PNG"
Shp.Delete
obj.Delete
Shp1.Delete
Set Shp = Cht2.Shapes.AddPicture(fn, msoFalse, msoCTrue, l, t, w, h)
Kill fn
End Sub