0

I am using excel vba to perform some calculation. At the end I create a chart and put it on a user form. On the first time I perform it, it works. Afterwards, I have a 'restart' button which starts the program all over again (without terminating it). On the second time I perform the code, the picture comes out blurry for some reason. As you can see in below code, I save the picture in my documents and it saves the picture blurry on the second time the code runs (on the first time it comes out good). This behaviour is very wierd and I can't understand it. Here's my code that saves the chart:

Private Sub ShowGraphButton_Click()
'Creating an image chart, saving it and displaying it in the userform

Dim result As Chart
Dim chart_data As Range
Dim trend As Trendline

'Setting a chart
Set result = Sheet1.Shapes.AddChart(xlXYScatter).Chart
Set chart_data = Sheets("Sheet2").Range("B2:B10000")

'Application.ScreenUpdating = False

'Chart settings
With result
    'Series settings
    .SeriesCollection.NewSeries
    .SeriesCollection(1).Name = "                       "
    .SeriesCollection(1).Values = chart_data
    .SeriesCollection(1).XValues = Sheets("Sheet2").Range("A2:A10000")
    .SeriesCollection(1).MarkerSize = 12

    'Axes settings
    .Axes(xlCategory, xlPrimary).ReversePlotOrder = False
    .Axes(xlCategory, xlPrimary).HasTitle = True
    .Axes(xlCategory, xlPrimary).AxisTitle.Text = "Time (seconds)"
    .Axes(xlCategory, xlPrimary).AxisTitle.Font.size = 12
    .Axes(xlValue, xlPrimary).HasTitle = True
    .Axes(xlValue, xlPrimary).AxisTitle.Text = "StandardDeviation"
    .Axes(xlValue, xlPrimary).AxisTitle.Font.size = 12

    'Area settings
    .ChartArea.Height = 350
    .ChartArea.Width = 600
    .ChartArea.Format.Fill.ForeColor.RGB = RGB(183, 207, 255)

    'Chart title settings
    .HasTitle = True
    .ChartTitle.Text = "StandardDeviation per second"
    .ChartTitle.Characters.Font.Bold = True
    .ChartTitle.Characters.Font.Color = RGB(0, 0, 0)
    .ChartTitle.Characters.Font.Name = "arial"

    'Legend settings
    .HasLegend = True
    .Legend.Position = xlLegendPositionLeft
    .Legend.IncludeInLayout = True
    .Legend.Height = 20
    .Legend.Width = 100

    'Trendline settings
    Set trend = .SeriesCollection(1).Trendlines.Add
    trend.DisplayEquation = True
    trend.DisplayRSquared = True
    trend.DataLabel.Left = 20

End With


'Creates the image
Dim img_name As String
img_name = Application.DefaultFilePath & Application.PathSeparator & "myChart.jpeg"
result.Export Filename:=img_name, Filtername:="jpeg"

'Deletes the chart from sheet1
Sheet1.ChartObjects(1).Delete

GraphForm.Picture = LoadPicture(img_name)

RunningMode.Hide
GraphForm.Show

Application.ScreenUpdating = True
End Sub

If any other information is required I will share accordantly.

user2809151
  • 23
  • 1
  • 10

1 Answers1

0

Firstly, you can try saving the chart as a png. That often results in a higher quality image when exporting.

Another reason for the blurriness could be that the chart is too small when exported. Try increasing the size of the chart in the following code:

.ChartArea.Height = 350
.ChartArea.Width = 600

You'll also have to increase font sizes, in the axis title for example, and possibly re-position the legend.

Another option would be to export the chart as a pdf, which is a vectored file:

Private Sub btnSavePDF_Click()
Dim Filename As String, myShell As Object
Filename = Application.InputBox("Enter the pdf file name", Type:=2)
ActiveSheet.ChartObjects("RefChart").Activate
ActiveSheet.ExportAsFixedFormat xlTypePDF, Filename, xlQualityStandard
Set myShell = CreateObject("WScript.Shell")
myShell.Run Filename
End Sub

Source: http://www.vbaexpress.com/forum/archive/index.php/t-33369.html

0liveradam8
  • 752
  • 4
  • 18
  • The issue is that first run the picture is exported well. On second run (without terminating the program) it exports the picture blurry. Also I need to show the picture in a user form which does not accepts png or pdf formats. So I must stay with jpeg. @0liveradam8 – user2809151 Aug 15 '17 at 16:36