多个散点图循环表示不同的X和Y值

时间:2017-07-17 21:53:55

标签: excel vba loops charts series

我试图使用多个系列(不同的X和Y值)为多个图表(循环)创建一个宏。对于一个sart我只使用了两个X和Y值系列,一切顺利,第一系列的X和Y值可以在这里看到:

第一个值范围:

VB:

Set myRange1 = Range(.Cells(c, 1), .Cells(c + 27, 1)) 
Set myRange2 = Range(.Cells(c, 2), .Cells(c + 27, 2))

http://i.imgur.com/O9FelLI.png

但是,我对下一个值范围得到了不同的结果。简而言之,出于某种原因,该系列被分为另外两个,不遵循我在此代码中定义的范围:

VB:

Set myRange3 = Range(.Cells(c, 3), .Cells(c + 27, 3)) 
Set myRange4 = Range(.Cells(c, 4), .Cells(c + 27, 4)) 

结果如下图所示: http://i.imgur.com/iT7vmDX.png

以下是我使用的代码:

Sub loopChart_M() 

    Dim myChart As Chart 
    Dim MultiRange1 As Range 
    Dim MultiRange2 As Range 
    Dim MultiRange As Range 
    Dim myRange1 As Range 
    Dim myRange2 As Range 
    Dim myRange3 As Range 
    Dim myRange4 As Range 
    Dim c As Integer 
    c = 1 


    While c <= 120 '1=dataSource1, 4=dataSource2, 7=dataSource3
         'set data source for the next chart
        With Worksheets("CM_AXIAL") 
            Set myRange1 = Range(.Cells(c, 1), .Cells(c + 27, 1)) 
            Set myRange2 = Range(.Cells(c, 2), .Cells(c + 27, 2)) 
            Set MultiRange1 = Union(myRange1, myRange2) 
            Set myRange3 = Range(.Cells(c, 3), .Cells(c + 27, 3)) 
            Set myRange4 = Range(.Cells(c, 4), .Cells(c + 27, 4)) 
            Set MultiRange2 = Union(myRange3, myRange4) 
            Set MultiRange = Application.Union(MultiRange1, MultiRange2) 

        End With 


         'create chart
        Sheets("CM_AXIAL").Select 
        ActiveSheet.Shapes.AddChart.Select 


        With ActiveChart 
            .ChartType = xlXYScatter 'xlLine
            .SetSourceData Source:=MultiRange, PlotBy:=xlColumns 'sets source data for graph including labels
             ' you can chage the columnd and rows ploting modes using ActiveChart.PlotBy = xlRows or  ActiveChart.PlotBy = xlColumns
            .SetElement (msoElementLegendRight) 'including legend
            .HasTitle = True 
             'dimentions & location:
            .Parent.Top = c * 14 'defines the coordinates of the top of the chart
            .Parent.Left = 0 'defines the coordinates for the left side of the chart
            .Parent.Height = 400 
            .Parent.Width = 429 
             '.ChartTitle.Text = "Title here"
             'ActiveChart.ChartArea.Select
             ' ActiveChart.FullSeriesCollection(1).Name = "=""CM"""
             'ActiveChart.FullSeriesCollection(2).Name = "=""Geometry"""







        End With 


        c = c + 29 




        With ActiveChart 


             'chart name
            .HasTitle = True 
            ActiveSheet.ChartObjects(1).Activate 



            .ChartTitle.Characters.Text = "Diagonal " ' & c
             'X axis name
            .Axes(xlCategory, xlPrimary).HasTitle = True 
            .Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "Element length (m)" 
             'y-axis name
            .Axes(xlValue, xlPrimary).HasTitle = True 
            .Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "Axial force(KN)" 
        End With 






    Wend 


End Sub 

0 个答案:

没有答案