2015-01-03 89 views
0

我是一位在路易斯安那州的小型石油公司工作的地質學家。我構成了我們的技術部門,不幸的是我的編碼經驗非常有限。過去我使用過非常基本的vba編碼,但在日常工作中我沒有編寫太多的代碼,所以我已經忘記了大部分。我最近發現了這個網站,它已經成爲我的一個很好的資源。我已經能夠從以前對以前問題的回答中收集一些代碼,但我再次陷入困境。基於數據範圍生成圖表

我的整個宏觀點是從外部數據庫檢索石油生產數據,根據所述數據計算某些值,然後創建顯示數據某些方面的圖。我有完成前兩個目標的代碼,但我正在努力實現圖形制作過程自動化的代碼。

我的問題是每口井有不同數量的數據。例如,一口井將生產5年,而下一口井生產10口井。我需要能夠生成一個宏,選擇單元中的所有數據,然後繪製該數據。目前,無論何時我選擇要繪製的列,excel都會嘗試繪製整個列的圖形,而不是僅繪製數據的範圍,從而導致空間占主導地位的非常大的圖形。列J需要是X軸,列L需要是Y軸。列J中有文本和數字,列L只有數字

此外,我希望宏使用工作表名稱和我將輸入的字符串來爲圖表生成名稱。所有制作的圖表中的字符串都是相同的。我希望圖表符合標記圖表。

命名過程的一個例子就是走這樣的事情

工作表名稱

含油率下降

下面是我到目前爲止所生成的代碼:

Sub automated_graphs() 
' 
' automated_graphs Macro 
' 

' 
    Range("L:L,J:J").Select 
    Range("J1").Activate 
    ActiveSheet.Shapes.AddChart.Select 
    ActiveChart.ChartType = xlLineMarkers 
    ActiveChart.SetSourceData Source:=Range(_ 
     "'EP Allen 1'!$L:$L,'EP Allen 1'!$J:$J") 
    ActiveChart.Axes(xlCategory).Select 
    ActiveChart.SeriesCollection(1).Delete 
    ActiveChart.SeriesCollection(1).XValues = "='EP Allen 1'!$J:$J" 
    ActiveChart.ChartTitle.Select 
    ActiveChart.ChartTitle.Text = "Worksheet Name here" & Chr(13) & "Oil Production by year" 
    Selection.Format.TextFrame2.TextRange.Characters.Text = _ 
     "Worksheet Name here" & Chr(13) & "Oil Production by year" 
    With Selection.Format.TextFrame2.TextRange.Characters(1, 20).ParagraphFormat 
     .TextDirection = msoTextDirectionLeftToRight 
     .Alignment = msoAlignCenter 
    End With 
    With Selection.Format.TextFrame2.TextRange.Characters(1, 20).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 
    With Selection.Format.TextFrame2.TextRange.Characters(21, 22).ParagraphFormat 
     .TextDirection = msoTextDirectionLeftToRight 
     .Alignment = msoAlignCenter 
    End With 
    With Selection.Format.TextFrame2.TextRange.Characters(21, 3).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 
    With Selection.Format.TextFrame2.TextRange.Characters(24, 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 = 18 
     .Italic = msoFalse 
     .Kerning = 12 
     .Name = "+mn-lt" 
     .UnderlineStyle = msoNoUnderline 
     .Strike = msoNoStrike 
    End With 
    ActiveChart.SeriesCollection(1).Select 
    With Selection.Format.Fill 
     .Visible = msoTrue 
     .ForeColor.RGB = RGB(0, 176, 80) 
     .Transparency = 0 
     .Solid 
    End With 
    With Selection.Format.Line 
     .Visible = msoTrue 
     .ForeColor.RGB = RGB(0, 176, 80) 
     .Transparency = 0 
    End With 
End Sub 

謝謝你並請讓我知道我是否可以提供任何說明

+0

作爲一個方面說明,我知道我沒有張貼代碼的權利,但我無法弄清楚如何格式化代碼才能在本網站上正確顯示。我遵照指示,但似乎無法使其工作。任何建議在這裏也不勝感激 –

+0

在您用來撰寫問題或答案的文本編輯器中,有一個用於嵌入代碼的按鈕。它在引號的右側。只需將代碼粘貼到編輯器中,突出顯示所有內容,然後單擊該按鈕即可在大多數情況下使用。有時您可能不得不在段落中斷之前結束行[ENTER]。參觀。 http://stackoverflow.com/tour – peege

回答

0

像這樣的東西應該工作

Sub DoChart() 

Dim sht As Worksheet 
Dim xVals As Range, yVals As Range 
Dim co As Shape, cht As Chart, s As Series 

    Set sht = ActiveSheet 
    Set co = sht.Shapes.AddChart() 
    Set cht = co.Chart 

    'remove any existing series 
    Do While cht.SeriesCollection.Count > 0 
     cht.SeriesCollection(1).Delete 
    Loop 

    cht.ChartType = xlLineMarkers 

    'get the extent of the XValues... 
    Set xVals = sht.Range(sht.Range("J2"), sht.Cells(Rows.Count, "J").End(xlUp)) 
    Set yVals = xVals.Offset(0, 2) 

    Set s = cht.SeriesCollection.NewSeries 
    s.XValues = xVals 
    s.Values = yVals 

    With s.Format.Fill 
     .Visible = msoTrue 
     .ForeColor.RGB = RGB(0, 176, 80) 
     .Transparency = 0 
     .Solid 
    End With 
    With s.Format.Line 
     .Visible = msoTrue 
     .ForeColor.RGB = RGB(0, 176, 80) 
     .Transparency = 0 
    End With 

    cht.HasLegend = False 

    cht.SetElement (msoElementPrimaryCategoryAxisTitleAdjacentToAxis) 
    cht.Axes(xlCategory, xlPrimary).AxisTitle.Text = "Year" 

    cht.SetElement (msoElementPrimaryValueAxisTitleRotated) 
    cht.Axes(xlValue, xlPrimary).AxisTitle.Text = "Production" 

    cht.SetElement (msoElementChartTitleCenteredOverlay) 
    With cht.ChartTitle 
     .Text = sht.Name & Chr(10) & "Oil Production by year" 
     .Characters.Font.Size = 12 
    End With 

End Sub 
+0

蒂姆,非常感謝你的代碼。它工作得很好!我有一個後續問題。我怎麼能自動添加數據標籤到圖表?我怎麼能改變編碼,在Y軸上包含多個系列? –

0

添的代碼開裂。我學到了很多。約西亞,因爲你出現新的節目,因爲我讀添的代碼我已經添加了一些變化之下,你可能會從中受益:

Set xVals = sht.Range(sht.Range("J2"), sht.Cells(SHT.Rows.Count, "J").End(xlUp)) 
' Added SHT for completeness. 

Set yVals = xVals.Offset(0, 2) 
' THE ABOVE LINE WORKS BUT REPLACING WITH THE LINE BELOW MAKES IT MORE ROBUST AS 
' YOU SIMPLY CHANGE "J" to be "X" if the data moves from column J to column X. 

Set yVals = intersect(xVals.entirerow, sht.columns("J"))