2017-08-15 11 views
0

Excelを使用して計算を実行しています。最後に、グラフを作成してユーザーフォームに貼り付けます。 私は初めてそれを実行します。その後、私はプログラムを全部(終了せずに)起動する「再起動」ボタンを持っています。 2回目にコードを実行すると、何らかの理由で画像がぼやけてしまいます。 以下のコードでわかるように、私は自分のドキュメントに画像を保存し、コードが2回目にはぼやけて表示されます(最初はうまくいきます)。 この動作は非常に面白く、わかりません。 ここで、チャートを保存し、私のコードです:Excel vbaエクスポートグラフが2回目にぼやけます

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 

その他の情報が必要な場合は、私がaccordantly共有することになります。

答えて

0

まず、グラフをpngとして保存することができます。エクスポートすると、高品質の画像が得られることがよくあります。

ぼかしのもう1つの理由は、エクスポート時にグラフが小さすぎることが原因である可能性があります。次のコードでは、グラフのサイズを大きくしてみてください。

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

あなたは伝説を例えば軸タイトルで、フォントサイズを増加させ、そしておそらく再配置することもあります。

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 

出典:

別のオプションは、ベクタファイルであるPDFファイルとしてチャートをエクスポートするだろうhttp://www.vbaexpress.com/forum/archive/index.php/t-33369.html

+0

問題が最初にうまくエクスポートされた絵を実行することです。 2回目の実行(プログラムを終了せずに)で、画像がぼやけて書き出されます。また、私はpng形式やpdf形式を受け付けないユーザーフォームで画像を表示する必要があります。だから私はjpegと一緒にいなければならない。 @ 0liveradam8 – user2809151

関連する問題