类型不匹配错误 - 图表数据

时间:2015-06-29 14:59:35

标签: vba compiler-errors type-mismatch

Sub Button2_Click()

Dim LastRow As Long
Dim xTitle As Range
Dim xData As Range
Dim yColumn As Long
Dim yTitle As Range
Dim yData As Range
Dim GraphRange As Range


'Find last row with data
LastRow = ActiveSheet.Cells(ActiveSheet.Rows.Count, "A").End(xlUp).Row

'Set x-axis title and data
Set xTitle = Range("A1")
Set xData = Range("A2" & LastRow)


 'Find Cap, set y-axis title

= Range("B1")


'set y-axis data
**yData = Range("B2" & LastRow)**

'set total graph range
Set GraphRange = Union(xTitle, xData, yTitle, yData)

'create chart
 ActiveSheet.Shapes.AddChart.Select
 ActiveChart.ChartType = xlXYScatter
 ActiveChart.SetSourceData Source:=GraphRange
 ActiveChart.Location Where:=xlLocationAsNewSheet
 ActiveChart.SetElement (msoElementLegendNone)
 ActiveChart.ChartTitle.Text = "MM Index"
 ActiveChart.SetElement (msoElementPrimaryCategoryAxisTitleAdjacentToAxis)
 Selection.Caption = xTitle
 ActiveChart.SetElement (msoElementPrimaryValueAxisTitleRotated)
 Selection.Caption = yTitle

Application.ScreenUpdating = True



End Sub

嗨,我试图绘制一系列数据,X轴来自col A,Y轴来自B. 我不能停止因类型不匹配而收到编译错误。有人可以请解释导致此错误的原因以及如何避免此错误。

谢谢。

1 个答案:

答案 0 :(得分:0)

您的xData和yData设置不正确。替换

Set xData = Range("A2" & LastRow) 
Set yData = Range("B2" & LastRow)

通过

Set xData = Range("A2:A" & LastRow)
Set yData = Range("B2:B" & LastRow)