創建圖表,我有以下代碼:迅速從數據透視表
Private Sub PivGraphMaker1(ByVal shtnm As String, ByVal src1 As String, ByVal chrtnm As String)
Application.StatusBar = "Making graph 1."
Sheets(shtnm).Select
Range("O1").Select
ActiveSheet.Shapes.AddChart.Select
ActiveChart.ChartType = xlColumnClustered
ActiveChart.SetSourceData Source:=Range(src1)
ActiveChart.Parent.Name = chrtnm
ActiveChart.Legend.Select
Selection.Delete
ActiveSheet.ChartObjects(chrtnm).Activate
ActiveChart.SeriesCollection(1).Select
Selection.Format.Fill.Visible = msoFalse
ActiveChart.SeriesCollection(1).ApplyDataLabels
ActiveChart.SeriesCollection(1).DataLabels.Select
Selection.Position = xlLabelPositionInsideEnd
ActiveChart.ChartArea.Select
ActiveChart.SetElement (msoElementChartTitleAboveChart)
ActiveChart.ChartTitle.Text = "DownTime by Fault Message"
Selection.Format.TextFrame2.TextRange.Characters.Text = _
"DownTime by Fault Message"
With Selection.Format.TextFrame2.TextRange.Characters(1, 25).ParagraphFormat
.TextDirection = msoTextDirectionLeftToRight
.Alignment = msoAlignCenter
End With
With Selection.Format.TextFrame2.TextRange.Characters(1, 17).Font
.BaselineOffset = 0
.Bold = msoTrue
.NameComplexScript = "+mn-cs"
.NameFarEast = "+mn-ea"
.Fill.Visible = msoTrue
.Fill.ForeColor.RGB = RGB(0, 0, 0)
.Fill.Transparency = 0
.Fill.Solid
.Size = 18
.Italic = msoFalse
.Kerning = 12
.Name = "+mn-lt"
.UnderlineStyle = msoNoUnderline
.Strike = msoNoStrike
End With
ActiveChart.ChartArea.Select
ActiveChart.SetElement (msoElementPrimaryValueAxisTitleRotated)
ActiveChart.Axes(xlValue, xlPrimary).AxisTitle.Text = "Duration in Minutes"
Selection.Format.TextFrame2.TextRange.Characters.Text = "Duration in Minutes"
With Selection.Format.TextFrame2.TextRange.Characters(1, 19).ParagraphFormat
.TextDirection = msoTextDirectionLeftToRight
.Alignment = msoAlignCenter
End With
With Selection.Format.TextFrame2.TextRange.Characters(1, 19).Font
.BaselineOffset = 0
.Bold = msoTrue
.NameComplexScript = "+mn-cs"
.NameFarEast = "+mn-ea"
.Fill.Visible = msoTrue
.Fill.ForeColor.RGB = RGB(0, 0, 0)
.Fill.Transparency = 0
.Fill.Solid
.Size = 10
.Italic = msoFalse
.Kerning = 12
.Name = "+mn-lt"
.UnderlineStyle = msoNoUnderline
.Strike = msoNoStrike
End With
ActiveChart.ChartArea.Select
ActiveChart.ChartTitle.Text = shtnm & " DownTime by Fault Message"
ActiveChart.ChartArea.Select
ActiveChart.Location Where:=xlLocationAsNewSheet
ActiveSheet.Name = shtnm & " Faults"
Application.PrintCommunication = False
With ActiveChart.PageSetup
.LeftHeader = ""
.CenterHeader = ""
.RightHeader = ""
.LeftFooter = ""
.CenterFooter = ""
.RightFooter = ""
.LeftMargin = Application.InchesToPoints(0.7)
.RightMargin = Application.InchesToPoints(0.7)
.TopMargin = Application.InchesToPoints(0.75)
.BottomMargin = Application.InchesToPoints(0.75)
.HeaderMargin = Application.InchesToPoints(0.3)
.FooterMargin = Application.InchesToPoints(0.3)
.ChartSize = xlScreenSize
.PrintQuality = 600
.CenterHorizontally = False
.CenterVertically = False
.Orientation = xlLandscape
.Draft = False
.OddAndEvenPagesHeaderFooter = False
.DifferentFirstPageHeaderFooter = False
.EvenPage.LeftHeader.Text = ""
.EvenPage.CenterHeader.Text = ""
.EvenPage.RightHeader.Text = ""
.EvenPage.LeftFooter.Text = ""
.EvenPage.CenterFooter.Text = ""
.EvenPage.RightFooter.Text = ""
.FirstPage.LeftHeader.Text = ""
.FirstPage.CenterHeader.Text = ""
.FirstPage.RightHeader.Text = ""
.FirstPage.LeftFooter.Text = ""
.FirstPage.CenterFooter.Text = ""
.FirstPage.RightFooter.Text = ""
.PaperSize = xlPaperTabloid
.FirstPageNumber = xlAutomatic
.BlackAndWhite = False
.Zoom = 100
End With
Application.PrintCommunication = True
End Sub
的代碼做的工作。我的問題是 - 所有這些行必要?那麼,有什麼我可以做的,以加速代碼(將刪除不必要的行幫助)?
爲了回答我的一些評論,我知道會出現 - 我的電腦確實很慢,動力不足,所以我注意到運行時速度方面的小變化。我也已經有屏幕更新和事件禁用的東西。
讓我知道我能做什麼!
有沒有真正的需要創建圖表作爲嵌入式chartobject,然後將它移動到圖表工作表(只使用'charts.add'創建圖表在開始時),很多格式都是不必要的,因爲它可能是默認值。此外,儘量避免pagesetup,除非絕對必要 - 即使PrintCommunication關閉它也需要一些時間。 – Rory
我使用宏記錄器創建了這個,因爲我不知道如何通過編碼自己來創建圖表。代碼看起來像使用'chart.add'會怎樣?謝謝! – tannman357