如何使用VBA将粘贴图片放在excel图表中心?

时间:2016-04-01 15:59:44

标签: excel vba excel-vba

我的下面的代码包含一个范围并将其转换为我自动保存的图片。图片始终粘贴在右上角。有没有办法将图片置于图表中心,甚至缩小图表以仅适合图片大小?

Sub topicture(Sendrng As range)

        Dim xcht As Chart
        Dim Sname As String
        Sname = ActiveSheet.Name
        Sendrng.CopyPicture xlScreen, xlPicture
        Set xcht = Charts.Add
        With xcht
            .ChartArea.ClearContents
            .Paste
            .ChartArea.Left = (xcht.ChartArea.Width - .Width) / 2
            .Export Filename:="CDrive\Photos\" & Sname & ".jpg", Filtername:="JPG"
            .Delete
        End With
End Sub

1 个答案:

答案 0 :(得分:1)

如果您将图表放在工作表上,则可以调整图表的大小:

Sub tester()
    ExportRangePicture Range("B4:G20"), "C:\_Stuff\test1.jpg"
    ExportRangePicture Range("B4:G4"), "C:\_Stuff\test2.jpg"
End Sub


Sub ExportRangePicture(Sendrng As Range, fPath As String)
    Dim xcht
    Sendrng.CopyPicture xlScreen, xlPicture
    Set xcht = Sendrng.Parent.Shapes.AddChart
    With xcht.Chart
        Do While .SeriesCollection.Count > 0
            .SeriesCollection(1).Delete
        Loop
        .Parent.Width = Sendrng.Width
        .Parent.Height = Sendrng.Height
        .Paste
        .Export Filename:=fPath, Filtername:="JPG"
        .Parent.Delete
    End With
End Sub