Gridline object... How to retrieve the reference to the worksheet? - excel

how to retrieve the reference to the chart and to the worksheet where the chart of the gridline object is, starting from the object itself?
For example, if I have a Range object, the object.Parent is the reference to the Worksheet.
How to do it if the starting object is the gridline object of a chart embedded into a worksheet?
Thanks in advance to everyone can help to put me in the right direction

I start using the answer of the post stackoverflow.com/questions/24045305. Then I added a For.. Next cycle through the worksheets collection (I don't have only one sheet and it is not always the same Chart of interest).
Furthermore, because of the AxisTitle caption is currently meaningful for me and its value is based on a cell formula result (it can't be prefixed and formatted to be white and the rest leaved to be automatic), I solved the problem adding a textbox whose value is now the cell formula result and the AxisTitle caption contains now only the unique identifier formatted to be white.

Related

How to control excel chart source range through VBA

I want to be able to control and update the source (range) of an excel chart through VBA. The chart is ultimately going to be presented in a powerpoint presentation and the whole process shall update automatically.
I have tried using tables which doesn't give me the level of control I want. For example if i delete one datapoint the chart doesn't "shrink" accordingly.
ActiveChart.SetSourceData (Sheets("Sheet2").Range("E5:E9"))
The current script doesn't run. But I just want to "set" a source range for a particular chart, preferably accessed by name. And if new datapoints are added I just run the script again and the new range will be the top cell in the column down to .End(xlDown)
Thanks guys! I manged to find a solution that works for me and made a function of it, if anyone else has the same problem. Nothing fancy but you can set a range like "A2:A14" and the chart will update with the new range. The function is called by sheet name, chart name and range like below:
Call changecharts("Sheet2", "Chart1", "A2:A11")
Public Function changecharts(sheeet As String,chartname As String, rnge As String)
Worksheets(sheeet).ChartObjects(chartname).Activate
With ActivateChart
ActiveChart.SetSourceData Source:=ActiveWorkbook.Sheets(sheeet).Range(rnge)
End With
End Function

Dynamic Range for Intentionally Showing Nothing

So, I've created a dynamic range for a chart, that's all well and easy.
However, in this chart there are two lines, but I only want one of the lines to show up under certain conditions, else it displays nothing! So I've tried creating my dynamic range as follows
=IF('WorksheetName'!$M$10 ='WorksheetName'!$F$31,'WorkSheetName'!dynamic_range, #N/A)
The problem is that when I do this the chart freaks out. It gives me this error:
Your formula contains an invalid external reference to a worksheet.
Verify that the path, workbook, and range name or cell reference are
correct, and try again.
If I click "ok" half the time it shows up correctly (that is, the second line disappears and the chart adjusts accordingly) and the other half the time it glitches.
Basically, how do I create a dynamic range for graphing that the chart will understand when I want it to do NOTHING and when I want it to display the range?
You need a second source range, that's cells are just empty. Applying your approach to switch between the filled range (intended to be visible) vs. the empty range (will be invisible), shall solve the issue. Note: The chart parameter "Show empty cells as:" should be set to "Gaps". (Refer to the Hidden and Empty Cells options in the chart's Select Data dialog. This is applicable to X/Y charts mainly.)

Referencing sheet in excel

I am new to excel vba and I have some questions regarding referencing a worksheet
I noticed that when I used
Worksheets(3)
The worksheet would be obtained according to the sequence of the worksheet in the workbook
When I used
Worksheets("Name")
It would be retrieved according to the name of the worksheet
However, I found that both approach is troublesome because for method 1, I need to fix the sequence of the worksheet. Once I dragged the worksheet around, the reference would become incorrect.
Method 2 would need me to fix the work sheet name , which is not that flexible.
I noticed that at the left panel of VBA editor, under the Microsoft Excel Objects, whenever the worksheet is created, a new sheet like
Sheet1 (Name) would be created.
Is there any way that I could reference the worksheet based the the Sheet1 variable, which I could fixed it so that I could freely drag the sheet around or change the worksheet name?
Thanks.
The name you refer to is called the CodeName. You can refer to a sheet by this name.
Eg, for your example Sheet1 (Name) can be referenced as
Worksheets("name")
or
Sheet1
Eg Worksheets("name").Activate or Sheet1.Active would both work
Note that you can change this name to something meaningful in the Properties window of the VBA IDE, but you can't change it at run time

access a chart's shape ID - excel vba

Some background first.
Excel allows duplicate names for shapes. That is, you can have both a ChartObject and an oval shape in the same worksheet with exactly the same name. You can also have two charts named both "Chart 2". If you try to reference a shape with a duplicate name, e.g.
ActiveSheet.Shapes("Dupe").Select,
excel seems to resort to returning the object with the lowest ID (and the duplicate name).
There is no way (that I know of) of linking an ActiveChart with its corresponding containing shape.
I want to create a function like
function GetAChartsShape(c as chart) as Shape,
but I don't know how. The immediate use for this would be to format the selected chart (since there is no way of globally changing a chart's font). Of course, this could also have other uses.
The name of the shape containing an embedded chart (the shape is also the chartobject) is:
activechart.parent.name
or if c is declared a chart:
c.parent.name
But of course you know you don't need to select an object to work on it, so just do what you need to do on
c.parent
which avoids the problem of duplicate names.

How to insert an excel chart into Word using AddOLEObject

I'm trying to create a linked OLE Object in a Word document using VB.Net.
Existing code uses InlineShapes.AddOLEObject(FileName:="abc.xlsx", LinkToFile:=True, Range:=Some Word Range) to insert a worksheet into a Word document.
I need more control than this. To select a range of cells I've found that extra information after the filename can be useful, for example: FileName:="abc.xlsx!sheet1!R1C1:R20C5"
Is there a way to specify a specific chart within a worksheet? So can I specify the second chart on the worksheet as the object to link to?
Thanks.
Thanks for your help Mark.
I eventually figured out that if the Chart is in it's own sheet, rather than an object in Sheet1, then the AddOLEObject code works correctly with the following setting:
FileName:="abc.xlsx!Chart1"
I'm happy with this solution.
A chart will either be a whole worksheet so address as per your sheet1 e.g. abc.xlsx!sheet1 or an object on a sheet so use the object name e.g. abc.xlsx!sheet1!chart_object

Resources