【问题标题】:add data point to chart on another worksheet将数据点添加到另一个工作表上的图表
【发布时间】:2017-06-24 23:57:48
【问题描述】:

我正在尝试将数据点添加到我在 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

【问题讨论】:

    标签: excel vba data-manipulation


    【解决方案1】:

    您应该将ActiveChart 替换为:

    ThisWorkbook.Charts("yourChartName")
    

    或者为了简化代码,

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

    并在任何地方将ActiveChart 替换为ch

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-12
      • 2010-11-22
      • 1970-01-01
      • 1970-01-01
      • 2012-09-13
      • 1970-01-01
      相关资源
      最近更新 更多