Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
excelvba中带变量系列的图表_Excel_Vba_Charts - Fatal编程技术网

excelvba中带变量系列的图表

excelvba中带变量系列的图表,excel,vba,charts,Excel,Vba,Charts,我是VBA和这个论坛的新手。我有一个表,其中日期作为第一列(x列)和12列数据(y值)。我试图用一个简单的xlLine图表来绘制数据。仅为y值绘制少数选定列。使用列顶部的组合框选择列。行数是可变的 我正在使用此代码,但它不起作用。有人能告诉我什么地方出了问题,并修复它吗?谢谢你的帮助。提前谢谢 Sub drawchart1() ' ' drawchart1 Macro ' ' Dim i As Integer Dim j As Integer Dim n As Inte

我是VBA和这个论坛的新手。我有一个表,其中日期作为第一列(x列)和12列数据(y值)。我试图用一个简单的xlLine图表来绘制数据。仅为y值绘制少数选定列。使用列顶部的组合框选择列。行数是可变的

我正在使用此代码,但它不起作用。有人能告诉我什么地方出了问题,并修复它吗?谢谢你的帮助。提前谢谢

Sub drawchart1()
'
' drawchart1 Macro
'

'
    Dim i As Integer
    Dim j As Integer
    Dim n As Integer

    ' finding the number of rows

    j = Range("Charts!A1").Offset(Sheet1.Rows.Count - 1, 0).End(xlUp).Row

    ' selecting some range and adding a chart which is then modified.(not sure this is the correct method.)

    Range("A10:C15").Select
    ActiveSheet.Shapes.AddChart.Select
    ActiveChart.ChartType = xlLine

    i = 2
    n = 2

    ' Cells (9,1) contains the value "Date". Defining the X Axis values

    ActiveChart.SeriesCollection(1).Name = Sheets("Charts").Cells(9, 1).Value
    ActiveChart.SeriesCollection(1).XValues = "=Charts!R10C1:R" & j & "C1"

    Do While i < 14

    ' Cells(8,i) contain the results of combo box - true or false. 
    ' Cells(9,i) contain the names of the series

    If Cells(8, i).Value = True Then
        ActiveChart.SeriesCollection(n).Name = Sheets("Charts").Cells(9, i).Value
        ActiveChart.SeriesCollection(n).Values = "=Charts!R10C" & i & ":R" & j & "C" & i
        n = n + 1
        i = i + 1
    Else
        i = i + 1
    End If
    Loop

End Sub
Sub drawchart()

Dim j As Integer
Dim Chartstring As String

j = Range("Charts!A1").Offset(Sheet1.Rows.Count - 1, 0).End(xlUp).Row

Chartstring = "A9:A" & j

If Cells(8, 2).Value = True Then
    Chartstring = Chartstring & ", B9:B" & j
Else
    Chartstring = Chartstring
End If

If Cells(8, 3).Value = True Then
    Chartstring = Chartstring & ", C9:C" & j
Else
    Chartstring = Chartstring
End If

' And similarly added code for each of the 14 columns

' And finally fed the chartstring into the "Source"

ActiveSheet.Shapes.AddChart.Select
ActiveChart.ChartType = xlLine
ActiveChart.SetSourceData Source:=Range(Chartstring)

End Sub

也许你不再看了。这里有另一种方法

Sub DrawChart1()
  Dim i As Long
  Dim j As Long
  Dim ws As Worksheet
  Dim rCht As Range, rYVals As Range
  Dim cht As Chart

  ' finding the number of rows
  Set ws = Worksheets("Charts")
  j = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row

  ' start with X values (row 10 to j), include header row (row 9)
  Set rCht = ws.Range(ws.Cells(9, 1), ws.Cells(j, 1))

  ' add column of Y values if row 8 of column is TRUE
  For i = 2 To 14
    If ws.Cells(8, i).Value Then
      Set rYVals = ws.Range(ws.Cells(9, i), ws.Cells(j, i))
      Set rCht = Union(rCht, rYVals)
    End If
  Next

  ' if we've had any Y values, insert chart, using range we've built up
  If Not rYVals Is Nothing Then
    Set cht = ws.Shapes.AddChart(xlLine).Chart
    cht.SetSourceData Source:=rCht, PlotBy:=xlColumns
  End If
End Sub