+ Reply to Thread
Results 1 to 8 of 8

loop macro to create different charts by selecting different x-axis data

  1. #1
    Registered User
    Join Date
    04-01-2013
    Location
    Malelane, South Africa
    MS-Off Ver
    Excel 2010
    Posts
    6

    Question loop macro to create different charts by selecting different x-axis data

    Good day

    I am a novice at VBA, and a bit lost. I have recorded a macro to create a scatterplot chart from a template in a worksheet. However, I now need to repeat the process but with a different x-axis data range every time (the y-axis data range remains the same with every subsequent chart).

    The data are arranged in columns - thus for the first chart data are located in column F rows 2-49 (x-axis), and column D rows 2-49 (y-axis). For the next chart the data are in the same rows but in column G (x-axis) and column D (y-axis) and so on till the x-axis data range reach column AZ (x-axis) and column D (y-axis).

    I would really appreciate some help...
    Regards
    Brand

    Here is the macro I recorded for the first chart:

    Sub create_charts()
    '
    ' create_charts Macro
    '
    ' Keyboard Shortcut: Ctrl+t
    '
    ActiveSheet.Shapes.AddChart.Select
    ActiveChart.ApplyChartTemplate ( _
    "C:\Users\Brand\AppData\Roaming\Microsoft\Templates\Charts\mag grafieke met rou data.crtx" _
    )
    ActiveChart.SeriesCollection(1).Name = "='post natal'!$F$1"
    ActiveChart.SeriesCollection(1).XValues = "='post natal'!$F$2:$F$49"
    ActiveChart.SeriesCollection(1).Values = "='post natal'!$D$2:$D$49"
    ActiveChart.SetElement (msoElementChartTitleCenteredOverlay)
    End Sub

  2. #2
    Valued Forum Contributor
    Join Date
    03-29-2013
    Location
    United Kingdom
    MS-Off Ver
    Office/Excel 2013
    Posts
    1,749

    Re: loop macro to create different charts by selecting different x-axis data

    You don't really need a macro.. You can use a Listbox to display the X axes options and the offset formula to pull the selected axis data from the table to a display area... If you're not sure upload the file and I'll set it up for you
    Elegant Simplicity............. Not Always

  3. #3
    Registered User
    Join Date
    04-01-2013
    Location
    Malelane, South Africa
    MS-Off Ver
    Excel 2010
    Posts
    6

    Re: loop macro to create different charts by selecting different x-axis data

    Thanks Andy
    I am not sure how to do that and would appreciate your help. Attahced is the file with the first two charts as I need to do them.... I've changed the column headings for the file I'm uploading so if by any change you need it the macro for the first chart is:

    Sub create_chart()
    '
    ' create_chart Macro
    '

    '
    ActiveSheet.Shapes.AddChart.Select
    ActiveChart.ChartType = xlXYScatter
    ActiveWindow.SmallScroll Down:=-18
    ActiveChart.SeriesCollection.NewSeries
    ActiveChart.SeriesCollection(1).XValues = "=Sheet1!$F$2:$F$49"
    ActiveChart.SeriesCollection(1).Values = "=Sheet1!$D$2:$D$49"
    ActiveChart.SeriesCollection(1).Name = "=Sheet1!$F$1"
    ActiveWindow.SmallScroll Down:=-9
    End Sub

    Thanks again
    Brand
    Attached Files Attached Files

  4. #4
    Valued Forum Contributor
    Join Date
    03-29-2013
    Location
    United Kingdom
    MS-Off Ver
    Office/Excel 2013
    Posts
    1,749

    Re: loop macro to create different charts by selecting different x-axis data

    Got it.... Be right back

  5. #5
    Valued Forum Contributor
    Join Date
    03-29-2013
    Location
    United Kingdom
    MS-Off Ver
    Office/Excel 2013
    Posts
    1,749

    Re: loop macro to create different charts by selecting different x-axis data

    Attachment 224869

    The above should give you what you need


    ...............Don't mention it
    Last edited by AndyLitch; 04-01-2013 at 10:17 AM.

  6. #6
    Registered User
    Join Date
    04-01-2013
    Location
    Malelane, South Africa
    MS-Off Ver
    Excel 2010
    Posts
    6

    Re: loop macro to create different charts by selecting different x-axis data

    Hi Andy.

    Sorry for only replying to you now. Tried to open the attachment bu it says: "Invalid attachment specified". Can you perhaps reattach it?

    Regards
    B

  7. #7
    Valued Forum Contributor
    Join Date
    03-29-2013
    Location
    United Kingdom
    MS-Off Ver
    Office/Excel 2013
    Posts
    1,749

    Re: loop macro to create different charts by selecting different x-axis data

    SelectCharts.xlsx

    sorry about that, i deleted the uploaded file to make space

  8. #8
    Registered User
    Join Date
    04-01-2013
    Location
    Malelane, South Africa
    MS-Off Ver
    Excel 2010
    Posts
    6

    Re: loop macro to create different charts by selecting different x-axis data

    Thanks Andy, quite nifty!
    Have a good day,
    Brand

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