Results 1 to 4 of 4

Thread: Help needed to add a Chart with a macro in Excel 2007

  1. #1
    Join Date
    Dec 2008
    Posts
    137

    Help needed to add a Chart with a macro in Excel 2007

    Hi friends,

    I have already added some data in a sheet for which i have built a macro. But now i want same data to showed through chart.For the same i have made a recording for macro to chart it and it is included below. But when I try to run the recorded macro it hangs up on the "ActiveSheet.Shapes.addchart.Select" .
    Can any one help me out with this issue.

    Code:
    Range("B1:E54").Select
    ActiveSheet.Shapes.addchart.Select
    ActiveChart.SetSourceData Source:=Range("'52weeks'!$B$1:$E$54")
    ActiveChart.ChartType = x1LineMarkers

  2. #2
    Join Date
    Dec 2008
    Posts
    144

    Re: Help needed to add a Chart with a macro in Excel 2007

    I would prefer you not to record macro instead of that you can take an example of the following code which will help you a lot.
    Code:
    Sub Chrt_Current_Status_Click()
    
    Dim chtChart As Chart
    
    ' Create a new chart.
    
    Set chtChart = Charts.Add
    Set chtChart = chtChart.Location(Where:=xlLocationAsObject,
    Name:="sheetname")
    
    With chtChart
    
    .ChartType = xlCylinderColClustered ' Chart type
    
    ' Set data source range.
    
    .SetSourceData Source:=Sheets("BASIC CHART DATA").Range("G34:H39") '
    source of data
    .HasTitle = True
    .ChartTitle.Text = "Current Status"
    .SeriesCollection(1).XValues = "='BASIC CHART DATA'!$G$34:$G$39"
    '.Axes(xlCategory, xlPrimary) = True
    
    ' The Parent property is used to set properties of the Chart. ' sets
    size of chart
    
    With .Parent
    .Top = Range("G3").Top
    .Left = Range("G3").Left
    .Width = Range("G3:R34").Width
    .Height = Range("G3:R34").Height
    
    End With
    
    End With
    
    End Sub

  3. #3
    Join Date
    Dec 2008
    Posts
    123

    Re: Help needed to add a Chart with a macro in Excel 2007

    I had made similar type of issue, I use the following code and it worked for me hope, it,s works for you too.

    Code:
    Dim myChtObj As ChartObject
    Dim rngChtData As Range
    Dim rngChtXVal As Range
    Dim iColumn As Long
    
    If TypeName(Selection) <> "Range" Then Exit Sub
    Range("B1:E54").Select
    Set rngChtData = Selection
    With rngChtData
    Set rngChtXVal = .Columns(1).Offset(1).Resize(.Rows.Count - 1)
    End With
    Set myChtObj = ActiveSheet.ChartObjects.Add _
    (Left:=250, Width:=375, Top:=75, Height:=225)
    With myChtObj.Chart
    .ChartType = xlLineMarkers
    
    Do Until .SeriesCollection.Count = 0
    .SeriesCollection(1).Delete
    Loop
    For iColumn = 2 To rngChtData.Columns.Count
    With .SeriesCollection.NewSeries
    .Values = rngChtXVal.Offset(, iColumn - 1)
    .XValues = rngChtXVal
    .Name = rngChtData(1, iColumn)
    End With
    Next
    myChtObj.Activate
    End With
    Call FormatChart

  4. #4
    Join Date
    Nov 2010
    Posts
    3

    Re: Help needed to add a Chart with a macro in Excel 2007

    I get the same problem as reported by Clemens, so I tried Crona's suggestion and now I get the following error:
    Type Mismatch on the Set chtChart = Charts.Add

    ' Create a new chart.

    Set chtChart = chtChart.Location(Where:=xlLocationAsObject,
    Name:="sheetname")

    Any help please, this is driving me nuts.

Similar Threads

  1. Excel 2003 Macro doesn't work in Excel 2007
    By jjaw in forum Windows Software
    Replies: 3
    Last Post: 03-01-2014, 03:28 PM
  2. Help needed to enter a table inside a Excel chart
    By Ambition in forum MS Office Support
    Replies: 2
    Last Post: 25-02-2012, 11:48 AM
  3. Excel 2007 macro help needed
    By AZUL in forum Software Development
    Replies: 4
    Last Post: 19-11-2009, 06:10 PM
  4. Excel 2007 chart templates
    By raviranch in forum Windows Software
    Replies: 2
    Last Post: 13-07-2009, 01:30 PM
  5. How to lock Chart in Excel 2007
    By Durjaya in forum Windows Software
    Replies: 3
    Last Post: 08-05-2009, 03:07 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
  •  
Page generated in 1,713,494,519.58244 seconds with 17 queries