Results 1 to 5 of 5

Thread: Code To Insert Chart And Delete Charts Using VBA

  1. #1
    Senior Member
    Join Date
    Jul 2013
    Posts
    102
    Rep Power
    12

    Code To Insert Chart And Delete Charts Using VBA

    Please see attached sample workbook

    There are 3 information sets which I require a chart for each set.

    I want to be able to use code to see the 3 sets of data and then with button selection insert the charts ( like the chart position /stylein workbook attached)

    There also is a button to delete the Charts

    Best Regards

    Jeff
    Attached Files Attached Files

  2. #2
    Administrator Excel Fox's Avatar
    Join Date
    Mar 2011
    Posts
    1,402
    Rep Power
    10
    Are you saying that you will have 3 sets of data, and on the click of a button, you want 3 charts to be prepared. The position of the chart will be based on where the data set is?
    A dream is not something you see when you are asleep, but something you strive for when you are awake.

    It's usually a bad idea to say that something can't be done.

    The difference between dream and aim, is that one requires soundless sleep to see and the other requires sleepless efforts to achieve

    Join us at Facebook

  3. #3
    Senior Member
    Join Date
    Jul 2013
    Posts
    102
    Rep Power
    12
    Yes thank you
    Jeff

  4. #4
    Administrator Excel Fox's Avatar
    Join Date
    Mar 2011
    Posts
    1,402
    Rep Power
    10
    Jeff, here's one code. I'm sure you can replicate this for all the other charts.

    Code:
    Sub AddChart()
    
        With ActiveSheet.Shapes.AddChart
            .Width = 544.5: .Height = 171.25: .Left = 291
            With .Chart
                .SetSourceData Source:=Range("Sheet1!$B$21:$E$23")
                .ChartType = xlColumnClustered
                .Legend.Delete
                .Axes(xlValue).Delete
                .Axes(xlValue).MajorGridlines.Delete
                .SeriesCollection(1).ApplyDataLabels
            End With
            With ActiveSheet.ChartObjects(.Name).Border
                .Color = 0
                .LineStyle = 1
                .Weight = 2.6
            End With
        End With
        
    End Sub
    And the code to delete all charts in the active sheet is
    Code:
    Activesheet.ChartObjects.Delete
    A dream is not something you see when you are asleep, but something you strive for when you are awake.

    It's usually a bad idea to say that something can't be done.

    The difference between dream and aim, is that one requires soundless sleep to see and the other requires sleepless efforts to achieve

    Join us at Facebook

  5. #5
    Senior Member
    Join Date
    Jul 2013
    Posts
    102
    Rep Power
    12
    ExcelFox
    Thanks for the code to create & delete charts.
    Is it best to place the code using Insert Module or is it best to place in Sheet Module
    As I only have 1 sheet I inserted into Module and it works
    Regards
    Jeff

Similar Threads

  1. Custom Charts in Excel :: Gauge Chart (aka Dial/Speedometer Chart)
    By Transformer in forum Tips, Tricks & Downloads (No Questions)
    Replies: 0
    Last Post: 08-07-2013, 05:16 PM
  2. VBA code to delete cell ranges
    By rich_cirillo in forum Excel Help
    Replies: 3
    Last Post: 07-08-2013, 09:18 AM
  3. Custom Charts in Excel :: Waterfall Chart
    By Transformer in forum Tips, Tricks & Downloads (No Questions)
    Replies: 0
    Last Post: 06-21-2013, 04:52 PM
  4. Insert Or Delete Columns Based On User Input
    By HDMI in forum Excel Help
    Replies: 4
    Last Post: 06-21-2013, 03:00 AM
  5. Adding charts via code to a protected sheet
    By Rasm in forum Excel Help
    Replies: 2
    Last Post: 11-14-2012, 05:11 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •