+ Reply to Thread
Results 1 to 5 of 5

Loop - Reference Cells Correctly?

Hybrid View

  1. #1
    Registered User
    Join Date
    10-30-2009
    Location
    UK
    MS-Off Ver
    Excel 2003, 2007, 2010
    Posts
    60

    Loop - Reference Cells Correctly?

    Hi folks

    I've been working on trying to create a loop for adding a graph to a new worksheet for several rows of data and I've got so far with it but hit a brick wall with referencing the correct row and I can't seem to get my head around what is bound to be a simple thing.

    This is my code with graph formatting etc removed:

    Sub graphs()
    '
    ' graphs Macro
    ' 13/02/2015
    
        
        Dim n As Integer      
        For n = 5 To 100
        If Sheets("Data").Cells(n, 1) = 0 Then Exit For
      
        Sheets.Add
        ActiveSheet.Move after:=Sheets(ActiveWorkbook.Sheets.Count)
        ActiveSheet.Name = Worksheets("data").Cells(n, 1).Value
        
        Charts.Add
        ActiveChart.ChartType = xlBarClustered
        
        ActiveChart.SeriesCollection.NewSeries
        ActiveChart.SeriesCollection.NewSeries
    
    'Adds static data series:
        ActiveChart.SeriesCollection(1).XValues = "=Data!R3C2:R3C3"
        ActiveChart.SeriesCollection(1).Values = "=Data!R4C2:R4C3"
        ActiveChart.SeriesCollection(1).Name = "=Data!R4C1"
        
    'Adds Individual data series:
        ActiveChart.SeriesCollection(2).Values = "=Data!R5C2:R5C3"
        ActiveChart.SeriesCollection(2).Name = Sheets("Data").Cells(n, 1)
        ActiveChart.Location Where:=xlLocationAsObject, Name:=Worksheets("data").Cells(n, 1).Value
          
            
           Next n
    
    End Sub

    I have got the name of the series(2) data referencing correctly, but I'm unsure how to code the values of this series.
    Attached Files Attached Files
    Last edited by Janc; 02-13-2015 at 09:37 AM. Reason: Solved. Thank you.

  2. #2
    Forum Expert
    Join Date
    08-02-2013
    Location
    Québec
    MS-Off Ver
    Excel 2003, 2007, 2013
    Posts
    1,414

    Re: Loop - Reference Cells Correctly?

    Hello Janc,

    If I understood well, change this part of the code :

            'Adds Individual data series:
            ActiveChart.SeriesCollection(2).Values = Sheets("Data").Cells(n, 2).Resize(1, 2) '          "=Data!R5C2:R5C3"
            ActiveChart.SeriesCollection(2).Name = Sheets("Data").Cells(n, 1)
            ActiveChart.Location Where:=xlLocationAsObject, Name:=Worksheets("data").Cells(n, 1).Value
    GC Excel

    If this post helps, then click the star icon (*) in the bottom left-hand corner of my post to Add reputation.

  3. #3
    Registered User
    Join Date
    10-30-2009
    Location
    UK
    MS-Off Ver
    Excel 2003, 2007, 2010
    Posts
    60

    Re: Loop - Reference Cells Correctly?

    Thanks so much, I really appreciate the explanation as well as the code help as that will (should!) help me learn more for progressing this project.

  4. #4
    Registered User
    Join Date
    10-30-2009
    Location
    UK
    MS-Off Ver
    Excel 2003, 2007, 2010
    Posts
    60

    Re: Loop - Reference Cells Correctly?

    Thank you so much, that works perfectly

    Could you explain the logic behind this part: Cells(n, 2).Resize(1, 2) so I can understand where I'll need to make changes to include further columns in my series, and also so I can add an additional new chart on each sheet from a later column.
    Last edited by Janc; 02-13-2015 at 05:19 AM.

  5. #5
    Forum Expert
    Join Date
    08-02-2013
    Location
    Québec
    MS-Off Ver
    Excel 2003, 2007, 2013
    Posts
    1,414

    Re: Loop - Reference Cells Correctly?

    Your previous code was adding the info from row 5 only for the 2nd data series
    ActiveChart.SeriesCollection(2).Values = "=Data!R5C2:R5C3"
    My piece of code takes the values starting from the cells in row n column B (.cells(n,2)) and extends the range by 2 columns (.Resize(1,2))
    ActiveChart.SeriesCollection(2).Values = Sheets("Data").Cells(n, 2).Resize(1, 2)
    The trick here is the .Resize(number of row, number of columns) function.
    I'm starting from Cell(n, 2) and extends it 2 columns.
    So if we are in B5, the final selection is B5:C5

    Check the Help in Excel on those 2 functions : OFFSET and RESIZE. They are very useful when it's time to select range.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Changing my code to loop through a list of cells (lots of reference cells)
    By joshnathan in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-06-2014, 12:31 PM
  2. LOOP not functioning correctly
    By rlsublime in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-02-2013, 02:03 PM
  3. Run time Error 91 - End Loop not working correctly
    By rowing190 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 08-05-2009, 05:18 PM
  4. How to write this loop macro correctly?
    By cb95amc in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-11-2009, 08:38 AM
  5. loop does not work correctly
    By fredagamie@gmail.com in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-22-2006, 07:30 PM

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.6.0 RC 1