+ Reply to Thread
Results 1 to 6 of 6

Using range and cell variables in the series function

Hybrid View

  1. #1
    Registered User
    Join Date
    09-25-2014
    Location
    Manchester
    MS-Off Ver
    2007
    Posts
    5

    Using range and cell variables in the series function

    Hello,

    I am trying to automate a report where specific data is pulled out, saved as a range variable and then used to update a number of existing graphs.

    I'm having trouble referencing the variables correctly in the code - please could someone help me with this?


    Sub Automatic_Reporting(month_value As String, year_value As String)
    ' Macro building to create automatic reports
    ' Input year and month to get report
    
    Dim Title As String
    Title = Range("A3")
    
    'Search for desired year
        Rows("1:1").Select
        Selection.Find(What:=year_value, After:=ActiveCell, LookIn:=xlFormulas, _
            LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, _
            MatchCase:=False, SearchFormat:=False).Activate
            
    'Search for desired month
        Cells.Find(What:=month_value, After:=ActiveCell, LookIn:=xlFormulas, LookAt:= _
            xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:= _
            False, SearchFormat:=False).Select
    
    'Create the variable for the previous 12 months of data
    Dim Twelve_Month_Range As Range
    ' Move 1 cell down to get to data:
    Selection.Offset(1, 0).Select
    ' Jump to the first month of necessary data - 12 months previously:
    Selection.Offset(0, -12).Resize(1, 12).Select
    'Save data to range variable
    Set Twelve_Month_Range = Selection
    
    'Same as above but for the previous 15 months
    Dim Fifteen_Month_Range As Range
    Selection.Offset(0, -3).Resize(1, 15).Select
    Set Fifteen_Month_Range = Selection
    
        ActiveSheet.ChartObjects("Chart 1").Activate
        ActiveChart.SeriesCollection(1).Select
        Selection.Formula = "=SERIES(Title, Sheet1! & Twelve_Month_Range & , Sheet1! & Twelve_Month_Range & ,1)"
    
    End Sub

    It's the final line before 'End Sub' that is causing the problems!!

    Any help would be much appreciated.

    Thanks,
    Rachael

  2. #2
    Forum Expert mrice's Avatar
    Join Date
    06-22-2004
    Location
    Surrey, England
    MS-Off Ver
    Excel 2013
    Posts
    4,967

    Re: Using range and cell variables in the series function

    Can you post a copy of the workbook? It will make analysis so much easier.

  3. #3
    Registered User
    Join Date
    09-25-2014
    Location
    Manchester
    MS-Off Ver
    2007
    Posts
    5

    Re: Using range and cell variables in the series function

    Automatic report building Example.xlsm

    This is the simple example workbook I've been working on... The real one is very similar, there's just a lot more data and more graphs to update.

    Normally, you press "create a report" then the userform feeds the year and month that you choose into the code. It retrieves the ranges fine and they work in simple functions such as 'average' but I'm doing something wrong for the series function it seems!

    Thanks for you help,
    Rachael
    Last edited by rachmanchester; 09-28-2014 at 04:14 PM.

  4. #4
    Forum Expert mrice's Avatar
    Join Date
    06-22-2004
    Location
    Surrey, England
    MS-Off Ver
    Excel 2013
    Posts
    4,967

    Re: Using range and cell variables in the series function

    Try this edit

    'Create the variable for the previous 12 months of data
    Dim Twelve_Month_Range_X As Range
    Dim Twelve_Month_Range_Y As Range
    
    Set Twelve_Month_Range_X = Selection.Offset(0, -12).Resize(1, 12)
    ' Move 1 cell down to get to data:
    Selection.Offset(1, 0).Select
    ' Jump to the first month of necessary data - 12 months previously:
    Selection.Offset(0, -12).Resize(1, 12).Select
    'Save data to range variable
    Set Twelve_Month_Range_Y = Selection
    
    'Same as above but for the previous 15 months
    Dim Fifteen_Month_Range As Range 'Change as above
    Selection.Offset(0, -3).Resize(1, 15).Select
    Set Fifteen_Month_Range = Selection
    
    ActiveSheet.ChartObjects("Chart 1").Activate
    ActiveChart.SeriesCollection(1).Name = Title
    ActiveChart.SeriesCollection(1).Values = "=Sheet1!" & Twelve_Month_Range_Y.Address
    ActiveChart.SeriesCollection(1).XValues = "=Sheet1!" & Twelve_Month_Range_X.Address

  5. #5
    Registered User
    Join Date
    09-25-2014
    Location
    Manchester
    MS-Off Ver
    2007
    Posts
    5

    Re: Using range and cell variables in the series function

    THANK YOU!!!

    You are excellent.

    :D

  6. #6
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Using range and cell variables in the series function

    As it appears you've reached a conclusion, I've marked this thread SOLVED for you.
    FYI, this is done through the Thread Tools located above the first post in this thread. Thanks.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

+ 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. [SOLVED] Using variables in the Range function
    By NicholasL in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-12-2013, 09:45 AM
  2. [SOLVED] Trying to use variables in .range function
    By satania in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-13-2013, 04:16 PM
  3. Passing variables to Range function?
    By svaiskau in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-13-2007, 11:29 AM
  4. Replies: 1
    Last Post: 05-21-2006, 11:15 AM
  5. [SOLVED] Can I use variables in a Range function?
    By André - Brazil in forum Excel General
    Replies: 2
    Last Post: 05-05-2005, 06:06 PM

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