Charts based on one Module

Forestq

Active Member
Joined
May 9, 2010
Messages
482
Team,

I have code which is creating chart about active sheet.
I need to use this code to create a few charts in seperate sheets,
for example:
- sheet1 - had data to 3 charts
- sheet2 - had data to 2 charts
- sheet3 - had data to 5 charts.

I don't want to duplicate my module for each chart (this makes no sense). I think I use some proceuder to call my function but add some new source data for each charts.
Please help me how to do it.

my code:
Code:
Sub CreateAChart()

    'create an embedded chart
    Dim co As ChartObject
      
    'position chart using column width and row height units
    Set co = ActiveSheet.ChartObjects.Add(100, 100, 400, 300)
    
    'set chart type
    co.Chart.ChartType = xlLine 'xlPie
    
    
    'name it
    co.Name = "ChartExample"
    
    'Debug.Print co.Name 
    'Debug.Print co.Chart.Name     
 
    
    'add data series
    co.Chart.SeriesCollection.Add _
        Source:=ActiveSheet.Range("A1:C6"), _
        Rowcol:=xlColumns, SeriesLabels:=True, _
        Categorylabels:=True
        
    
    
    'add axes (default settings - here is for illustration)
    With co.Chart
        .HasAxis(xlCategory, xlPrimary) = True
        .HasAxis(xlCategory, xlSecondary) = False
        .HasAxis(xlValue, xlPrimary) = True
        .HasAxis(xlValue, xlSecondary) = False
    End With
       
    'axis title formatting
    With co.Chart.Axes(xlCategory)
        .HasTitle = True
        .AxisTitle.Caption = "Types"
        .AxisTitle.Border.Weight = xlMedium
    End With
    
    With co.Chart.Axes(xlValue)
        .HasTitle = True
            
        With .AxisTitle
            .Caption = "Quantity for 1999"
            .Font.Size = 8
            .Orientation = xlHorizontal
            .Characters(14, 4).Font.Italic = True
            .Border.Weight = xlMedium
        End With
    End With
    'change the category name (Types) to lower case (1 kolumna)
    co.Chart.Axes(xlCategory).CategoryNames = _
    Array("a", "b", "c", "d", "e")
    
    'set the crossing point on the (primary) value axis at 50
    co.Chart.Axes(xlValue).CrossesAt = 50
    'horizontal but no vertical gridlines (siatka na wykresie)
    co.Chart.Axes(xlValue).HasMajorGridlines = True 'pozioma
    co.Chart.Axes(xlCategory).HasMajorGridlines = False 'pionowa
    
    'outside Tickmarks on category axis 
    co.Chart.Axes(xlCategory).MajorTickMark = xlTickMarkCross
    'move tick labels to below chart area
    co.Chart.Axes(xlCategory).TickLabelPosition = _
        xlTickLabelPositionNextToAxis
        
    'set chart area fill to solid white
    co.Chart.ChartArea.Interior.Color = RGB(255, 255, 255)
    
    'set plot area fill to gray
    co.Chart.PlotArea.Interior.ColorIndex = 15
       
   With co.Chart.SeriesCollection(1)
        .MarkerSize = 10
        .MarkerStyle = xlMarkerStyleDiamond
    With .Points(2)
        .MarkerSize = 20
        .MarkerStyle = xlMarkerStyleCircle
    End With
   End With
   
End Sub


regards,
PvK
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Copy your module? In the parlance, a module is a VBA code window. I don't hink it's what you mean.

Isn't it easy enough to copy the first chart, and change the source data range in the copy?
 
Upvote 0
You mean, change source in each chart?

I want to run a macro, which will create (6, 8, 10 charts), in separate sheets, and the source will be diffrents.

My idee: create vb code (function or procedure), and when I will be call it for chart1, chart2, chart3 (it would be : sub_1, sub_2, sub_3), in each procedure only source (code) will be diffrents, function (code how to create chart) will be same.

PvK
 
Upvote 0
Will it be okay to select the data, then run the code to make a chart from that particular data, and rerun the code each time you select more data?
 
Upvote 0
yes, it should be ok,
also I was thinking, to set constant data ,for exmaple, for 8 charts.
It easy for me this way, not always looking a source.
 
Upvote 0

Forum statistics

Threads
1,224,507
Messages
6,179,176
Members
452,893
Latest member
denay

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