0
我正在使用excel vba執行一些計算。最後,我創建一個圖表並將其放在用戶表單上。 我第一次執行它,它工作。之後,我有一個'重新啓動'按鈕,可以重新啓動程序(不必終止它)。第二次執行代碼時,由於某種原因,圖片模糊不清。 正如您在下面的代碼中看到的那樣,我將圖片保存在我的文檔中,並且在代碼運行的第二次時(第一次出現效果很好)將圖片保存爲模糊。 此行爲非常奇怪,我無法理解它。 這裏是我的代碼,保存圖表:Excel vba導出圖表第二次模糊
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分享。
的問題是,首先運行畫面輸出很好。第二次運行(不終止程序)會將圖片導出模糊。此外,我需要以不接受png或pdf格式的用戶表單顯示圖片。所以我必須留在jpeg。 @ 0liveradam8 – user2809151