Activechart.name is throwing 'out of memory' error every time - vba

I have made multiple excel files trying to solve this problem. I have gotten to this point and am still getting an error for this function:
Sub graph1()
ActiveChart.Name = IChart
MsgBox "done"
End Sub
This is in a macro enabled excel file with numerical-only data on Sheet 1 in the range A1 to F754 and a (manually made) Smooth Scatter Graph on Sheet 2.
Trying to run this code (with the chart selected to make ActiveChart effective) I am getting the error:
Run Time Error '7':Out of Memory
Please help, I need to be able to name the chart so I can make .Axis formatting changes, re-size the chart, and some other things with a VBA macro.

How about assigning the variables something like the below, and making the changes as described:
Function test()
Dim ic As Chart
' Somehow assign the chart to a variable
Set ic = ActiveChart
' Change the name as desired
ic.Parent.Name = IChart
' Change the axes as desired
ic.Axes(xlCategory).CrossesAt = -350
End Function

Don't mark this as answer since Demetri answered it correctly in the comments.
He is correct that you should use:
ActiveChart.Parent.Name = "ChartName"
Or you can use a variable that contains your name.
Why use Parent property? The reason is ActiveChart points to Chart property of the ChartObject and you can only set the Name property on ChartObject.
So basically,
Dim Co As ChartObject
Set Co = ActiveChart.Parent
Debug.Print Co.Name 'retrieve chart object name
Co.Name = "Chart Name" 'assign name
I hope this clear things out.

Related

How to assign a selected chart name to a cell (not the other way around)?

