我遇到了这个VBA代码的问题:我希望x轴交叉是A列中的最后一个单元格值(数据从A2开始......),这样每次散点图数据扩展时,然后将x轴交叉重置为列A中最后一个单元格的值
这是我的VBA代码(在{Selection.CrossesAt = SelRange}行有一个运行时错误):
Sub Dyna()
Dim SelRange As Range
'This code moves down column A to the end of the list.
' Select cell A2, *first line of data*.
Range("A2").Select
' Set Do loop to stop when an empty cell is reached.
Do Until IsEmpty(ActiveCell)
' Insert your code here.
' Step down 1 row from present location.
ActiveCell.Offset(1, 0).Select
Loop
Set SelRange = Selection
' Create a chart based on the sample source of data.
Charts.Add
With ActiveChart
.ChartType = xlLineMarkersStacked
.SetSourceData Source:=Sheets("Sheet1").Range("A2:A5"), PlotBy:=xlColumns
.Location Where:=xlLocationAsObject, Name:="Sheet1"
End With
' Set the category axis to cross the value axis at value 3.
ActiveChart.Axes(xlValue).Select
Selection.CrossesAt = SelRange
End Sub
答案 0 :(得分:0)
这应该是你要找的东西:
Sub Dyna()
Dim lngLastRow As Long
'This will find the last row of data in Column A
lngLastRow = Sheets("Sheet1").Cells(2, 1).End(xlDown).Row
Charts.Add
With ActiveChart
.ChartType = xlLineMarkersStacked
'This next line selects all data in column A, from A2 to the last row of column A
.SetSourceData Source:=Sheets("Sheet1").Range(Sheets("Sheet1").Cells(2, 1), Sheets("Sheet1").Cells(lngLastRow, 1)), PlotBy:=xlColumns
.Location Where:=xlLocationAsObject, Name:="Sheet1"
End With
ActiveChart.Axes(xlValue).Select
'This next line sets the axis crossover point equal to the last data point
Selection.CrossesAt = Sheets("Sheet1").Cells(lngLastRow, 1).Value
End Sub
如果您将运行此工作表的工作表的名称更改为Sheet1以外的任何其他工作,请确保将代码表单(" Sheet1")更改为新工作表名称。