[英]Excel VBA - Insert chart from dynamic range
I've got a vba application that creates a new sheet and inserts a variable number of values into the first two columns below a header (column A and B will always have a corresponding value, I just don't know how many rows there will be): 我有一个vba应用程序,该应用程序创建一个新工作表并将可变数量的值插入到标题下方的前两列中(A和B列始终具有对应的值,我只是不知道会有多少行是):
I'd like to use vba to embed a simple bar graph a few columns over in the same sheet that looks kind of like this: 我想使用vba将一个简单的条形图嵌入几张表,看起来像这样:
Is there an easy way to accomplish this? 有没有简单的方法可以做到这一点? I've tried a bunch of examples but can't seem to get the formatting correct due to the range always being variable.
我尝试了很多示例,但由于范围始终是可变的,因此似乎无法正确设置格式。
This is the basic setup I use. 这是我使用的基本设置。 You can add a lot of formatting to add data labels, remove gridlines, change the title, etc. You may consider learning about defining ranges before going much further in VBA.
您可以添加许多格式来添加数据标签,删除网格线,更改标题等。您可以在进一步了解VBA之前考虑学习定义范围。
Sub AddGraphs()
'Set the dynamic ranges
LR = Cells(Rows.Count, 1).End(xlUp).Row
LC = Cells(1, Columns.Count).End(xlToLeft).Column
'Create the chart
Charts.Add
With ActiveChart
.ChartType = xlColumnClustered
.SetSourceData Source:=Range(Cells(1, 1), Cells(LR, LC))
.Location xlLocationAsObject, "Sheet1"
End With
'Format chart and set location
With ActiveChart
.Parent.Top = Cells(1, LC + 3).Top
.Parent.Left = Cells(1, LC + 3).Left
.HasLegend = False
End With
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.