简体   繁体   English

使用宏和.End函数在Excel VBA中创建动态图表

[英]Creating a dynamic chart in Excel VBA using macro and .End function

I'm trying to make a dynamic chart based on the last row and column. 我正在尝试根据最后一行和最后一列制作一个动态图表。 The problem occurs when I have to define the Chart range. 当我必须定义图表范围时,会出现问题。 It identifies both the last row and column but when I try to set the chart range the last column is equal to the full spreadsheet. 它标识了最后一行和最后一列,但是当我尝试设置图表范围时,最后一列等于整个电子表格。

Code: 码:

Sub Spiderweb_test()
'
'Spiderweb_test Macro
    Dim chtObj As ChartObject

    lr = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
    lc = ActiveSheet.Cells(4, Columns.Count).End(xlToLeft).Column

    Set chtRng = Range("A4:lc" & lr)
    Set ChartArea = Range("a" & lr + 3 & ":f" & lr + 15)

    ActiveSheet.Shapes.AddChart2(317, xlRadarMarkers).Select
    ActiveChart.SetSourceData Source:=chtRng

    Set chtObj = ActiveChart.Parent
    chtObj.Top = ChartArea.Top
    chtObj.Left = ChartArea.Left
    chtObj.Height = ChartArea.Height
    chtObj.Width = ChartArea.Width
End Sub

Picture of output: 输出图片:

输出图片

Use: 采用:

Set chtRng = Range(Cells(4, 1), Cells(lr, lc))

Since lc is actually the number of the column, not the letter. 由于lc实际上是列号,而不是字母。 Hence the Chart Range is not being set correctly. 因此,图表范围设置不正确。

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

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