VBA run-time error "7" when naming xlXY ScatterLines chart - excel

I'm running a code that requires me to name a chart so I can refer back to it later. The code I use for this is:
Sheets("Summary").Activate
ActiveSheet.Shapes.AddChart.Select
ActiveChart.ChartType = xlXYScatterLines
With ActiveChart
.Name = "energy distribution chart"
End With
I've tried just naming the chart not using the with... as in:
ActiveChart.Name = "energy distribution chart"
Doesn't work also. Whenever the code runs at that line I get a "run-time error 7 - Out of memory". I've tried putting this code so that it starts to run in a new macro in a new module but it still always gets that error 7 - 3 lines into a new piece of code! I have no idea what's going on. Help?
Edit: Following to mehow's answer, how do I then re-select the chart?
I've tried:
With Worksheets("Summary").ChartObjects("energy distribution chart").Chart(1)
.SeriesCollection(sheet_number - 1).XValues = Worksheets(sheet_number).Range(Sheets(sheet_number).Cells(1, 7), Sheets(sheet_number).Cells(i4, 7))
.SeriesCollection(sheet_number - 1).Values = Worksheets(sheet_number).Range(Sheets(sheet_number).Cells(1, 8), Sheets(sheet_number).Cells(i4, 8))
End With
Which doesn't seem to work... .. hm. I've also tried getting rid of the (1) next to charts. Yep. No idea.

Chart it's an embed object. If you want to change of the object ( not chart itself ) then
ActiveChart.Parent.Name = "chart name"
See MSDN for more details

Related

How to change the subtype of Chart from VBA code

I create charts from some data:
ActiveSheet.Shapes.AddChart2(297, xlColumnStacked).Select
But on Excel there are different subtypes of the xlColumnStacked chart...
I need to show the second one, not the default one.
It has to be programmatically, not manually.
I tried to record the macro and see what does the change, but it doesn't record anything... I tried loading from Templates also but they don't work exactly like the one I need.
Thanks for your support.
I thing you have to change .ChartTyp
for example:
ActiveChart.ChartType = 52
Here is list of proper Value
https://bettersolutions.com/excel/charts/vba-chart-types.htm
If you want to switch x axis and y axis data try one of this:
ActiveChart.PlotBy = xlColumns
ActiveChart.PlotBy = xlRows
I hope I helped :)
They both looked like stacked columns. VBA doesn't have chart subtypes, and controls the chart type using this syntax:
ActiveChart.ChartType = xlColumnClustered
ActiveChart.ChartType = xlColumnStacked
ActiveChart.ChartType = xlColumnClusteredStacked100
VBA's Object Browser shows you all of the possibilities.

DialogSheets("Name").Labels.Visible = True not working on all DialogSheets in Excel VBA

I've been given an ancient but, for the most part, working Excel solution.
It uses a lot of DialogSheets (an Excel 5/95 technique before UserForms).
Now I'm trying to figure out what the author tried to do, the code is pretty horrible. One problem I stumbled across is this:
Some sheets work fine with code like:
DialogSheets("Name1").Show
DialogSheets("Name1").Labels.Visible = True
DialogSheets("Name1").EditBoxes.Visible = True
DialogSheets("Name1").CheckBoxes.Visible = True
DialogSheets("Name1").DropDowns.Visible = True
On others, I get a:
runtime error 1004 "Unable to set the Visible property of the Labels class"
I cannot quite work out the difference, and the documentation on Dialogsheets is pretty sparse. If anyone had any idea I'd appreciate a suggestion.
Update
The answers suggest that some DialogSheets may not have Labels, thus throwing the 1004. This is not the case. This happens in the ImmediateWindow:
?DialogSheets("Name1").Labels.Count
205
?DialogSheets("Name2").Labels.Count
7
DialogSheets("Name1").Labels.Visible = True ' Throws 1004
DialogSheets("Name2").Labels.Visible = True ' executes fine
?DialogSheets("Name2").Labels.Visible = True ' returns true
True
The code above sets the visibility of the Labels, DropDowns, EditBoxes and CheckBoxes to True.
It throws error 1004, because on some DialogSheets the Labels, DropDowns, EditBoxes or CheckBoxes are missing. And it works on those, where all 4 are present.
To see it working, close all Excel applications. Then open a brand new one and run this code:
Sub TestMe()
Dim someDialogSheet As DialogSheet
Set someDialogSheet = ThisWorkbook.Sheets.Add(Type:=xlDialogSheet)
With someDialogSheet
.Name = "SoReadyToHelp"
.Labels.Add(5, 5, 5, 5).Select
.DropDowns.Add(10, 10, 10, 10).Select
.CheckBoxes.Add(15, 15, 15, 15).Select
Debug.Print .Labels.Visible
Debug.Print .CheckBoxes.Visible
Debug.Print .DropDowns.Visible
.Show
End With
End Sub
Or there is On Error Resume Next somewhere on the code where it works.
The code will fail if there are no labels to make visible.
Add a check like this, to make sure there are any before making them visible:
If DialogSheets("Name1").Labels.Count > 0 Then
DialogSheets("Name1").Labels.Visible = True
End If
Edit after comment
Then I suggest you try something like this:
For Each lbl In DialogSheets("Name1").Labels
lbl.Visible = True
Next

Macro to format y- axis in Excel

