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.
Bookmarks