简体   繁体   English

Excel:如何格式化柱形图,以便在添加新数据时自动添加新列?

[英]Excel: How to format a column graph so that it automatically adds new columns when new data is added?

A bit of background: I have a table which by using a combination of vlookup and index, goes through my main dataset and pulls out each row of data for a given month (in this case March).一点背景:我有一个表,它通过使用 vlookup 和索引的组合,遍历我的主数据集并提取给定月份(在本例中为三月)的每一行数据。 Each month will have a maximum of 15 entries, but may have less.每个月最多有 15 个条目,但可能会更少。

The image below is an example of the table for the month of March, which currently has 4 entries.下图是三月份的表格示例,目前有 4 个条目。

在此处输入图片说明

I want to create a column graph which is sourced from this table, that formats in a way such that if there is:我想创建一个源自该表的柱形图,其格式设置为:

  • 4 entries, it shows 4 evenly spaced columns 4 个条目,它显示 4 个均匀间隔的列
  • 5 entries, it shows 5 evenly spaced columns 5 个条目,它显示 5 个均匀间隔的列
  • etc...等等...

However, when I create this graph using the two yellow highlighted columns of data, I get 4 columns (in the graph) squashed over to the left side, and a whole bunch of blank space, as seen below:但是,当我使用两个黄色突出显示的数据列创建此图表时,我将 4 列(在图表中)压扁到左侧,以及一大堆空白区域,如下所示:

在此处输入图片说明

Is there a way that I can create a graph that looks like the graph on the left, and that will add in new columns automatically as data appears in the table, without looking like the graph on the right?有没有一种方法可以创建一个看起来像左边的图形的图形,并且当数据出现在表格中时会自动添加新列,而不像右边的图形?

Thanks谢谢

You could automatically updated your graph data ranges using VBA.您可以使用 VBA 自动更新图形数据范围。

For example if your data range is in columns A and B of Sheet2 and your graph is Sheet1 you could use the following code:例如,如果您的数据范围在Sheet2 columns A and B ,而您的图形是Sheet1 ,则可以使用以下代码:

Sub UpdateRange()
Dim LastRow As Long
Dim RngCount as Range

Set RngCount = Sheets("Sheet2").Range("A1:A10")
LastRow  = Application.WorksheetFunction.CountA(RngCount)

Sheets("Sheet1").ChartObjects("Chart 1").Chart.SetSourceData Source:=Sheets("Sheet2").Range("A1:B" & LastRow)

End Sub

Graph with old data:旧数据图:

在此处输入图片说明 在此处输入图片说明

After data was added and the macro was run:添加数据并运行宏后:

在此处输入图片说明 在此处输入图片说明

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

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