在另一个工作表上添加数据指向图表

时间:2017-02-07 16:24:04

标签: excel vba data-manipulation

我正在尝试将数据点添加到我在Excel电子表格中的另一个选项卡上的图表上,目前它在您打开图表页面时添加但我需要在其他选项卡上添加时添加,任何建议?这是我的代码:

Sub Submit()
    Dim Well_Outage As Long
    Dim DP As Double
    Dim i As Long
    Dim j As Long
    Dim myDocument As Worksheet
    'Dim graph As Worksheet

    Set myDocument = Worksheets(2)
    Set graph = Worksheets
    j = ActiveChart.SeriesCollection.Count

    If myDocument.Cells(19, "I") = "" And myDocument.Cells(20, "I") = "" Then
        MsgBox ("Enter Drilling and Production Lost Times")
        Exit Sub
    ElseIf myDocument.Cells(19, "I") = "" Then
        MsgBox ("Enter Drilling Lost Times")
        Exit Sub
    ElseIf myDocument.Cells(20, "I") = "" Then
        MsgBox ("Enter Production Lost Times")
        Exit Sub
    End If

    i = 1
    Well_Outage = myDocument.Cells(17, "L")
    DP = (myDocument.Cells(19, "I") / myDocument.Cells(20, "I"))
    DP = Round(DP, 1)
    myDocument.Range("L6:L16").Value = "N"
    myDocument.Range("I19:I20").Value = ""

    Do While Not (myDocument.Cells(i, 1) = "")
        i = i + 1
    Loop

    Do While Not (myDocument.Cells(i, 2) = "")
        i = i + 1
    Loop

    myDocument.Cells(i, 1).Value = DP
    myDocument.Cells(i, 2).Value = Well_Outage

    ActiveChart.ChartArea.Select
    ActiveChart.SeriesCollection.NewSeries
    ActiveChart.FullSeriesCollection(j).XValues = (myDocument.Cells(i, 1))
    ActiveChart.FullSeriesCollection(j).values = (myDocument.Cells(i, 2))
    ActiveChart.ChartArea.Select
    ActiveChart.FullSeriesCollection(j).Select
    With Selection
        .MarkerStyle = 8
        .MarkerSize = 5
    End With
    Selection.MarkerStyle = -4168
    Selection.MarkerSize = 6
    Selection.MarkerSize = 7
    Selection.MarkerSize = 8
    Selection.MarkerSize = 9
    Selection.MarkerSize = 10
    With Selection.Format.line
        .Visible = msoTrue
        .weight = 2
    End With
    With Selection.Format.line
        .Visible = msoTrue
        .ForeColor.ObjectThemeColor = msoThemeColorText1
        .ForeColor.TintAndShade = 0
        .ForeColor.Brightness = 0
        .Transparency = 0
    End With
    With Selection.Format.Fill
        .Visible = msoTrue
        .ForeColor.ObjectThemeColor = msoThemeColorText1
        .ForeColor.TintAndShade = 0
        .Transparency = 0
        .Solid
    End With
    Selection.Format.Fill.Visible = msoFalse
End Sub

1 个答案:

答案 0 :(得分:0)

您应该在ActiveChart处替换:

ThisWorkbook.Charts("yourChartName")

或稍微简化代码,

Dim ch as Chart
Set ch = ThisWorkbook.Charts("yourChartName")

ActiveChart替换为ch到处