VBA to create LineMarkers chart from dynamic rows and fixed number of column

dibyendu

New Member
Joined
Jun 16, 2015
Messages
28
I have a worksheet named "Summary". Column "A" to column "Q" have variable numbers of data. The number of rows are not fixed(dynamic) for a column. Also Sometimes one or more column are empty or all the cells in a column have missing data.
Now, I want to create a LineMarkers chart from each column (having different number of rows in column "A" to column "Q").The first row is for heading. The VBA should able to recognised the changes made in data and accordingly the chart will also gets updated every time.
I have the following code which take row as range for the chart. If the VBA code can be modified to take column as range for the chart.Thanks


Sub chart()
'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("Summary").Range("A65536").End(xlUp).Row


'Find the last used column
LastColumn = Sheets("Summary").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("Summary").Select


'Adds chart to the sheet
Set chrt = Sheets("Summary").Shapes.AddChart.chart
'sets the chart type
chrt.ChartType = xlLineMarkers


'now the line chart is added...setting its data source here
With Sheets("Summary")
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
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.

Forum statistics

Threads
1,223,264
Messages
6,171,081
Members
452,377
Latest member
bradfordsam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top