I'm still a beginner with VBA and I'm learning a ton from stackoverflow and from general googling.
I'm hitting a wall on this very general task : I'm trying to show a text giving a very general explanation of a chart when it is selected / hovered-over.
The way I was thinking of approaching this was to create a tab with all my chart names (which I already have for other tasks) and create a little text for each of them. A cell (the VBA part) would contain the selected chart name that I could use to do a simple vlookup to fetch the explanation.
I tried to look on google how to do this and I'm usually pretty successful with forums and such, but there are sooooo many information on how to name a chart name based on a cell that I can't seem to find information on how to name a cell based on a chart name.
Edit : was cut off while typing by my newborn waking up, my bad completely forgot to come back and add my attempted code !!!
Sub Test_Chart_Name()
Dim T As String
T = ActiveChart.ChartTitle.Text
Range("AM41").Value = T
End Sub
So far it works when I run it, I do believe I should be able to make it run automatically whenever I select a new chart but right now the wrong behavior is that it display the chart title instead of the name I assigned to it (ie it paste 'Pay per month in dollars' instead of 'Monthly_pay'.
Here's how I approached the request. I wrote a macro that looks up the description of the chart, and displays it in a message box. For each chart you want to run it with, right click the chart, click Assign Macro from the pop-up menu, and select the macro. When you click on the chart, the macro runs. You can also run the macro anytime from Developer tab > Macros or the shortcut Alt+F8.
I set up a lookup range on the active sheet (it could be anywhere) with chart names in the first column and descriptions in the second.
Sub PopUpChartDescription()
Dim rTable As Range, rCell As Range
Dim sName As String, sDescription As String, sCaller As String
On Error Resume Next
sCaller = Application.Caller
If Len(sCaller) > 0 Then ' macro called by clicking on a chart
' activate the chart or it is deactivated after the macro runs
ActiveSheet.ChartObjects(sCaller).Activate
DoEvents
DoEvents
End If
On Error GoTo 0
If Not ActiveChart Is Nothing Then ' so ActiveChart is something, eh?
sName = ActiveChart.Parent.Name
Set rTable = ActiveSheet.Range("DisplayTable") ' my lookup range
Set rCell = rTable.Columns(1).Find(What:=sName) ' find the chart name
If Not rCell Is Nothing Then ' so rCell containing chart name was found
sDescription = rCell.Offset(, 1).Text
' show the description
MsgBox sDescription, vbInformation, "Chart Description"
End If
End If
End Sub
You could try the code below:
Sub Test_Chart_Name()
Dim T As String
T = ActiveChart.Parent.Name
Range("AM41").Value = T
End Sub
Hope that helps!

Is there any way to adjust the sequence of LegendEntries (Series) in Excel chart by VBA?

I'm working on an Excel chart automation. When I try to move up a LegendEntry(Series) object, I cannot find any related API method from MS site. The manual way is like this: Select the chart > Right click > Select Data... > Choose one of the LegendEntry > Click button Move Up. How could I do this by VBA? Thank you in advance.
Here is another way
Sub Sample()
Dim ws As Worksheet
Dim objChrt As ChartObject
Dim chrt As Chart
Set ws = ActiveSheet
Set objchart = ws.ChartObjects(1)
Set chrt = objchart.Chart
chrt.FullSeriesCollection(2).PlotOrder = 1
End Sub
Screenshot
The legend entry order is the same as the series order. I'm not sure if this is even exposed in the object model. You can change the number of the last parameter of the series formula to re-order the series and the legend entries alike.
ActiveSheet.ChartObjects("Chart 1").Activate
Debug.Print ActiveChart.FullSeriesCollection(1).Formula
This will get you something like
=SERIES(Sheet1!$B$6,,Sheet1!$C$6,1)
The 1 as the last parameter means that it is the first series in the chart and hence the legend. Change that parameter to move the legend entry to a different position.

Error in script which copies charts from Excel to PowerPoint

I am attempting to call the below Sub in order to copy given chart to a specified PowerPoint presentation. However, when I run the macro which calls this Sub, the line indicated below returns the following error: "Object doesn't support this property or method." What's odd is that both Shapes and Slide do contain the methods which are called. As well, the bitmap is correctly copied to my clipboard and pastes into the slide before the error is called. You will find the Sub() below.
Sub copyChart(chrt As Chart, pres As PowerPoint.Presentation)
Dim curSlide As Slide, dummySlide As Slide
Set dummySlide= pres.Slides(2) 'Second slide is dummy slide.
Set curSlide = dummySlide.Duplicate(1) 'Duplicate dummy, set as current slide.
chrt.CopyPicture Appearance:=xlScreen, Format:=xlBitmap 'Copy the chart as a picture.
curSlide.Shapes.Paste '<-----------Error here.
End Sub
As well, I was hoping to provide a .txt file of my entire script, but was unsure how (it is a little lengthy to paste here). Thanks for your help.
(Note that this implementation is very similar to that at Paste Excel Chart into Powerpoint using VBA, further confusing me.)
I have had a lot of trouble in recent versions of Office. 2003 and earlier didn't have this problem, 2007 and 2010 had it a bit, and 2013 and 2016 have it in spades.
When you step through the code it works fine, but when you run it at full speed, it errors on the paste.
It's as if the copy doesn't have time to finish finish, so when you paste the clipboard doesn't have anything in it yet to paste.
Sometimes this helps:
chrt.CopyPicture Appearance:=xlScreen, Format:=xlBitmap
DoEvents
curSlide.Shapes.Paste
DoEvents tells VBA to wait while background operations have a chance to finish up.
It looks like the error can be attributed to how VBA handles variables across different references. (In particular, how PPT VBA handles them.) I was able to get the macro to work by actively selecting/copying the charts. I will need to do a little more research to get why variables cause problems, but at least I know how tackle the problem.
Sub copyChart(curSlide As Slide)
Dim chr as ChartObject
Set chr = Sheets("CHARTSHEET").ChartObjects(1)
Sheets("CHARTSHEET").Select
ActiveChart.CopyPicture
curSlide.Shapes.PasteSpecial
End Sub
I like to use another method, I like to define an Object, then set it to the pasted Chart. Afterwards, it's much easier modifying the pasted Chart object's parameters inside PowerPoint (from Excel).
See code below:
Sub copyChart(curSlide As Slide)
Dim chr As ChartObject
Dim myChart As Object
Set chr = Sheets("CHARTSHEET").ChartObjects(1)
chr.Copy
' setting myChart object to the pasted chart (let's me later an easy way to modify it's parameters)
Set myChart = curSlide.Shapes.PasteSpecial(ppPasteBitmap, msoFalse) ' can change first parameter to other avaialabe formats : ppPasteGIF, ppPasteEnhancedMetafile, ppPasteOLEObject, etc.
' set different parameters for the pasted chart in PowerPoint slide
With myChart
.Left = 200
.Top = 200
End With
End Sub
In the code line:
Set myChart = curSlide.Shapes.PasteSpecial(ppPasteBitmap, msoFalse)
You can change the first parameter in brackets: ppPasteBitmap to many other avaialble formats (test them and see which one gives you the best result), such as: ppPasteGIF, ppPasteEnhancedMetafile, ppPasteOLEObject, etc.

method in deleting a line excel VBA

I would like to know the best way to delete a line in excel by using vba
For my project, every specific line is link with a graphic, when i delete the line by using the method
Rows(i).delete
It appears in my graphic #REF
I've tried the methode
Selection.Insert Shift:=xlUp
But when I entered data to the same line, it will not used the color which i had attributed to the data on the line
thank you very much
By "graphic" I assume you mean Chart. In Excel, if you delete a range on which a series in a chart is based then the series will remain on the chart but display #REF! in the series list. So in addition to deleting the row you will also have to remove the series from the chart. Let's say the name of the chart is "Chart 1":
Dim co as ChartObject
Dim c as Chart
Set co = ActiveSheet.ChartObjects("Chart 1")
Set c = co.Chart
On Error Resume next
Do While Err.Number = 0
c.SeriesCollection("#REF!").Delete
Loop
On Error GoTo 0
By placing the above code after your Rows(i).Delete the related series in the chart will also be removed.
Have you tried to use ClearContents?
Something like this:
Rows(i).ClearContents
This method doesnt clear formats or other things, just the values or formulas.

Recorded macro for data labels does not work

So, I'm a novice at using VBA but not so new that I know not to use .select and the likes wherever possible but I will still record macros to find out how to call certain objects. I am working on a large piece of code to do various things before manipulating a chart at the end, it wasn't working so breaking it down into a new spreadsheet I found that some of my lines for manipulating chart labels were giving me an error. I recorded a macro to make sure I hadn't mis-typed anything, but could not fix it, then tried to run the recorded macro, but couldn't get it to work.
Heres the aim of my code:
1. Apply data labels to one point in a series.
2. Delete the label I just applied
(in the final code there will be "if" functions ect)
Here is the macro code, straight from the recorder:
ActiveSheet.ChartObjects("Chart 1").Activate
ActiveChart.SeriesCollection(2).Select
ActiveSheet.ChartObjects("Chart 1").Activate
ActiveChart.SeriesCollection(2).Points(1).Select
ActiveChart.SeriesCollection(2).Points(1).ApplyDataLabels
ActiveSheet.ChartObjects("Chart 1").Activate
ActiveChart.SeriesCollection(2).Points(1).DataLabel.Select
Selection.ShowValue = 0
Selection.ShowCategoryName = -1
' this line ^ gives error 438, object does not support this property or method
ActiveSheet.ChartObjects("Chart 1").Activate
ActiveChart.SeriesCollection(2).Points(1).DataLabel.Select
Selection.Delete
What's also very curious is that sometimes rather than Selection.ShowValue = 0 it will use Selection.ShowValue = False
Anyone know why the recorded code for this is so jumpy? Also if anyone could suggest simpler code for manipulating data labels that would be very useful.
I am using excel 07 on windows 7.
Charts in VBA have always been a pain in the rear for me. That being said, here's some code that tosses a few of the more common objects into variables (so you can break the code on a line and see what's going on in the "locals" window). This will iterate through all of the points on your chart and add a datalabel containing the "Value".
Sub test()
Dim myChart As ChartObject 'A "ChartObject" contains a Chart, in which the "SeriesCollection" resides
Dim chartSeries As Series 'Multiple "Series" can be found in a "SeriesCollection"
Dim scPoint As Point
Set myChart = ActiveSheet.ChartObjects("Chart 1")
Set chartSeries = myChart.Chart.SeriesCollection(1) 'Get the first "Series" in the "SeriesCollection" for this chart
'loop through the points in this Series. As it loops the point will be available in the "scPoint" variable.
For Each scPoint In chartSeries.Points
With scPoint.DataLabel 'Finally the "DataLabel" is part of the "Point"
.ShowValue = True 'Just show the value. There are other options here, just create a new line and start typing with a period to see what other options are available for a "DataLabel"
End With
Next scPoint
'Instead of iterating, if you just want to address a single point's datalabel then:
chartSeries.Points(2).DataLabel.ShowValue
End Sub