LoginSignup
0
2

More than 3 years have passed since last update.

セル範囲を画像として保存するExcelマクロ

Posted at

これは

選択したセル範囲をPNG画像として保存するだけの、ちょっとしたExcelマクロです。

コード

'参照設定:
' Microsoft Scripting Runtime
' Windows Script Host Object Model

Function RangeToPng(r As Range, Optional fn As String = vbNullString)
    With New FileSystemObject
        If fn = vbNullString Then fn = .BuildPath(DesktopFolder, Replace(.GetTempName, "tmp", "png"))
    End With
    With New Excel.Application
        With .Workbooks.Add
            With .Worksheets(1).ChartObjects.Add(0, 0, r.Width, r.Height).Chart
                r.CopyPicture
                .Paste
                .Export fn, "png"
            End With
            .Close False
        End With
        .Quit
    End With
End Function

Private Function DesktopFolder()
    With New WshShell
        DesktopFolder = .SpecialFolders("Desktop")
    End With
End Function

使用例

Sub セル範囲を画像にする()
    Dim fn As String
    RangeToPng ActiveWindow.RangeSelection, fn
    MsgBox fn
End Sub
0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2