我有兴趣为折线图选择彼此不相邻的行。下图显示了所需的选择。
我还想要选择相对于内容为ABCDEF的单元格。
到目前为止,我有以下代码,但它只选择一行。
Sub PlotTheCharts()
'
' PlotTheCharts Macro
'
'
Set bookName = Range("A2") ' ABCDEF cell location
Range(bookName.Offset(0, 1), bookName.Offset(0, 1).End(xlToRight)).Select
bookName.Offset(2, 1).Activate
Range(Selection, Selection.End(xlToRight)).Select
End Sub
数据如下:
ABCDEF label Mon1 Mon2 Mon3 Mon4 Mon5 Mon6 Mon7 Mon8 Mon9 Mon10 Mon11 Mon12
Total Animals 1800 1800 1800 1800 1800 1800 1800 1800 1800 1800 1800 1800
Test Product 44.69 16.76 66.19 8.29 51.8 53.16 81.44 99.52 29.79 88.4 60.91 24.52
Test Competition 30.87 59.96 68.22 58.65 5.07 34.16 70.8 33.32 98.42 80.76 72.92 31.82
Test Market 28.34 47.48 13.44 61.55 41.39 70.04 99.37 70.65 32.38 66.7 9.53 33.73
Control Product 16.49 64.71 49.35 2.97 82.08 30.46 40.21 76.17 71.71 28.74 55.43 18.09
我正在添加另一张图片,试图进一步解释我想要做的事情。
答案 0 :(得分:1)
以下代码将根据ABCDEFG
的位置将所需范围加载到范围对象中。只要数据结构保持不变,这将起作用。如果它会有所不同,则需要另一种解决方案。
Sub PlotTheCharts()
Dim DataSheet as Worksheet
Set DataSheet = Worksheets("myData") 'change as needed
With Datasheet
Dim RelativeCell as Range
Set RelativeCell = .Range("A2")
Dim TotalColumns as Long
TotalColumns = RelativeCell.End(xlToRight).Column
Dim AxisRange as Range
Set AxisRange = RelativeCell.Offset(,1).Resize(1,TotalColumns)
Dim YPlotRange as Range
Set YPlotRange = RelativeCell.Offset(2,1).Resize(1,TotalColumns)
Dim XPlotRange as Range
Set YPlotRange = RelativeCell.Offset(5,1).Resize(1,TotalColumns)
'load ranges into chart ....
End With
End Sub
答案 1 :(得分:1)
您的意思是折线图,而不是散点图。
Sub Macro1()
Dim r As Range, n As Long, s As String
Set r = Sheet1.Columns(1).Find(What:="ABCDEF", LookAt:=xlWhole, MatchCase:=False, SearchFormat:=False)
If Not r Is Nothing Then
n =r.CurrentRegion.Columns.Count - 1
s = r.Offset(, 1).Resize(, n).Address & "," & _
r.Offset(2, 1).Resize(, n).Address & "," & _
r.Offset(5, 1).Resize(, n).Address
ActiveSheet.Shapes.AddChart.Select
ActiveChart.SetSourceData Source:=Range(s)
ActiveChart.ChartType = xlLine
End If
End Sub