I'm working on a macro that creates a couple of pie charts. It is intended to be used on a successive set of files, all with differing filenames, and each file containing a single sheet that is named the same as the file. However, the sheet name is without the .xls extension, and of course if the filename exceeds 31 characters (and sometime it does), the sheet name is truncated after 31 characters.
Here's the problem; as long as the the sheet name contains 30 characters or less, the macro works great. If it has 31 characters, it blows up with a runtime 5 error on the line where I am specifying the chart location. Here is the relevant code chunk:
<code>
' set range to prepare for chart creation
Dim rData As Range
Set rData = ActiveSheet.Range("C6", [EndCell])
' Create initial chart
Charts.Add
ActiveChart.ChartType = xlPie
ActiveChart.SetSourceData Source:=rData, PlotBy:= _
xlColumns
ActiveChart.Location Where:=xlLocationAsObject, Name:=MySheet
ActiveChart.ApplyDataLabels AutoText:=True, LegendKey:=False, _
HasLeaderLines:=True, ShowSeriesName:=False, ShowCategoryName:=True, _
ShowValue:=True, ShowPercentage:=True, ShowBubbleSize:=False
</code>
The problem line is the one that starts with ActiveChart.Location
Further up in the code, I have assigned the active sheet name to a variable MySheet to allow me to accomodate the varying sheet names.
Here are two ideas I had:
1. Find some other way to specify the current (and only!) sheet. I have tried (ActiveSheet.Name) but it gives me a different error.
2. Find some way to automatically limit the sheet name to 30 characters
You experts out there will undoubtedly recognize that the above is a mishmash of macro recording and hand editing, but hey, if it works for sheet names with 30 characters, I can't figure out why it chokes on 31!
Apologies, I must have the wrong syntax for the code tag.
Here's the problem; as long as the the sheet name contains 30 characters or less, the macro works great. If it has 31 characters, it blows up with a runtime 5 error on the line where I am specifying the chart location. Here is the relevant code chunk:
<code>
' set range to prepare for chart creation
Dim rData As Range
Set rData = ActiveSheet.Range("C6", [EndCell])
' Create initial chart
Charts.Add
ActiveChart.ChartType = xlPie
ActiveChart.SetSourceData Source:=rData, PlotBy:= _
xlColumns
ActiveChart.Location Where:=xlLocationAsObject, Name:=MySheet
ActiveChart.ApplyDataLabels AutoText:=True, LegendKey:=False, _
HasLeaderLines:=True, ShowSeriesName:=False, ShowCategoryName:=True, _
ShowValue:=True, ShowPercentage:=True, ShowBubbleSize:=False
</code>
The problem line is the one that starts with ActiveChart.Location
Further up in the code, I have assigned the active sheet name to a variable MySheet to allow me to accomodate the varying sheet names.
Here are two ideas I had:
1. Find some other way to specify the current (and only!) sheet. I have tried (ActiveSheet.Name) but it gives me a different error.
2. Find some way to automatically limit the sheet name to 30 characters
You experts out there will undoubtedly recognize that the above is a mishmash of macro recording and hand editing, but hey, if it works for sheet names with 30 characters, I can't figure out why it chokes on 31!
Apologies, I must have the wrong syntax for the code tag.