+ Reply to Thread
Results 1 to 7 of 7

Thread: Creating Multiple Charts using VBA

  1. #1
    Registered User
    Join Date
    12-29-2010
    Location
    Philadelphia, USA
    MS-Off Ver
    Excel 2003
    Posts
    14

    Creating Multiple Charts using VBA

    Hi,

    I am fairly new to VBA and need help with a report to create multiple charts. In the report you will see two tabs one marked Data the other charts. In the charts tab I have manually created two charts using some of the data from the Data tab. You can see it is a fairly simply pattern. Columns C and D are the titles for the series, and I need charts for every 4 rows. After every 4th row I would like to create a new chart. Row E6:BE6 (date) serves as the X value, while the data below are the values for the chart. I have only created two charts but you can see that I need to create charts for all the values. The problem is that the number of entries, will change. Here I have 8 set of 4's. I could have X number of sets of 4's. I need to write code to make sure I catch all the data. I have tried many different types of loops but can't seem to get it. It would be great if the charts could form right under each other, but that is not that important, as long as they don't form right on top of each other. Also exactly how the charts look is not that important either, something like what I have would be great. Chart title and series names are important...i.e. they need to keep changing as the data changes. If anyone needs more info or has questions please just let me know. Please any help anyone can give is greatly appreciated.


    Tom


    Sorry wasn't sure of the rules here is a link to another board I posted the question. I just really need help on this one.
    http://www.ozgrid.com/forum/showthread.php?t=149308


    Thanks,

    Tom
    Attached Files Attached Files
    Last edited by tmd2; 01-02-2011 at 05:18 AM. Reason: Provide link to cross post URL

  2. #2
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    Apparently I can't say
    MS-Off Ver
    Apparently I can't say
    Posts
    8,274

    Re: Creating Multiple Charts using VBA

    Also posted here.

    Your post does not comply with Rule 8 of our Forum RULES. Cross-posting is when you post the same question in other forums on the web. You'll find people are disinclined to respond to cross-posts because they may be wasting their time solving a problem that has been solved elsewhere. We prefer that you not cross-post at all, but if you do (and it's unlikely to go unnoticed), you MUST provide a link (copy the url from the address bar in your browser)to the cross-post. Expect cross-posts without a link to be closed a message will be posted by the moderator explaining why. We are here to help so help us help you!

    Read this to understand why we ask you to do this

  3. #3
    Registered User
    Join Date
    12-29-2010
    Location
    Philadelphia, USA
    MS-Off Ver
    Excel 2003
    Posts
    14

    Re: Creating Multiple Charts using VBA

    Sorry I edited my original post. This has been driving me crazy for a few days. I was just looking for help from as many places as I could get. Any help any one can give is greatly appreciated.

  4. #4
    Registered User
    Join Date
    12-29-2010
    Location
    Hitchin, England
    MS-Off Ver
    Excel 2003
    Posts
    2

    Re: Creating Multiple Charts using VBA

    Hi,

    I had a quick play around with this. I'm a complete amateur, so the code is probably not as good as it could be, but hopefully it is a starter for 10.

    Let me know if the attached doesn't work for you

    (code used linked to the button on your data tab)

    Sub Refresh()
    
    'Variables
    'wsCharts and wsData are the two worksheets
    ' conSpacing is the constant for the spacing between graphs (20 rows)
    ' this is based on how excel creates graphs on my machine, it changes if
    ' excel isn't full screen (I think)
    ' i and j are just counters
    ' LastRow is the final data entry in the table in column C
    
        Dim wsCharts As Worksheet
        Set wsCharts = Worksheets("Charts")
        Dim wsData As Worksheet
        Set wsData = Worksheets("Data")
        Const conSpacing = 20, conStartInRow = 6
        Dim i, j, LastRow As Long
        
    'Delete all existing charts on the page
    
        wsCharts.ChartObjects.Delete
    
    'Find the last data entry in column C
    
        With wsData
            LastRow = .Cells(.Rows.Count, "C").End(xlUp).Row
        End With
        
    'Add a blank chart
    'Linemarkers is the chart type
    'set a dummy data range, and tell the graph to plot by rows
    'Put the chard in the charts worksheet
    'Tell the sheet it has a title
    'Set the x and y axis as Date and Hours
    'Cut the sample chart
    
        Charts.Add
        ActiveChart.ChartType = xlLineMarkers
            ActiveChart.SetSourceData Source:=wsData.Range("E6:BE10"), PlotBy:= _
            xlRows
        ActiveChart.Location where:=xlLocationAsObject, Name:=wsCharts.Name
        With ActiveChart
            .HasTitle = True
            .Axes(xlCategory, xlPrimary).HasTitle = True
            .Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "Date"
            .Axes(xlValue, xlPrimary).HasTitle = True
            .Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "Hours"
        End With
        wsCharts.ChartObjects.Cut
        
    'For 1 to the number of groups of four
    
        For i = 1 To (LastRow - conStartInRow) / 4
        
    'If it is the first i, select cell A1, else select the cell according to the spacing
            
            If i = 1 Then
                wsCharts.Cells(1, 1).Select
            Else
                wsCharts.Cells((i - 1) * conSpacing, 1).Select
            End If
            
    'Paste the chart to this location
    
            wsCharts.Paste
            
    'Set the series 1 to 4, according to the 4 data ranges in the table
            
            For j = 0 To 3
        
                ActiveChart.SeriesCollection(j + 1).XValues = "=" & wsData.Name & "!R6C5:R6C57"
                
                ActiveChart.SeriesCollection(j + 1).Values = "=" & wsData.Name & "!R" & _
                                    (i * 4) + (conStartInRow / 2) + j & "C5:" _
                                    & "R" & (i * 4) + (conStartInRow / 2) + j & "C57"
            
                ActiveChart.SeriesCollection(j + 1).Name = "=" & wsData.Name & "!R" & _
                                        (i * 4) + (conStartInRow / 2) + j & "C3:" _
                                        & "R" & (i * 4) + (conStartInRow / 2) + j & "C4"
                
            Next
    
    'Give the chart a title of the first label of the data series
    
            With ActiveChart
                .ChartTitle.Characters.Text = wsData.Cells(i * 4 + conStartInRow, 3)
            End With
        Next
        
        
    
    End Sub
    Attached Files Attached Files
    Last edited by koko85; 12-29-2010 at 01:29 PM.

  5. #5
    Registered User
    Join Date
    12-29-2010
    Location
    Philadelphia, USA
    MS-Off Ver
    Excel 2003
    Posts
    14

    Re: Creating Multiple Charts using VBA

    Wow koko85...it works great!!! I can't thank you enough!! I feel like I should pay you with all the work you put in. Brilliant!! I knew I needed two loops but couldn't put it all together. The dividing by 4 was something I didn't think of either. Wow again I can't thank you enough

  6. #6
    Registered User
    Join Date
    12-29-2010
    Location
    Hitchin, England
    MS-Off Ver
    Excel 2003
    Posts
    2

    Re: Creating Multiple Charts using VBA

    No worries. The point of these forums is that they are free I've got loads of advice through searching through other people's posts, so thought I would give something back.

    One thing, please please please test this thoroughly before you use this on any data which is valuable to you, because as I say - I'm no pro.

    If you think this has solved your problem... I'm not sure if you need to put solved in the post title?...

    Have a happy new year.

  7. #7
    Registered User
    Join Date
    12-29-2010
    Location
    Philadelphia, USA
    MS-Off Ver
    Excel 2003
    Posts
    14

    Re: Creating Multiple Charts using VBA

    Yes let me test more today, I will come back and put solved, or whatever needs to be done. I added an 11th set worked fine though. Thanks again for everything. Happy New Year!!

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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