+ Reply to Thread
Results 1 to 2 of 2

Macro Chart Title

  1. #1
    Registered User
    Join Date
    02-24-2009
    Location
    green bay, WI, USA
    MS-Off Ver
    Excel 2003
    Posts
    1

    Macro Chart Title

    Hi I Am using Excel 2003 and have created a Macro part of which is pasted here.
    " Charts.Add" & vbCr & _
    " ActiveChart.ChartType = xlColumnClustered " & vbCr & _
    " ActiveChart.SetSourceData Source:=Sheets(""PackageWeightDetail"").Range( _" & vbCr & _
    " ""B5:B" & rows & ",C5:C" & rows & """), PlotBy:=xlColumns" & vbCr & _
    " ActiveChart.Location Where:=xlLocationAsNewSheet, Name:=""PackageWtBarGraph"" " & vbCr & _
    " With ActiveChart" & vbCr & _
    " .HasTitle = True" & vbCr & _
    " .ChartTitle.Characters.Text = ""Package Weight BReak""" & vbCr & _
    " .Axes(xlCategory, xlPrimary).HasTitle = True" & vbCr & _
    " .Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = ""Weight """ & vbCr & _
    " .Axes(xlValue, xlPrimary).HasTitle = True" & vbCr & _
    " .Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = ""Packages"" " & vbCr & _
    " End With" & vbCr & _
    " ActiveChart.HasLegend = True" & vbCr & _
    " ActiveChart.Legend.Select" & vbCr & _
    " Selection.Position = xlBottom" & vbCr & _
    " ActiveChart.PlotArea.Select" & vbCr & _
    " With Selection.Border" & vbCr & _
    " .ColorIndex = 16" & vbCr & _
    After this chart is created in the same excel workbook i go through a for loop and delete the Sheet 1,2 3 that was in there by deafult.
    like in the following code.
    For Each wksht In Excel.Sheets
    If (Trim(wksht.Name) = "Sheet1" Or Trim(wksht.Name) = "Sheet2" Or Trim(wksht.Name) = "Sheet3" Or Trim(wksht.Name) = "Delete") Then
    Excel.Sheets(wksht.Name).Delete()
    End If
    Next
    The code goes through and deletes the Sheet 2, 3 etc but when it comes to the sheet created by the chart packageWtBArgraph as it is named in the chart it mentions Invalid CAst and fails.

    I am not sure if this name is not set correctly in a string format or what. CAn someone please help what or why i am getting this error.
    Last edited by VBA Noob; 02-24-2009 at 03:39 AM.

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Macro Chart Title

    Welcome to the forum.

    Please take a few minutes to read the Forum Rules, and then edit your post to wrap your code with Code Tags.
    Entia non sunt multiplicanda sine necessitate

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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