动态 select 个单元格并在图表中输入
Dynamically select cells and input in chart
我想创建一个 运行 通过 table 中的一系列数据并能够自动绘制图表的宏。问题是,数据可能有更多或更少的点,但代码仍然需要能够 select 并绘制此所需数据。
这是我正在使用的(下方)。我希望能够在同一图表上绘制每个点号(在点号列下)的垂直坐标与垂直位移的关系图。如您所见,有四个不同的点号 (1、2、3、4),每个点号迭代 9 次。但是,这些数字可以更改(例如,可能有 8 个点编号,每个点编号有 3 次迭代)并且代码只需要能够 select 基于点编号值本身的数据。
这是我希望使用此代码生成的图表示例:
由于我是 VBA 的新手,我仍然没有完全掌握语法,但这是我的想法(有些不是 VBA 语言):
Sub CreateChart()
Dim x as Range
Range("C8").Select
Range(Selection, Selection.End(xlDown)).Select 'selects whole column which will always start from cell C8
For each x in selection
'Select the columns of *Vertical Coordinate* and *Vertical Displacement* corresponding to Point No. 1
'Graph the relationship as a new series in a scatterplot
x = x+1
Next x
End Sub
我知道这是完全不正确的语法,但此时我对这门语言的掌握仍然非常有限。任何帮助表示赞赏!谢谢。
--------------------------------!!!!!!!!!!!!! 编辑 !!!!!!!!!!!!!!!!!---------------------
对于原始案例中的场景,我收到了来自@Viktor 的很好的回应,但我想知道是否有任何方法可以修改代码以获得更具挑战性的代码(以及超出我理解范围的代码) ):
我在我的 table 中添加了更多的列(见下文)并希望代码创建一个额外的图表来绘制*垂直坐标与垂直应力”,同时仍然保持图表来自Vertical Coordinate vs. Vertical Displacement. 当前代码不满足这一点的原因是因为它假定 sheet 上没有其他数据,其中 [=45] =] 是(但有)。我希望能够添加更多的列并创建更多的图表(所有图表都是针对垂直坐标绘制的)而不影响其他图表。如果有任何方法可以修改代码,那将不胜感激!谢谢。
.com/GYsZo.png
实际上我认为使用公式 + 命名范围更容易完成任务,但编写代码是一个挑战和学习机会。我希望它对你有用。
我也试着评论它以便更好地理解。
Sub MakeXYGraph()
'
Dim ws As Worksheet
Set ws = Sheet1 'This is the codename of the sheet where the data is
'For the test, deleting all the previous charts
Dim vChartObject As ChartObject
For Each vChartObject In ws.ChartObjects
vChartObject.Delete
Next vChartObject
'rngData is the range where the data are. It is assumed that nothing else is on the sheet than what you displ
Dim rngData As Range
Set rngData = ws.UsedRange.Offset(1).Resize(ws.UsedRange.Rows.Count - 1)
' Get the number of series
Dim iMaxSeries As Integer
iMaxSeries = Application.WorksheetFunction.Max(rngData.Columns(1))
' Is the actual Series, but in the sheet it called Point
Dim iPoint As Integer
'Used for setting the ranges for the series data
Dim lFirstRow As Long, lLastRow As Long, lFirstColumn As Long, lLastColumn As Long
lFirstColumn = rngData(1).Column
lLastColumn = rngData.Columns(rngData.Columns.Count).Column
'Creating the Chart
Dim cht As ChartObject
Set cht = ws.ChartObjects.Add(Left:=250, Width:=500, Top:=50, Height:=300)
With cht.Chart
.ChartType = xlXYScatterLines
'X axis name
.Axes(xlCategory, xlPrimary).HasTitle = True
.Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "Vertical Displacement"
'Y-axis name
.Axes(xlValue, xlPrimary).HasTitle = True
.Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "Vertical Coordinate"
' deleting the unwanted series (Excel tries to find out the data, but no need for it.)
Do Until .SeriesCollection.Count = 0
.SeriesCollection(1).Delete
Loop
End With
For iPoint = 1 To iMaxSeries
'Search for the first occurence of the point
lFirstRow = rngData.Columns(1).Offset(-1).Find(what:=iPoint).Row
'Search for the first occurence of the second point -1 is the last of this point
If iPoint = iMaxSeries Then
lLastRow = rngData.Rows(rngData.Rows.Count).Row - 1
Else
lLastRow = rngData.Columns(1).Find(what:=iPoint + 1).Row - 1
End If
'Add the series
With cht.Chart.SeriesCollection.NewSeries
.XValues = ws.Range(Cells(lFirstRow, lFirstColumn + 1), Cells(lLastRow, lLastColumn - 1))
.Values = ws.Range(Cells(lFirstRow, lFirstColumn + 2), Cells(lLastRow, lLastColumn))
.Name = "Point " & CStr(iPoint)
End With
Next iPoint
End Sub
我想创建一个 运行 通过 table 中的一系列数据并能够自动绘制图表的宏。问题是,数据可能有更多或更少的点,但代码仍然需要能够 select 并绘制此所需数据。
这是我正在使用的(下方)。我希望能够在同一图表上绘制每个点号(在点号列下)的垂直坐标与垂直位移的关系图。如您所见,有四个不同的点号 (1、2、3、4),每个点号迭代 9 次。但是,这些数字可以更改(例如,可能有 8 个点编号,每个点编号有 3 次迭代)并且代码只需要能够 select 基于点编号值本身的数据。
由于我是 VBA 的新手,我仍然没有完全掌握语法,但这是我的想法(有些不是 VBA 语言):
Sub CreateChart()
Dim x as Range
Range("C8").Select
Range(Selection, Selection.End(xlDown)).Select 'selects whole column which will always start from cell C8
For each x in selection
'Select the columns of *Vertical Coordinate* and *Vertical Displacement* corresponding to Point No. 1
'Graph the relationship as a new series in a scatterplot
x = x+1
Next x
End Sub
我知道这是完全不正确的语法,但此时我对这门语言的掌握仍然非常有限。任何帮助表示赞赏!谢谢。
--------------------------------!!!!!!!!!!!!! 编辑 !!!!!!!!!!!!!!!!!---------------------
对于原始案例中的场景,我收到了来自@Viktor 的很好的回应,但我想知道是否有任何方法可以修改代码以获得更具挑战性的代码(以及超出我理解范围的代码) ):
我在我的 table 中添加了更多的列(见下文)并希望代码创建一个额外的图表来绘制*垂直坐标与垂直应力”,同时仍然保持图表来自Vertical Coordinate vs. Vertical Displacement. 当前代码不满足这一点的原因是因为它假定 sheet 上没有其他数据,其中 [=45] =] 是(但有)。我希望能够添加更多的列并创建更多的图表(所有图表都是针对垂直坐标绘制的)而不影响其他图表。如果有任何方法可以修改代码,那将不胜感激!谢谢。
实际上我认为使用公式 + 命名范围更容易完成任务,但编写代码是一个挑战和学习机会。我希望它对你有用。 我也试着评论它以便更好地理解。
Sub MakeXYGraph()
'
Dim ws As Worksheet
Set ws = Sheet1 'This is the codename of the sheet where the data is
'For the test, deleting all the previous charts
Dim vChartObject As ChartObject
For Each vChartObject In ws.ChartObjects
vChartObject.Delete
Next vChartObject
'rngData is the range where the data are. It is assumed that nothing else is on the sheet than what you displ
Dim rngData As Range
Set rngData = ws.UsedRange.Offset(1).Resize(ws.UsedRange.Rows.Count - 1)
' Get the number of series
Dim iMaxSeries As Integer
iMaxSeries = Application.WorksheetFunction.Max(rngData.Columns(1))
' Is the actual Series, but in the sheet it called Point
Dim iPoint As Integer
'Used for setting the ranges for the series data
Dim lFirstRow As Long, lLastRow As Long, lFirstColumn As Long, lLastColumn As Long
lFirstColumn = rngData(1).Column
lLastColumn = rngData.Columns(rngData.Columns.Count).Column
'Creating the Chart
Dim cht As ChartObject
Set cht = ws.ChartObjects.Add(Left:=250, Width:=500, Top:=50, Height:=300)
With cht.Chart
.ChartType = xlXYScatterLines
'X axis name
.Axes(xlCategory, xlPrimary).HasTitle = True
.Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "Vertical Displacement"
'Y-axis name
.Axes(xlValue, xlPrimary).HasTitle = True
.Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "Vertical Coordinate"
' deleting the unwanted series (Excel tries to find out the data, but no need for it.)
Do Until .SeriesCollection.Count = 0
.SeriesCollection(1).Delete
Loop
End With
For iPoint = 1 To iMaxSeries
'Search for the first occurence of the point
lFirstRow = rngData.Columns(1).Offset(-1).Find(what:=iPoint).Row
'Search for the first occurence of the second point -1 is the last of this point
If iPoint = iMaxSeries Then
lLastRow = rngData.Rows(rngData.Rows.Count).Row - 1
Else
lLastRow = rngData.Columns(1).Find(what:=iPoint + 1).Row - 1
End If
'Add the series
With cht.Chart.SeriesCollection.NewSeries
.XValues = ws.Range(Cells(lFirstRow, lFirstColumn + 1), Cells(lLastRow, lLastColumn - 1))
.Values = ws.Range(Cells(lFirstRow, lFirstColumn + 2), Cells(lLastRow, lLastColumn))
.Name = "Point " & CStr(iPoint)
End With
Next iPoint
End Sub