简体   繁体   中英

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. Hence the Chart Range is not being set correctly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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