forensic93
New Member
- Joined
- Jan 14, 2020
- Messages
- 16
- Office Version
- 2016
- Platform
- Windows
Hi,
I have attempted at creating a new chart for each column of data that i have. The first column data isn't needed so i've started from column B to I.
My code however doesn't seem to like the loop i have created.
If anyone could see what i've done wrong that would be appreciated.
I have attempted at creating a new chart for each column of data that i have. The first column data isn't needed so i've started from column B to I.
My code however doesn't seem to like the loop i have created.
If anyone could see what i've done wrong that would be appreciated.
VBA Code:
Sub CreateChart()
Application.ScreenUpdating = False
Dim LastRow As Long
Dim xTitle As Range
Dim xData As Range
Dim yTitle As Range
Dim yData As Range
Dim column As Integer
'Find last row with data
LastRow = ActiveSheet.Cells(ActiveSheet.Rows.Count, "A").End(xlUp).Row
For column = 2 To 9
'Set x-axis title
Set xTitle = Range("A1")
'Set y-axis title and data
Set yTitle = Range(column, 1)
Set yData = Range(column, LastRow)
'set total graph range
Set GraphRange = Union(yTitle, yData)
'create chart
ActiveSheet.Shapes.AddChart.Select
ActiveChart.ChartType = xlLine
ActiveChart.SetSourceData Source:=GraphRange
ActiveChart.Location Where:=xlLocationAsNewSheet
ActiveChart.SetElement (msoElementLegendNone)
ActiveChart.SetElement (msoElementPrimaryCategoryAxisTitleAdjacentToAxis)
Selection.Caption = xTitle
ActiveChart.SetElement (msoElementPrimaryValueAxisTitleRotated)
Selection.Caption = yTitle
Next column
Application.ScreenUpdating = True
End Sub