I am making a report on Excel that will create graphs for the user based on data entered into the spreadsheet. I am quite new to VBA and, whilst I think I'm getting the hang of some basic expressions and structures, I'm definitely having trouble getting my head around the options available for coding graphs.
Basic Google searches produce lots of guides, the following being examples:
https://peltiertech.com/Excel/ChartsHowTo/QuickChartVBA.html
https://www.thespreadsheetguru.com/blog/2015/3/1/the-vba-coding-guide-for-excel-charts-graph
...and searches on Stack Overflow and looking through the Microsoft help pages provide a certain amount of insight. However, there seem to be several ways of making graphs using VBA, and I'm struggling to understand the nuances in different examples of code.
For example, I'm hesitant about which variables I should use for the chart. What might be the consequences in using Dim cht As Chart as opposed to As Shape or As ChartObject? I imagine this might inform the expressions I'd consider using to create the actual graph - which, according to the examples, seem to include:
Set cht = ws.Shapes.AddChart2(201, xlColumnClustered)
Or:
ws.ChartObjects.Add(*Left, Top, Width, Height*)
Or:
Set chtSht = Charts.Add
...Each of which seem quite different and I ultimately don't understand what purposes the differences serve.
I have a hunch it's mostly to do with whether the charts are embedded or not, though I'm not sure of the extent to which this would inform the code.
So, for those who are more experienced than I am and can probably address these concerns more efficiently than I can digest Google search results: How should I go about creating graphs in Excel using VBA?
ChartObject(which is basically just a container for an actualChartobject) to a sheet. The last one creates a chart sheet (ie not a chart embedded on a worksheet) Any of those approaches are fine - use whatever works for you.