+ Reply to Thread
Results 1 to 7 of 7

Thread: Multiple charts from one sheet

  1. #1
    Registered User
    Join Date
    08-29-2011
    Location
    Montreal
    MS-Off Ver
    Excel 2003
    Posts
    8

    Multiple charts from one sheet

    Hi,

    I am not familiar with VB so I'm not sure how to write this macro, but I would like the script to plot each sample (See Sample.xls) on a different chart. I found a VB script on Microsoft's website that plots all samples on the same plot.

    Sub CreateChart()
       ' Select the cell in the upper-left corner of the chart.
        ActiveSheet.Range("A1:T2").Select
       ' Select the current range of data. This line of code assumes that
       ' the current region of cells is contiguous - without empty rows
       ' or columns.
       Selection.CurrentRegion.Select
       ' Assign the address of the selected range of cells to a variable.
       myrange = Selection.Address
       ' Assign the name of the active sheet to a variable. This line is
       ' used in order to allow a chart to be created on a separate chart
       ' sheet.
       mysheetname = ActiveSheet.Name
       ' Add a chart to the active sheet.
       ' ActiveSheet.ChartObjects.Add(125.25, 60, 301.5, 155.25).Select
       ' To create a chart on a separate chart sheet, remark out the
       ' previous line, and substitute the next line for the one above.
       Charts.Add
       Application.CutCopyMode = False
       ' This line can best be written by recording a macro, and
       ' modifying the code generated by the Microsoft Excel Macro
       ' recorder.
       ActiveChart.ChartWizard _
          Source:=Sheets(mysheetname).Range(myrange), _
          Gallery:=xlLine, Format:=4, PlotBy:=xlRows, _
          CategoryLabels:=1, SeriesLabels:=1, HasLegend:=1, _
          Title:="", CategoryTitle:="", _
          ValueTitle:="", ExtraTitle:=""
    End Sub
    I would modify this myself but I don't know the VB language.

    Thanks.
    Attached Files Attached Files

  2. #2
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    2003 & 2007 & 2010
    Posts
    11,349

    Re: Multiple charts from one sheet

    Something like this to output a chart for each row.
    Sub CreateChart()
    
        Dim rngDate As Range
        Dim rngName As Range
        Dim rngData As Range
        Dim objCht As ChartObject
        Dim sngLeft As Single
        Dim sngTop As Single
        Dim sngWidth As Single
        Dim sngHeight As Single
        
        Set rngDate = Range("B1:J1")
        Set rngData = rngDate.Offset(1)
        Set rngName = Range("A2")
        
        sngLeft = 10
        sngTop = 100
        sngWidth = 250
        sngHeight = 200
        
        Do While Len(rngName) > 0
            Set objCht = ActiveSheet.ChartObjects.Add(sngLeft, sngTop, sngWidth, sngHeight)
            With objCht.Chart
                Do While .SeriesCollection.Count > 0
                    .SeriesCollection(1).Delete
                Loop
                .ChartType = xlLine
                With .SeriesCollection.NewSeries
                    .Name = rngName
                    .Values = rngData
                    .XValues = rngDate
                End With
            End With
            Set rngData = rngData.Offset(1)
            Set rngName = rngName.Offset(1)
            sngTop = sngTop + sngHeight
        Loop
    
    End Sub
    Cheers
    Andy
    www.andypope.info

  3. #3
    Registered User
    Join Date
    08-29-2011
    Location
    Montreal
    MS-Off Ver
    Excel 2003
    Posts
    8

    Re: Multiple charts from one sheet

    Thanks a lot Andy, much appreciated. I have a small request tho. The Sample file I provided was just an example of what I needed. So, the actual amount of data might vary. I may have more dates or more samples. Basically I want the script to check the columns of dates until there is an empty column, same for the rows and then plot all the data. Is that possible?

    Thanks.

  4. #4
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    2003 & 2007 & 2010
    Posts
    11,349

    Re: Multiple charts from one sheet

    This change will do the date columns.

        Set rngDate = Range("B1",range("B1").end(xltoright))
    Rows are automatically processed until a rngName , column A, is empty.
    Cheers
    Andy
    www.andypope.info

  5. #5
    Registered User
    Join Date
    08-29-2011
    Location
    Montreal
    MS-Off Ver
    Excel 2003
    Posts
    8

    Re: Multiple charts from one sheet

    Works like a charm! Thank you very much. One last request, I would like to generate the charts in a separate sheet in the same excel file.

  6. #6
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    2003 & 2007 & 2010
    Posts
    11,349

    Re: Multiple charts from one sheet

    just add the Location method to the loop

            With objCht.Chart
                Do While .SeriesCollection.Count > 0
                    .SeriesCollection(1).Delete
                Loop
                .ChartType = xlLine
                With .SeriesCollection.NewSeries
                    .Name = rngName
                    .Values = rngData
                    .XValues = rngDate
                End With
                .Location xlLocationAsNewSheet
            End With
    Cheers
    Andy
    www.andypope.info

  7. #7
    Registered User
    Join Date
    08-29-2011
    Location
    Montreal
    MS-Off Ver
    Excel 2003
    Posts
    8

    Re: Multiple charts from one sheet

    Thanks again.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.2.0