I'm using this VBA to export a specific range to an Image file. But is asking where to save the file. Can someone help me to correct this VBA by setting a default file location?
And also don't know why this part is using "Loop Until (ChO.Chart.Shapes.Count > 0 Or i > 50)".
Thanks in advance.
And also don't know why this part is using "Loop Until (ChO.Chart.Shapes.Count > 0 Or i > 50)".
Thanks in advance.
VBA Code:
Sub SaveAsJPG()
Dim ChO As ChartObject, ExportName As String
Dim CopyRange As Range
Dim Pic As Picture
Dim i As Long
Dim xRg As Range
Application.ScreenUpdating = False
For Each xRg In Range("G12:G33")
If xRg.Value = "0" Then
xRg.EntireRow.Hidden = True
Else
xRg.EntireRow.Hidden = False
End If
Next xRg
Application.ScreenUpdating = True
With ActiveSheet
Set CopyRange = Range("A1:H43")
If Not CopyRange Is Nothing Then
Application.ScreenUpdating = False
ExportName = Application.GetSaveAsFilename(InitialFileName:=.Range("G3") & " " & .Range("C3"), fileFilter:="JPEG Files (*.jpg), *.jpg")
If Not ExportName = "False" Then
CopyRange.Copy
.Pictures.Paste
Set Pic = .Pictures(.Pictures.Count)
Set ChO = .ChartObjects.Add(Left:=10, Top:=10, Width:=Pic.Width, Height:=Pic.Height)
Application.CutCopyMode = False
Do
DoEvents
Pic.Copy
DoEvents
ChO.Chart.Paste
DoEvents
i = i + 1
Loop Until (ChO.Chart.Shapes.Count > 0 Or i > 50)
ChO.Chart.Export Filename:=ExportName, Filtername:="JPG"
ChO.Delete
Pic.Delete
End If
Application.ScreenUpdating = True
End If
End With
End Sub
Last edited by a moderator: