Export Selected Range as Image (Save Range as Picture) | Excel VBA Macro

Опубликовано: 15 Октябрь 2024
на канале: greggowaffles
6,560
116

Excel VBA Macro: Export Selected Range as Image (Save Range as Picture)

📈Open a Webull brokerage account and get up to 12 FREE stocks: https://a.webull.com/3xbKZC7ZoXzmhAHnrk

💥Subscribe:    / @greggowaffles  

Code:
Sub save_range_as_image()

Dim ws As Worksheet
Dim table As Range
Dim pic As Picture
Dim cht As ChartObject
Dim myPath As String
Dim myPic As String
Dim myWidth As Long
Dim myHeight As Long

Set ws = ThisWorkbook.Sheets("S&P 500 Stocks")
Set table = ws.Range("A1:C11")

myPath = "C:\Users\greggowaffles\Documents\Youtube Videos\Test\Investing\"
myPic = "Stock Performance " & Format(Date, "mm-dd-yy") & " " & _
WorksheetFunction.RandBetween(1000, 9999) & ".png"

table.CopyPicture xlScreen, xlPicture
myWidth = table.Width
myHeight = table.Height

Set cht = ws.ChartObjects.Add(Left:=0, Top:=0, _
Width:=myWidth, Height:=myHeight)

cht.Activate

With cht.Chart
.Paste
.Export Filename:=myPath & myPic, Filtername:="png"
End With

cht.Delete

End Sub

#ExcelVBA #ExcelMacro