Creating Line Chart for each Row using VBA excel (Dynamic Row,Column)

后端 未结 1 598
[愿得一人]
[愿得一人] 2021-01-16 05:04

I would appreciate if someone can help me with this code

-- I have dynamic rows and columns, the code finds number of rows and columns using LastRow, and LastColumn.

相关标签:
1条回答
  • 2021-01-16 06:04

    Try below code

    Sub main()
       'variable declaration
        Dim i As Long
        Dim LastRow As Long
        Dim LastColumn As Long
        Dim chrt As Chart
    
        'Find the last used row
        LastRow = Sheets("Sheet1").Range("A65536").End(xlUp).Row
    
        'Find the last used column
        LastColumn = Sheets("Sheet1").Range("A1").End(xlToRight).Column
    
        'Looping from second row till last row which has the data
        For i = 2 To LastRow
            'Sheet 2 is selected bcoz charts will be inserted here
            Sheets("Sheet2").Select
    
            'Adds chart to the sheet
            Set chrt = Sheets("Sheet2").Shapes.AddChart.Chart
            'sets the chart type
            chrt.ChartType = xlLine
    
            'now the line chart is added...setting its data source here
            With Sheets("Sheet1")
                chrt.SetSourceData Source:=.Range(.Cells(i, 1), .Cells(i, LastColumn))
            End With
    
            'Left & top are used to adjust the position of chart on sheet
            chrt.ChartArea.Left = 1
            chrt.ChartArea.Top = (i - 2) * chrt.ChartArea.Height
    
            Next
    
    End Sub
    

    enter image description here

    0 讨论(0)
提交回复
热议问题