2014-06-26 52 views
0

創建圖表,我有以下代碼:迅速從數據透視表

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 

的代碼做的工作。我的問題是 - 所有這些行必要?那麼,有什麼我可以做的,以加速代碼(將刪除不必要的行幫助)?

爲了回答我的一些評論,我知道會出現 - 我的電腦確實很慢,動力不足,所以我注意到運行時速度方面的小變化。我也已經有屏幕更新和事件禁用的東西。

讓我知道我能做什麼!

+2

有沒有真正的需要創建圖表作爲嵌入式chartobject,然後將它移動到圖表工作表(只使用'charts.add'創建圖表在開始時),很多格式都是不必要的,因爲它可能是默認值。此外,儘量避免pagesetup,除非絕對必要 - 即使PrintCommunication關閉它也需要一些時間。 – Rory

+0

我使用宏記錄器創建了這個,因爲我不知道如何通過編碼自己來創建圖表。代碼看起來像使用'chart.add'會怎樣?謝謝! – tannman357

回答

1

像這樣的東西(發碼):

Private Sub PivGraphMaker1(ByVal shtnm As String, ByVal src1 As String, ByVal chrtnm As String) 
    Dim cht As Excel.Chart 
    Application.StatusBar = "Making graph 1." 

    Sheets(shtnm).Select 
    Range(src1).Select 
    Set cht = Charts.Add 
    With cht 
     .ChartType = xlColumnClustered 
     .SetSourceData Source:=Range(src1) 
     .Name = shtnm & " faults" 
     .HasLegend = False 

     With .SeriesCollection(1) 
      .Format.Fill.Visible = msoFalse 
      .ApplyDataLabels 
      .DataLabels.Position = xlLabelPositionInsideEnd 
     End With 

     .SetElement msoElementChartTitleAboveChart 

     With .ChartTitle 
      .Text = shtnm & " DownTime by Fault Message" 
      With .Format.TextFrame2.TextRange.Characters.ParagraphFormat 
       .TextDirection = msoTextDirectionLeftToRight 
       .Alignment = msoAlignCenter 
      End With 
      With .Format.TextFrame2.TextRange.Characters(1, 17).Font 
       .Bold = msoTrue 
       .Size = 18 
       .Kerning = 12 
      End With 
     End With 

     .SetElement msoElementPrimaryValueAxisTitleRotated 
     With .Axes(xlValue, xlPrimary).AxisTitle 
      .Text = "Duration in Minutes" 
      With .Format.TextFrame2.TextRange.Characters(1, 19) 
       .ParagraphFormat 
       With .Font 
        .Bold = msoTrue 
        .Size = 10 
        .Kerning = 12 
       End With 
      End With 
     End With 

     Application.PrintCommunication = False 
     With .PageSetup 
      .Orientation = xlLandscape 
      .PaperSize = xlPaperTabloid 
     End With 
     Application.PrintCommunication = True 
    End With 
    Application.StatusBar = False 
End Sub 
+0

'發碼「是什麼意思?順便說一句,現在嘗試代碼 – tannman357

+1

壞笑話 - 只是意味着它不在我的頭頂。 ;) – Rory

+0

代碼工作得很好!我不得不將'.ParagraphFormat'行註釋掉,因爲它給出了一個錯誤。沒有什麼改變,所以我認爲可以做。 – tannman357