Excel 2010 VBA绘图图macros

我需要绘制一组数据的线图。 我知道包含时间序列的列(C26到底部),我将知道调用它的button和数据以(26)开始的行的数据列。 标签将在第26行。button将在第24行。数据不包含空格。

图表需要有数据标签。这里有多远我有什么build议,请让我知道! 目前它只在时间轴上绘制1。

Sub GraphTest() Dim xaxis As Range Dim yaxis As Range Dim fullRange As Range Dim topcell As Range Set xaxis = Range("$B$26", Range("$B$26").End(xlDown)) Set yaxis = ActiveSheet.Buttons(Application.Caller).TopLeftCell Set yaxis = Range(Cells(yaxis.Row, yaxis.Column).Offset(2, 0), Cells(yaxis.Row, yaxis.Column).Offset(2, 0).End(xlDown)) Set topcell = ActiveSheet.Buttons(Application.Caller).TopLeftCell Set fullRange = Union(xaxis, yaxis) fullRange.Select topcell.Activate ActiveSheet.Shapes.AddChart.Select ActiveChart.ChartType = xlLine ActiveChart.SetSourceData Source:=fullRange End Sub() 

如果您单独创build和控制系列,而不是使用SetSourceData一次SetSourceData整个数据集,情况会变得更加容易。 例:

 Dim xaxis As Range Dim yaxis As Range Set xaxis = Range("$B$26", Range("$B$26").End(xlDown)) Set yaxis = Range("$C$26", Range("$C$26").End(xlDown)) Dim c As Chart Set c = ActiveWorkbook.Charts.Add Set c = c.Location(Where:=xlLocationAsObject, Name:="Sheet1") With c .ChartType = xlLine ' set other chart properties End With Dim s As Series Set s = c.SeriesCollection.NewSeries With s .Values = yaxis .XValues = xaxis ' set other series properties End With 

编辑

所谓的“折线图”并不总是像你期望的那样。 这是一个例子。 当时间在x轴上,格式为dd / mm / yyyy hh:mm时,折线图将所有共享同一date(date)的点强制到同一个文件夹中,而不pipe一天中的哪个时间。 他们为什么这样做? 我没有任何线索。 线图做各种其他疯狂的东西。

我很确定你想要的是散点图。 事实上,你想要的东西几乎总是一个散点图,几乎从来没有一个折线图。

这是一个例子。 我做了我自己的温度数据,但保留了你的date。

在这里输入图像描述

使用此代码生成:

 Dim xaxis As Range Dim yaxis As Range Set xaxis = Range("$B$26", Range("$B$26").End(xlDown)) Set yaxis = Range("$C$26", Range("$C$26").End(xlDown)) Dim c As Chart Set c = ActiveWorkbook.Charts.Add Set c = c.Location(Where:=xlLocationAsObject, Name:="Sheet1") With c .ChartType = xlXYScatterLines 'A scatter plot, not a line chart! ' set other chart properties End With Dim s As Series Set s = c.SeriesCollection.NewSeries With s .Values = yaxis .XValues = xaxis ' set other series properties End With With c.Axes(xlCategory) .MajorUnit = 0.125 End With With c .Axes(xlCategory, xlPrimary).HasTitle = True .Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "Time" .Axes(xlValue, xlPrimary).HasTitle = True .Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "Temperature" .HasLegend = False End With 

从来没有得到这个工作太好,但这是我必须解决的办法,

 Sub Graph() Dim xaxis As Range Dim yaxis As Range Dim temp As Range Dim total As Range Set yaxis = ActiveSheet.Buttons(Application.Caller).TopLeftCell yaxis.Select Set yaxis = Range(Cells(yaxis.Row, yaxis.Column), Cells(yaxis.Row, yaxis.Column)) yaxis.Select Set temp = yaxis yaxis.Offset(2, 0).Select ActiveCell.Activate ActiveCell.Select Do Until ActiveCell.Value = "" ActiveCell.Offset(1, 0).Activate Loop Set yaxis = Range(yaxis.Offset(2, 0), ActiveCell.Offset(-1, 0)) yaxis.Select Range("$b$26").Activate Do Until ActiveCell.Value = "" ActiveCell.Offset(1, 0).Activate Loop Set xaxis = Range("$b$26", ActiveCell.Offset(-1, 0)) xaxis.Select Set total = Union(xaxis, yaxis) total.Select temp.Offset(2, 0).Activate temp.Select ActiveSheet.Shapes.AddChart.Select ActiveChart.ChartType = xlXYScatterLines ActiveChart.SetSourceData Source:=total