I've written some code to create a second y-axis in a chart. I recorded a macro when I formated the second y-axis and the code looks great. However, it doesn't work when I try to run it from the VBA code..
I've tried the Format Painter (the first y-axis), which would be optimal if it worked, and also by formating the second y-axis "by hand"
This is what I have (and it's doesn't work..)
Formating the second y-axis to Calibri ("by hand"):
ActiveChart.Axes(xlValue, xlSecondary).Select
Selection.Format.TextFrame2.TextRange.Font.Name = "+mj-lt" 'This line doesn't work...
Selection.Format.TextFrame2.TextRange.Font.Size = 14
Error code: Method TextFrame2 of Object 'ChartFormat' failed
Using Format Painter:
ActiveChart.Axes(xlValue).Select
Selection.Format.PickUp 'This line doesn't work...
ActiveChart.Axes(xlValue, xlSecondary).Select
Error code: Object doesn't support this property or method.
Does anyone know why none of the methods work or if there's a work around?
I found a work around by using:
With ActiveChart.Axes(xlValue, xlSecondary).TickLabels.Font
.Name = "Calibri"
.Size = 16
End with
I haven't been able to solve the issue with Format Painter though...

Access Error: Object variable or With block variable not set (Error 91)

I Know similar questions have been asked, but I still can't figure out this error.
I am working in Access, exporting to excel and used some excel macros to format the data and create charts. I am now trying to put those charts into a Powerpoint, and I am having trouble pasting only one of the four charts I am trying to copy over.
My code to export the third and fourth charts is as follows:
xl.Sheets("Sheet with chart 3").Select
Set rng = xl.ActiveChart
rng.CopyPicture
mySlide.Shapes.PasteSpecial DataType:=ppPasteEnhancedMetafile
Set myShapeRange = mySlide.Shapes(mySlide.Shapes.Count)
myShapeRange.Left = 365
myShapeRange.Top = 200
myShapeRange.Width = 345
xl.Sheets("Sheet with chart 4").Select
Set rng = xl.ActiveChart
rng.CopyPicture
mySlide.Shapes.PasteSpecial DataType:=ppPasteEnhancedMetafile
Set myShapeRange = mySlide.Shapes(mySlide.Shapes.Count)
myShapeRange.Left = 365
myShapeRange.Top = 200
myShapeRange.Width = 345
I get the error "Object variable or With block variable not set (Error 91)" on the second occurrence of the line
rng.CopyPicture
So why would this code fail after running three times prior? I believe I qualified everything correctly, and this code isn't running inside a with block either. Also, The excel sheet it is pulling from is practically identical to the others.
I had been stuck on this for awhile, the problem was caused by this line:
xlSheet.Range("G7").Select
which was the last line during the formatting on the excel book. I had recorded macros in excel to make the charts and pasted the resulting code in Access. I guess having this cell selected was preventing the chart from being set to active, so I couldn't copy it. Hopefully this helps someone having the same silly problem I was!

Accessing PivotChart SeriesCollection with a Sub

I am quite new to VBA. I have written a macro which creates about 10 pivot charts and some normal charts after filtering and cutting some data from a database spreadsheet. I now want to write a sub which goes through applying the same formatting to each one. The sub is as follows:
Sub FormatChart(Cht As ChartObject, title As String)
Cht.Activate
MsgBox ActiveChart.SeriesCollection.Count
With ActiveChart
.HasTitle = True
.ChartTitle.Text = title
.Axes(xlValue).HasMajorGridlines = False
End With
ActiveChart.SeriesCollection(1).Select
With Selection.Format.Fill
.Visible = msoTrue
.ForeColor.RGB = RGB(255, 182, 0)
End With
End Sub
I originally didn't include all the activates and selects, but couldn't get the macro to work without them and don't see it as the end of the world - the datasets are never going to be massive so speed isn't so much of a concern, and I disable screenupdating so that users can't click on cells/other objects and disrupt the macro as it runs.
Here's my problem. If I take out the second With loop, everything proceeds perfectly and the gridlines are removed and the title is added. However, whenever I try to edit the colours of columns with the above I get Run time error '1004': Invalid parameter. I've also tried keeping the content of the second with loop inside the first but then moved it out to try using selection to see if it made a difference.
I've fiddled around quite a bit and recorded various macros changing the colour of the chart in question, but I think the problem might be to do with referencing SeriesCollection as when I try to debug with
MsgBox ActiveChart.SeriesCollection.Count
I get "0".
Please let me know if I'm missing the point - as I said I am new to VBA and am trying to learn as much as possible :)
EDIT: The solution was that I was passing each chart to the above sub after I had created the chart, but before I had set a data source for the chart. Doh!
This obviously meant that there were no seriesCollections for that chart, hence the error I was getting.
I marked Joehannah as answering the question (even though it isn't technically the solution) because it made me check my code and notice that the above could be causing the problem - if I shouldn't do that someone please tell me and I'll try to fix it!
You are better off using Set cht = (create chart object) for each chart and then Immediately calling the format method passing in cht.
I have been told to post my own answer since I figured out the issue.
I was passing each chart to this function just after creating it, as follows:
Set ptr1 = Sheets("withinBRSPivots").PivotTables("UniqueClicks").TableRange1
Set cht1 = Sheets("BRS Overview").ChartObjects.Add(Left:=950, Top:=500, _
Width:=400, Height:=300)
cht1.Name = "UCChart"
Charter.FormatChart cht1, "Average Number of Unqique Clicks"
I then set the source data for the chart after doing the above. This meant that when the chart was passed to my chartformat sub, it had no source data. This is what resulted in being able to edit the title and gridlines, but not the seriesCollection. The fix was to just move the FormatChart sub line to after I had set the source data.
Many thanks to everyone who posted answers!

Resources