Вы можете попробовать это. Он использует инструмент «Камера», который в наши дни практически невидим в Excel.
На листе 1 есть пустой график, и он содержит связанный рисунок (см. SetUpChart
, чтобы добавить рисунок).
Изменение формулы изображения приводит к изменению связанного изображения. Похоже, что этот процесс не влияет на буфер обмена.
Обратите внимание, что если лист с диаграммой не активен во время выполнения, вы получите неправильный вывод.
'Test the export
Sub ExportSomeRanges()
ExportRangeNoCopy Sheet1.Range("B11:D24"), "c:\Temp\test.png"
ExportRangeNoCopy Sheet1.Range("B5:C8"), "c:\Temp\test2.png"
ExportRangeNoCopy Sheet2.Range("C8:K13"), "c:\Temp\test3.png"
End Sub
'the sheet with the chart container needs to be active...
Sub ExportRangeNoCopy(rng As Range, fName As String)
With Sheet1.ChartObjects(1)
'size the chart
.Height = rng.Height
.Width = rng.Width
With .Chart.Shapes(1)
'size the picture and set the source range
.Top = 0
.Left = 0
'change the link to the source range
.DrawingObject.Formula = "'" & rng.Parent.Name & "'!" & _
rng.Address(True, True)
.Width = rng.Width
.Height = rng.Height
End With
DoEvents
.Chart.Export fileName:=fName, FilterName:="png"
End With
End Sub
'Set up a chart with a linked picture object (one-time task)
'could not figure out how to do this manually. Cribbed from:
'https://www.ozgrid.com/forum/index.php?thread/5405-solved-camera-tool-how-to-use-it-in-code/
Sub SetUpChart()
Dim rngcolor
Set rngcolor = Range("B5:C8")
rngcolor.Copy
ActiveSheet.ChartObjects(1).Select
With ActiveChart.Pictures.Paste
.Formula = "=" & rngcolor.Address()
End With
Application.CutCopyMode = False
End Sub