Creating multiple graphs from each column using VBA

forensic93

New Member
Joined
Jan 14, 2020
Messages
16
Office Version
  1. 2016
Platform
  1. 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.

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
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Hi

Code:
    'set total graph range
    Set GraphRange = Union(yTitle, yData)

This creates a range with 2 cells. For ex., if LastRow is 10 and column is B you get the range with the cells B1 and B10.
Is this what you want or do you want the range B1:B10?

If that's the case use

VBA Code:
Set GraphRange = Range(yTitle, yData)
 
Upvote 0
If that's the case use
VBA Code:
Set GraphRange = Range(yTitle, yData)

Hi,

The code works to create one graph if i pull it out of the loop.
My issue is purely just that when i put it in the loop i get an error for using the variable column to define which column i'm trying use. The section of code i'm referring to i've posted below

VBA Code:
  'Set y-axis title and data
    Set yTitle = Range(column, 1)
    Set yData = Range(column, LastRow)
 
Upvote 0
I see. Range expects range parameters.
If you want to define the range with column and row number then use Cells

VBA Code:
  'Set y-axis title and data
    Set yTitle = Cells(column, 1)
    Set yData = Cells(column, LastRow)
.
 
Upvote 0

Forum statistics

Threads
1,214,827
Messages
6,121,824
Members
449,050
Latest member
Bradel

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