简体   繁体   English

基于单元格中的值的图表范围

[英]Chart range based on the value in a cell

I need to create a line chart that selects a range of data based on the value in a cell.我需要创建一个折线图,根据单元格中的值选择一系列数据。 For instance, in cell C1 I write A1:B4, this means the chart is a display of the data in cells A1 to B4.例如,在单元格 C1 中我写了 A1:B4,这意味着图表是单元格 A1 到 B4 中数据的显示。 If I simply change the value in cell C1 to A1:B9, I want the chart to display the data of this range - you get the point.如果我只是将单元格 C1 中的值更改为 A1:B9,我希望图表显示此范围的数据 - 你明白了。 This shouldn't be too hard, but i'm not getting it right (and for some reason the web is full of the same examples that do not apply for my)这应该不会太难,但我没有做对(出于某种原因,web 充满了不适用于我的相同示例)

I've tried using a Named Range function.我试过使用Named Range function。 I still think this is the way to go, but I need some help.我仍然认为这是通往 go 的方法,但我需要一些帮助。

There is no VBA needed for this.为此不需要VBA

Let's start having the following worksheet named Sheet1 :让我们开始使用以下名为Sheet1的工作表:

在此处输入图像描述

Now we need three named ranges.现在我们需要三个命名范围。 One for the whole range which we get indirect form C1 , one for the categories which is the left column of the whole range and one for the values which is the right column of the whole range.一个用于我们得到间接形式C1的整个范围,一个用于整个范围的左列的类别,一个用于整个范围的右列的值。

So in name manager we create following named ranges:因此,在名称管理器中,我们创建以下命名范围:

在此处输入图像描述

Note all named ranges are in scope of the sheet Sheet1 and not in workbook scope.请注意,所有命名范围都在工作表Sheet1的 scope 中,而不是在工作簿 scope 中。 So while creating the named ranges, always choose scope Sheet1 instead of Workbook因此,在创建命名范围时,请始终选择 scope Sheet1而不是Workbook

Name myRange refers to =INDIRECT(Sheet1!$C$1) .名称myRange指的是=INDIRECT(Sheet1!$C$1) So it gets it's range from that cell value.所以它从那个单元格值得到它的范围。

Name myCategories refers to =INDEX(Sheet1,myRange,,1) .名称myCategories指的是=INDEX(Sheet1,myRange,,1) That gets all rows (since no special row is given) from column 1 of myRange .myRange的第1列获取所有行(因为没有给出特殊行)。

Name myValues refers to =INDEX(Sheet1,myRange,,2) .名称myValues指的是=INDEX(Sheet1,myRange,,2) That gets all rows (since no special row is given) from column 2 of myRange .myRange的第2列获取所有行(因为没有给出特殊行)。

Now we can insert a chart (a pie chart for example).现在我们可以插入一个图表(例如饼图)。

Then we right-click the chart, and then choose Select Data .然后我们右键单击图表,然后选择Select Data

First we delete all present series on left side below Legend Entries (Series), if any.首先我们删除Legend Entries(系列)下方左侧的所有现有系列,如果有的话。 Then we add a new series.然后我们添加一个新系列。 In Series values: we put the formula =Sheet1!myValues , OK .在系列值中:我们输入公式=Sheet1!myValuesOK

On right side below Horizontal (Category) Axis Labels we click Edit and put in the formula =Sheet1!myCategories , OK .在水平(类别)轴标签下方的右侧,我们单击Edit并输入公式=Sheet1!myCategoriesOK

Then OK for the whole select-data-dialog.然后OK整个选择数据对话框。

Now if we change the cell value of C1 into something what INDIRECT can interpret as a cell range, then the chart will change too.现在,如果我们将C1的单元格值更改为INDIRECT可以解释为单元格范围的值,那么图表也会发生变化。


To give a VBA solution also:还要给出VBA解决方案:

Let's have the same sheet as above.让我们有与上面相同的工作表。 Data in A1:B8 and range address in C1 . A1:B8中的数据和C1中的范围地址。

Now create the wanted chart.现在创建想要的图表。 It must be the one and only chart object in that sheet.它必须是该表中唯一的图表 object。

Now put the following code in sheet module of Sheet1 (right click on the sheet tab and click View Code ):现在将以下代码放入Sheet1的工作表模块中(右键单击工作表选项卡并单击View Code ):

Private Sub Worksheet_Change(ByVal Target As Range)
 Dim oChartObject As ChartObject
 Dim oChart As Chart
 If Target.Row = 1 And Target.Column = 3 Then
  On Error Resume Next
  Set oChartObject = Me.ChartObjects(1)
  Set oChart = oChartObject.Chart
  oChart.SetSourceData Source:=Me.Range(Target.Value)
  On Error GoTo 0
 End If
End Sub

This code leads to changing of source data of the chart if the value of C1 (row 1, column 3) changes.如果C1 (第 1 行,第 3 列)的值发生变化,此代码会导致图表的源数据发生变化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM