Hello,
I'm using Win7 and MS Office 2010. What I am trying to do is select a chart that is within a worksheet that has a specific name. I currently name the charts to their according year ex)2014.
In my workbook I have sheets that are also named after a year (2014). When I put the sheet name into a variable (which shows up as 2014 when i debug) I try to use that variable to activate the correct chart which is also named 2014.
Here is the code:
I get this error: Run-time error '1004': Application-defined or object defined error. Which is happening on this line:
I have tried to define the variable as a string, integer, or not defining it at all which always brings up this error. I did find a work around that makes the code work, by changing the line above to this and changing the chart name to "Chart 2014":
I don't understand why one works and the other doesn't. I'm using a variable to define the chart in both except adding "Chart " to the name fixes it. I have recorded a macro selecting a chart named "2014" only; and it shows the chart name to be "2014" so it isn't necessary to have "Chart" in the name. Also if I manually type "2014" in the ChartObjects in the code, this will work as well.
Again I got it to work, but I want to know why it doesn't work and is there a way to leave the charts named only "2014" rather than "Chart 2014" which would make my code simpler in other macros.
This is my first time on this forum so I hope I did everything correct...but correct me if there is something I did wrong.
Thanks!
I'm using Win7 and MS Office 2010. What I am trying to do is select a chart that is within a worksheet that has a specific name. I currently name the charts to their according year ex)2014.
In my workbook I have sheets that are also named after a year (2014). When I put the sheet name into a variable (which shows up as 2014 when i debug) I try to use that variable to activate the correct chart which is also named 2014.
Here is the code:
Code:
Sub test()
Sheets("2014").Activate
wsname = ActiveSheet.Name
Avg = Format(Sheets(wsname).Range("AR6").Value, "###")
Sheets("Calf Price Hist.").Activate
ActiveWorkbook.Sheets("Calf Price Hist.").ChartObjects(wsname).Activate
With ActiveChart
.ChartTitle.Text = wsname & " - Price / Calf " & Chr(13) & "Average: $" & Avg
.ChartTitle.Format.TextFrame2.TextRange.Characters(22, 15).Font.Size = 11
End With
End Sub
I get this error: Run-time error '1004': Application-defined or object defined error. Which is happening on this line:
Code:
ActiveWorkbook.Sheets("Calf Price Hist.").ChartObjects(wsname).Activate
I have tried to define the variable as a string, integer, or not defining it at all which always brings up this error. I did find a work around that makes the code work, by changing the line above to this and changing the chart name to "Chart 2014":
Code:
ActiveWorkbook.Sheets("Calf Price Hist.").ChartObjects("Chart " & wsname).Activate
I don't understand why one works and the other doesn't. I'm using a variable to define the chart in both except adding "Chart " to the name fixes it. I have recorded a macro selecting a chart named "2014" only; and it shows the chart name to be "2014" so it isn't necessary to have "Chart" in the name. Also if I manually type "2014" in the ChartObjects in the code, this will work as well.
Again I got it to work, but I want to know why it doesn't work and is there a way to leave the charts named only "2014" rather than "Chart 2014" which would make my code simpler in other macros.
This is my first time on this forum so I hope I did everything correct...but correct me if there is something I did wrong.
Thanks!