使用相对范围在excel上绘图

所以我目前有一段代码只能在我的工作簿中的一个工作表上运行。 我想修改它,以便它可以在任何工作表上工作。

macros本质上是使用相对范围来绘制数据。 我试图自己解决它,但似乎无法得到它的工作,请参阅“错误1”。 我还碰到了第二个错误,当它在一张纸上工作时,它正在绘制一个额外的“系列”,我将在下面附上一个图像 – 参考错误2

Sub ChartTHIS() 'get offset location from active cell to new chart position Dim Col, Row Col = Split(ActiveCell(1).Offset(0, 9).Address(1, 0), "$")(0) 'nine right of active col Row = Split(ActiveCell(1).Offset(2, 0).Address(1, 0), "$")(1) 'two below active row 'MsgBox Col & " " & Row 'this row,col is used to position the chart 'example chart data range from 21-g-1028 '='21-G-1028'!$K$8:$O$8,'21-G-1028'!$K$10:$O$11,'21-G-1028'!$K$31:$O$31 'MsgBox ActiveChart.Parent.Index Dim rngSourceData As Range, wsData As Worksheet, wsChart As Worksheet Set wsData = Sheets(ActiveSheet.Name) Set wsChart = Sheets(ActiveSheet.Name) 'Set rngSourceData = Union(wsData.Range("K10:O10"), wsData.Range("K13:O13")) Set rngSourceData = wsData.Range("'21-G-1028'!$K$8:$O$8,'21-G-1028'!$K$10:$O$11,'21-G-1028'!$K$31:$O$31") 'declare a ChartObject Dim oChObj As ChartObject 'the Add method (of the ChartObjects object) is used to create a new empty embedded 'chart and add it to the collection (ChartObjects object) in the specified sheet Set oChObj = wsChart.ChartObjects.Add(Left:=2, Width:=350, Top:=5, Height:=200) 'using the Chart Property of the ChartObject object returns a Chart object 'which refers to a chart With oChObj.Chart 'use ChartType Property of the Chart object to set type of chart - Line with Markers .ChartType = xlLineMarkers 'use SetSourceData Method of the Chart object to set the range of source data for the chart .SetSourceData Source:=rngSourceData, PlotBy:=xlRows 'the Parent property of the Chart object returns its Parent object ie. ChartObject object (oChObj) 'the ChartTitle object exists and can be used only if the HasTitle property (of the Chart object) is True .HasTitle = True 'get sheet name into chart title: .ChartTitle.Characters.Text = ActiveSheet.Name & " STD Wear Trend" 'calc position of chart relative to active cell Col = Split(ActiveCell(1).Offset(0, 9).Address(1, 0), "$")(0) 'nine right of active col Row = Split(ActiveCell(1).Offset(2, 0).Address(1, 0), "$")(1) 'two below active row 'here use the calculated row and col offset to position the chart: With .Parent 'set the embedded chart to be free-floating so that it does not move or size with its underlying cells .Placement = xlFreeFloating 'align the left edge of the embedded chart with the left edge of the worksheet's column B .Left = wsChart.Columns(Col).Left .Top = wsChart.Rows(VBA.Val(Row)).Top 'set rounded corners for the embedded chart .RoundedCorners = True 'using the Name Property of the ChartObject object - set the name of the embedded chart to "AnnualSalesProfit" End With .Axes(xlCategory, xlPrimary).HasTitle = True .Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "Time (hours)" .Axes(xlCategory, xlPrimary).CategoryType = xlTimeScale '.Axes(xlCategory, xlPrimary).MinimumScaleIsAuto = True '.Axes(xlCategory, xlPrimary).MaximumScaleIsAuto = True .Axes(xlValue, xlPrimary).HasTitle = True .Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "Wear (mm)" .Axes(xlValue).MinimumScale = 0 .Axes(xlCategory).MinimumScale = 0 .Axes(xlCategory).MaximumScale = 4000 .Axes(xlCategory).MajorUnit = 1000 .Axes(xlCategory).MinorUnit = 1000 .Axes(xlCategory, xlPrimary).TickLabels.NumberFormat = "0" .SeriesCollection.NewSeries Dim col2, row2 'data series 1 ----- HOW TO CODE RELATIVE DATA RANGES INTO THE CHART MACRO 'example active K8 to Q8 Col = Split(ActiveCell(1).Offset(0, 0).Address(1, 0), "$")(0) 'nine right of active col 'this row will always be used as the x axis labels Row = Split(ActiveCell(1).Offset(0, 0).Address(1, 0), "$")(1) 'two below active row 'and K10 to Q10 col2 = Split(ActiveCell(1).Offset(0, 6).Address(1, 0), "$")(0) 'nine right of active col .SeriesCollection(1).XValues = Range("$" & Col & "$" & Row & ":$" & col2 & "$" & Row) row2 = Split(ActiveCell(1).Offset(2, 0).Address(1, 0), "$")(1) 'two below active row .SeriesCollection(1).Values = Range("$" & Col & "$" & row2 & ":$" & col2 & "$" & row2) .SeriesCollection(1).Name = "zone 0" 'data series 2 ----- 'only the row changes relatively row2 = Split(ActiveCell(1).Offset(3, 0).Address(1, 0), "$")(1) 'two below active row .SeriesCollection(2).XValues = Range("$" & Col & "$" & Row & ":$" & col2 & "$" & Row) 'next row down: .SeriesCollection(2).Values = Range("$" & Col & "$" & row2 & ":$" & col2 & "$" & row2) .SeriesCollection(2).Name = "zone 1" 'data series 3 ----- 'so you only need to change the row offset and then the other lines are the same: 'see this one has a long offset of 23 row2 = Split(ActiveCell(1).Offset(23, 0).Address(1, 0), "$")(1) 'two below active row .SeriesCollection(3).XValues = Range("$" & Col & "$" & Row & ":$" & col2 & "$" & Row) .SeriesCollection(3).Values = Range("$" & Col & "$" & row2 & ":$" & col2 & "$" & row2) .SeriesCollection(3).Name = "zone 12" 'and you can add as many zones of data as you want... 'using constant values in MsoChartElementType Enumeration to specify if the chart elements are to be displayed and how to display them 'Display chart title above chart .SetElement msoElementChartTitleAboveChart 'Display major gridlines along primary value axis .SetElement msoElementPrimaryValueGridLinesMajor 'turn off legend '.SetElement msoElementLegendRight End With End Sub 

我认为需要修改的那段代码是: Error 1

 'Set rngSourceData = Union(wsData.Range("K10:O10"), wsData.Range("K13:O13")) Set rngSourceData = wsData.Range("'21-G-1028'!$K$8:$O$8,'21-G-1028'!$K$10:$O$11,'21-G-1028'!$K$31:$O$31") 

我得到的第二个错误是,在其中绘制一个额外的“系列”,我不知道如何解决这个问题。 错误2