+ Reply to Thread
Results 1 to 6 of 6

Change Chart data range

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    04-02-2017
    Location
    Australia
    MS-Off Ver
    2016
    Posts
    405

    Change Chart data range

    Hi Guys,

    I have Column A full of date data.
    Column B to T has other data.
    I would like to graph Column B vs Column A, and then be able to change the graph data to Column C vs Column A (with a macro), then to Column D vs Column A, and so on. and hopefully have a second macro to change DvsA to CvsA and then BvsA etc.

    Ideally I would like a macro that would work for any selected graph/chart, where it'll change the selected data one column to the left or to the right.

    Thanks.
    Thanks,

    JimmyWilliams

  2. #2
    Registered User
    Join Date
    02-23-2020
    Location
    France
    MS-Off Ver
    2019
    Posts
    44

    Re: Change Chart data range

    Could you please send a sample workbook ?

    Thanks,
    --
    LR

  3. #3
    Forum Contributor
    Join Date
    04-02-2017
    Location
    Australia
    MS-Off Ver
    2016
    Posts
    405

    Re: Change Chart data range

    Quote Originally Posted by ArdusPetus View Post
    Could you please send a sample workbook ?

    Thanks,
    --
    LR

    Hi,

    A macro that will convert data in "Chart1" to be the same data as in "Chart2" (if that macro is ran once). If that macro is ran twice, converts the data in "Chart1" reflects data from "column C". If that macro is ran again convert the data to "column D". And so on.

    Similarly, a second macro that will convert the data from D->C->B, etc

    Ideally both macros should be written in a way that they can be changed to manipulate "Chart2" instead of "Chart1"

    The number of rows may vary from project to project, so the macros need to use the current number of rows from the current chart to figure out the new data range.
    I'm looking to have a tool that I can use on multiple projects in the future to help quickly analyzing and comparing results graphically.

    Any help would be appreciated.
    Attached Files Attached Files

  4. #4
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,085

    Re: Change Chart data range

    Here a possibility
    Chart1 is prepared with column "B" and each time the macro is used (Control + r) next column is selected.
    If it is OK next macro will select previous column
    Public WkCol As String
    
    Sub Auto_open()
        WkCol = "B"
        UpdateChart1 (WkCol)
    End Sub
     
     
    Sub ChangeChart1()
        WkCol = Split(Cells(1, Cells(1, WkCol).Column + 1).Address, "$")(1)
        UpdateChart1 (WkCol)
    
    End Sub
    
     
    
    Sub UpdateChart1(NwCol As String)
    Const Fom = "SERIES(Sheet1!$#$1,Sheet1!$A$2:$A$µ,Sheet1!$#$2:$#$µ,1)"
    Dim LR As Long
        LR = Cells(Rows.Count, 1).End(3).Row
        
        T = Replace(Fom, "µ", LR)
        T = Replace(T, "#", NwCol)
        ActiveSheet.ChartObjects(1).Chart.SeriesCollection(1).Formula = "=" & T
        ActiveSheet.Cells(1, WkCol).Select
    End Sub
    Attached Files Attached Files
    Last edited by PCI; 03-30-2020 at 01:22 PM.
    - Battle without fear gives no glory - Just try

  5. #5
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,085

    Re: Change Chart data range

    An update


    Public WkCol As String
    
    Sub Auto_open()
        WkCol = "B"
        UpdateChart1 (WkCol)
    End Sub
     
     
    Sub ChangeChart1()
        If (Cells(1, Columns.Count).End(xlToLeft).Column <> Cells(1, WkCol).Column) Then _
            WkCol = Split(Cells(1, Cells(1, WkCol).Column + 1).Address, "$")(1)
        UpdateChart1 (WkCol)
    
    End Sub
    Sub ChangeChart2()
        If (WkCol <> "B") Then _
            WkCol = Split(Cells(1, Cells(1, WkCol).Column - 1).Address, "$")(1)
        UpdateChart1 (WkCol)
    
    End Sub
    
    
     
    
    Sub UpdateChart1(NwCol As String)
    Const Fom = "SERIES(Sheet1!$#$1,Sheet1!$A$2:$A$µ,Sheet1!$#$2:$#$µ,1)"
    Dim LR As Long
        LR = Cells(Rows.Count, 1).End(3).Row
        
        T = Replace(Fom, "µ", LR)
        T = Replace(T, "#", NwCol)
        ActiveSheet.ChartObjects(1).Chart.SeriesCollection(1).Formula = "=" & T
        ActiveSheet.Cells(1, WkCol).Select
    End Sub
    Attached Files Attached Files

  6. #6
    Forum Contributor
    Join Date
    04-02-2017
    Location
    Australia
    MS-Off Ver
    2016
    Posts
    405

    Re: Change Chart data range

    PCI,

    What you've done so far is pretty awesome.
    I'm hoping to get something I can use on many different projects.


    However I've got a few more requests.
    • Make Program work for both Chart 1 on both Sheet1 and Sheet2 (independently, using their respective data. Ideally this should work for any number of many sheets, where the data may be located in many different places).
    • I've turned the Auto_open() off so you can see what I'm talking about.
    • (This is a bit off topic) On module2 I have a function that changes the axis of charts based on cell values (this allows for quickly editing charts). I'm curious to see if you can get the data to read the ranges corresponding to that data? Eg, Data3 will use values from Column D to set the data ranges.
    • The program you've made stops when you reach column T, the data might vary in number of columns, can you account for this? (I've included my Functions_Module, you could call upon BlanksToSkip, this might help find the end of the data?)

    I really love what you've done so far, and I'm hoping you can make it a little bit more dynamic, so it can be used for many different situations with minimal editing.
    Thanks man. I'm going to send you a lot of reputation.
    Attached Files Attached Files

+ 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] Want to change chart data range from Kg to stones
    By johnnypat in forum Excel Charting & Pivots
    Replies: 9
    Last Post: 05-18-2019, 02:21 AM
  2. Copy chart and change data range
    By excelactuary in forum Excel Charting & Pivots
    Replies: 9
    Last Post: 07-24-2018, 12:13 PM
  3. [SOLVED] Expand Defined Name Range Horizontally to Dynamically Change Chart with new data
    By machl22 in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 12-20-2017, 12:14 PM
  4. Change the data range for an existing chart
    By Eebigdog in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-07-2016, 01:13 PM
  5. [SOLVED] Change chart source data range
    By jprlimey in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-06-2015, 10:51 AM
  6. [SOLVED] Change Chart Data Range to show YTD data
    By Harribone in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-04-2015, 04:12 PM
  7. chart change on my range
    By pedjvak in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 02-04-2015, 02:45 AM

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