VBA 动态设置x轴交叉的代码
VBA code to set x axis crossover dynamically
我在使用这段 VBA 代码时遇到问题:我希望 x 轴交叉点是 A 列中的最后一个单元格值(数据从 A2 ......开始),这样每次散点图数据扩展,然后 x 轴交叉重置为 A 列中最后一个单元格的值。在 rhe
这是我的 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
这应该是您要查找的内容:
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
如果您将 sheet 的名称更改为 运行 除了 Sheet1 以外的任何名称,请确保将代码中显示 Sheets("Sheet1") 的任何地方更改为新的 sheet 名称。
我在使用这段 VBA 代码时遇到问题:我希望 x 轴交叉点是 A 列中的最后一个单元格值(数据从 A2 ......开始),这样每次散点图数据扩展,然后 x 轴交叉重置为 A 列中最后一个单元格的值。在 rhe
这是我的 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
这应该是您要查找的内容:
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
如果您将 sheet 的名称更改为 运行 除了 Sheet1 以外的任何名称,请确保将代码中显示 Sheets("Sheet1") 的任何地方更改为新的 sheet 名称。