VBA:Export Data from A Range as PNG Format

Susanto3311
3StarLounger
Posts: 240
Joined: 17 Feb 2022, 05:16

VBA:Export Data from A Range as PNG Format

Post by Susanto3311 »

hi all..

the below code work properly. the code from Hans..but i want to modified like this:
here this steps:
this step macro can do it:
1. first time, select your range/cell that you want export NOT your shape/picture, then run macro
2. browse folder location to save...
3. save as .png format
4. the macro code working in active sheet (any name sheet).

Code: Select all

Sub ExportAsPNG()
    Dim wsh As Worksheet
    Dim shp As Shape
    Dim fil As Variant
    Dim cho As ChartObject
    On Error Resume Next
    Set shp = Selection.ShapeRange(1)
    On Error GoTo 0
    If shp Is Nothing Then
        MsgBox "Please select a shape/picture, then try again!", vbExclamation
        Exit Sub
    End If
    fil = Application.GetSaveAsFilename(InitialFileName:="*.png", FileFilter:="PNG files (*.png), *.png")
    If fil = False Then
        MsgBox "You didn't specify a filename!", vbExclamation
        Exit Sub
    End If
    Set wsh = ActiveSheet
    Set cho = wsh.ChartObjects.Add(Left:=shp.Left, Top:=shp.Top, Width:=shp.Width, Height:=shp.Height)
    shp.Copy
    cho.Select
    ActiveChart.Paste
    ActiveChart.Export Filename:=fil, FilterName:="PNG"
    cho.Delete
End Sub
anyone help me out, greatly appreciated..
susant

User avatar
HansV
Administrator
Posts: 78446
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: VBA:Export Data from A Range as PNG Format

Post by HansV »

I edited your post to change [quote]...[/quote] tags to [code]...[/code] tags.
Best wishes,
Hans

User avatar
HansV
Administrator
Posts: 78446
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: VBA:Export Data from A Range as PNG Format

Post by HansV »

Try this:

Code: Select all

Sub ExportRangeAsPNG()
    Dim wsh As Worksheet
    Dim rng As Range
    Dim fil As Variant
    Dim cho As ChartObject
    fil = Application.GetSaveAsFilename(InitialFileName:="*.png", FileFilter:="PNG files (*.png), *.png")
    If fil = False Then
        MsgBox "You didn't specify a filename!", vbExclamation
        Exit Sub
    End If
    Set wsh = ActiveSheet
    Set rng = Selection
    rng.CopyPicture
    Set cho = wsh.ChartObjects.Add(Left:=rng.Left, Top:=rng.Top, Width:=rng.Width, Height:=rng.Height)
    cho.Select
    ActiveChart.Paste
    ActiveChart.Export Filename:=fil, FilterName:="PNG"
    cho.Delete
End Sub
Best wishes,
Hans

Susanto3311
3StarLounger
Posts: 240
Joined: 17 Feb 2022, 05:16

Re: VBA:Export Data from A Range as PNG Format

Post by Susanto3311 »

hi hans...thank you so much!! work fine.

Susanto3311
3StarLounger
Posts: 240
Joined: 17 Feb 2022, 05:16

Re: VBA:Export Data from A Range as PNG Format

Post by Susanto3311 »

HansV wrote:
02 Jul 2022, 15:24
Try this:

Code: Select all

Sub ExportRangeAsPNG()
    Dim wsh As Worksheet
    Dim rng As Range
    Dim fil As Variant
    Dim cho As ChartObject
    fil = Application.GetSaveAsFilename(InitialFileName:="*.png", FileFilter:="PNG files (*.png), *.png")
    If fil = False Then
        MsgBox "You didn't specify a filename!", vbExclamation
        Exit Sub
    End If
    Set wsh = ActiveSheet
    Set rng = Selection
    rng.CopyPicture
    Set cho = wsh.ChartObjects.Add(Left:=rng.Left, Top:=rng.Top, Width:=rng.Width, Height:=rng.Height)
    cho.Select
    ActiveChart.Paste
    ActiveChart.Export Filename:=fil, FilterName:="PNG"
    cho.Delete
End Sub
hi hans, in progress, the code not work if the column/rows is hidden --'for a reason the columns/rows is protected'
show message "run time error '1004' Application defined or object defined error.
show message errror (yellow highlight " rng.Offset(0, rng.Columns.Count).PasteSpecial"
please, how to fix it?
You do not have the required permissions to view the files attached to this post.

User avatar
HansV
Administrator
Posts: 78446
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: VBA:Export Data from A Range as PNG Format

Post by HansV »

Could you attach a sample workbook (with the code) that demonstrates the problem?
Best wishes,
Hans

Susanto3311
3StarLounger
Posts: 240
Joined: 17 Feb 2022, 05:16

Re: VBA:Export Data from A Range as PNG Format

Post by Susanto3311 »

hi Hans...sorry i confuse about this..
if i use workbook that contains macro/code and protected your code (Export Range not working show message :
Set cho = wsh.ChartObjects.Add(Left:=rng.Left, Top:=rng.Top, Width:=rng.Width, Height:=rng.Height)
if i use workbook not contains code, the code work fine..

for a reason i can't attach the original file that contains code & protected
You do not have the required permissions to view the files attached to this post.

User avatar
HansV
Administrator
Posts: 78446
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: VBA:Export Data from A Range as PNG Format

Post by HansV »

Is the sheet protected?
Or the workbook?
Or the VBA project?
Best wishes,
Hans

Susanto3311
3StarLounger
Posts: 240
Joined: 17 Feb 2022, 05:16

Re: VBA:Export Data from A Range as PNG Format

Post by Susanto3311 »

the workbook is protected, the sheet is protected & vba protected too.
all of them are protected.

User avatar
HansV
Administrator
Posts: 78446
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: VBA:Export Data from A Range as PNG Format

Post by HansV »

Try code like this. Replace "secret" with the worksheet password.

Code: Select all

Sub ExportRangeAsPNG()
    Dim wsh As Worksheet
    Dim rng As Range
    Dim fil As Variant
    Dim cho As ChartObject
    fil = Application.GetSaveAsFilename(InitialFileName:="*.png", FileFilter:="PNG files (*.png), *.png")
    If fil = False Then
        MsgBox "You didn't specify a filename!", vbExclamation
        Exit Sub
    End If
    Set wsh = ActiveSheet
    wsh.Unprotect Password:="secret"
    Set rng = Selection
    rng.CopyPicture
    Set cho = wsh.ChartObjects.Add(Left:=rng.Left, Top:=rng.Top, Width:=rng.Width, Height:=rng.Height)
    cho.Select
    ActiveChart.Paste
    ActiveChart.Export Filename:=fil, FilterName:="PNG"
    cho.Delete
    wsh.Protect Password:="secret"
End Sub
Best wishes,
Hans