Counting Chart series from a worksheet [by name] - vba

I have searched through the forums and I cant find an exact solution to my problem. I am somewhat familiar with vba and coding in general but I am still very new to the VBA syntax. I keep getting errors along the times of syntax error, object not in range, etc. I want to get away from selection based actions and I want to call specific charts from sheets to count the amount of data series within.
It seems so simple and at this point im just frustrated I havent been able to actually debug it. I have tried a lot of different combinations and have been Googling all morning and all I get is new and different error messages. So I figured one of you may have a quicker solution than my tinkering. Any help would be super helpful, thanks!
Dim SheetName As String
Dim SC2 As Long
SheetName = ActiveSheet.Name 'assign the name of the active sheet to the variable
'Count # of series in chart to find # of loops required
ActiveWorkbooks.Sheets(Volume CT).ChartObjects(1).Chart.SeriesCollection.Count
'*^^^THIS IS WHERE THE ERROR OCCURS*
'Debug.Print ThisWorkbook.Sheets("Time CT").ChartObjects(1).Chart.SeriesCollection.Count
'Debug.Print ThisWorkbook.Sheets("Temp CT").ChartObjects(1).Chart.SeriesCollection.Count

Dim SC2 As Long
'chartobject method (chart is housed in a regular worksheet)
SC2 = ActiveWorkbook.Sheets("Volume CT").ChartObjects(1).Chart.SeriesCollection.Count
'if the chart is on a chart sheet:
SC2 = ActiveWorkbook.Sheets("Volume CT").SeriesCollection.Count

Related

Looking for matching data in two workbooks of Excel, and format the matching results over a large data set

I am working on a problem in Excel, in which I have to compare data in two separate workbooks, and look for matching pieces of information over several columns - as the time stamp in row A isn't an exact match in both workbooks I need to rely on the data points from the other columns out to column G - and format the rows in which a match is found in a certain colour.
I could do this manually, but due to the amount of rows numbering into 5 figures, I think VBA seems the best way to do this. Having only basic programming knowledge, I am struggling to see the correct way to do this with VBA.
Here is what I have tried so far, and I will also include the error shown when I run the code.
I am aware I said that more than row A is needed to make the comparison, but I am not sure on how to apply the rules to a matrix over columns and rows.
Sub vbax_53997_Compare_Two_Ranges()
Dim i As Long
Dim wb1ws1, wb2ws2
Dim blnSame As Boolean
wb1ws1 = Workbooks("Copy_of_data.xlsm").Worksheets("Worksheet").Range("A1:A63").Value
wb2ws2 = Workbooks("Copy_of_data_to_be_compared.xlsm").Worksheets("Archive").Range("A1:A23067").Value
For i = LBound(wb1ws1) To UBound(wb1ws1)
If wb1ws1(i, 1) = wb2ws2(i, 1) Then
blnSame = True
End If
Next i
If blnSame = True Then
Sheets(sheetName).Cells(lRow, "A").Interior.ColorIndex = 3 'Set Color to Red'
End If
End Sub
This is the error when I run the code;
Run-time error '9': Subscript out of range.
I have no doubt its an easy fix and I could maybe do a nested vlookup/if statement, but taking longer in the short term to find a solution will probably be a benefit to automate the process in the long term.
Any help would be much appreciated as I have now run out of ideas.

Filling a variant array with charts in Excel VBA

I am working on a file that creates up to 120 charts based on data, variables, and format selections from the user. To do this I create a variant array to hold the charts which allows me to easily reference them for adding data, formatting, etc. This method has worked well for me so far.
Now I would like to let users make small tweaks to formatting (adjust the min and max on the axis, add or remove legend entries, etc.). To do this I would like to continue referencing the charts from an array, but I can't seem to add the existing charts to the variant array.
When I initially create the charts I use this line to add the chart to the array when it is created. I fill in appropriate parameters to place and size the chart and this seems to work fine.
Set charts(graphIndex) = activeSheet.ChartObjects.Add(...)
After creating all the charts, I think the non Global variables used are cleared from the cache (at least that is my current understanding). This means that in order to make these tweaks I need to reinitialize and redefine the variant array that I use to reference the charts. This is what I am struggling with. This is my current attempt to add the existing chart to the variant array.
charts(graphIndex) = Worksheets(activeSheetName).ChartObjects("chart name").Chart
When I run the code I am getting a "Run-time error '438': Object doesn't support property or method."
Hopefully I provided enough context, but any help on this would be greatly appreciated. This feels like it should be fairly easy, but I couldn't find any information online.
I am just guessing that in your code if you had the Set word it would have worked (However, I am not seeing the whole code, thus not sure).
This works, if you make sure to have 3 charts named "Diagramm 1", "Diagramm 2" and "Diagramm 3" on the first worksheet:
Option Explicit
Sub TestMe()
Dim cht2 As Chart
Dim varArray As Variant
With Worksheets(1)
Set cht2 = .ChartObjects("Diagramm 2").Chart
varArray = Array(.ChartObjects("Diagramm 1").Chart, cht2)
ReDim Preserve varArray(2)
Set varArray(2) = .ChartObjects("Diagramm 3").Chart
Dim cnt As Long
For cnt = LBound(varArray) To UBound(varArray)
Debug.Print varArray(cnt).Name
Next cnt
End With
End Sub
The Reedim Preserve increases the array units with one additional, while it keeps what it already has. Thus, at the end this is what we have in the locals:

PivotCache Refresh from Access

I am automating some Excel file creation from Access. I need help with dynamically resetting the pivotcache for all the pivot tables. The first set of code is me testing working code in Excel. Now I want to translate this so that it runs from Access module.
Sub Update_PTSource()
With Sheets("Pivot")
.PivotTables(1).ChangePivotCache ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=Sheets("Data").Range("data"))
End With
End Sub
Function pivot_refresh_test()
Dim pt As Variant
Dim wb, ws As Object
Dim strWBName As String
Dim strTabName As String
strWBName = "C:\Users\...\Packaged SKU.xlsb"
strTabName = "Pivot"
Set wb = GetObject(strWBName)
For Each pt In wb.Sheets(strTabName).Pivottables
pt.ChangePivotCache wb.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=wb.Sheets("Data").Range("data"))
pt.RefreshTable
Next pt
End Function
I'm getting error on pt.ChangePivotCache line with error
invalid procedure call or argument
As far as I can see, you've only made one simple error:
xlDatabase is an Excel enum, and since you're using late bindings, you don't have access to it. You can use it's integer value, 1, instead:
For Each pt In wb.Sheets(strTabName).Pivottables
pt.ChangePivotCache wb.PivotCaches.Create(SourceType:=1, SourceData:=wb.Sheets("Data").Range("data"))
pt.RefreshTable
Next pt
I'm anal about using the least code possible when designing solutions so I found a much easier method to what I wanted to do.
My task was to update and refresh the source data of all my pivot tables. There was one source, multiple pivot tables. I created a dynamic named range for my source for this purpose in my template Excel book that Access was writing into. I never save over the template I just used saveas vba to create the filled copy.
However, since I had created a named range and am modifying always the same template file with Access, I don't need to write a program to change my pivotcache. I can just use 'Change Data Source' in Excel ribbon. The vba code gets much easier. It is just a variation of: ThisWorkbook.RefreshAll. No code loops required!
The whole power of my strategy is the formula that creates my named range. =Data!$A$5:OFFSET(Data!$A$5,COUNTA(Data!$A:$A)-1,52). You just set the first cell of your data, and set how many columns there are for your fields, the rows are automatically adjusted.
I hope this little rant help whoever navigates here. I've been searching a lot for good dynamic pivot table automation but most of the internet has crap info on this.

Get Source Range of a PivotTable

I am building a set of reports to be updated daily for some users who are not super savvy when it comes to pivot charts and pivot tables in Excel (2010). I have data from an already automated data pull from our db, but to make that information useful (and more simple to use), I want to generate the pivots using VBA.
I have already generated the pivot tables just fine and can manually manipulate them to create the relevant charts. As I create the tables (all on one sheet), I am labeling them (if that helps solve my problem). Where I am running into trouble is creating the pivot charts, and specifically, setting the data source.
Since the charts are based on pivots that may change in size depending on the data present, I can't just assign a fixed range as the source. Same problem with a named range, as I still don't know what range to assign a name, unless I can assign a PT as a range.
I've tried something like this, but that didn't work, since I'm not looking at an actual 'Range' object:
Dim MyChartObject As ChartObject
Dim MyChart As Chart
'These variables are assigned elsewhere in the code.
Set MyChartObject = wksMainCharts.ChartObjects.Add( _
iChartLeft, iChartTop, iChartWidth, iChartHeight)
Set MyChart = MyChartObject.Chart
MyChart.SetSourceData wksMainPivot.PivotTables(PivotName).SourceData 'Fails; requires Range object
'More code to follow...
I do know that this is a string representation of the wrong range, but I was playing with what I could figure on my own.
Essentially what I want is the opposite of this question, which seeks to find the pivot table at a specified range. I also reviewed this question, but it's not quite what I'm looking for (there are also no answers provided).
Also: I'm asking here for a way to get the range of a PT, which I think would be interesting to know, but ultimately, I just want to create the pivot chart, so if anyone wants to offer a better method than what I've started on, I'm completely open to that as well.
Is this what you are trying to achieve?
Sub Sample()
Dim Chrt As Chart, pvtTbl As PivotTable
Set pvtTbl = ActiveSheet.PivotTables(1)
Set Chrt = ActiveSheet.ChartObjects(1).Chart
Chrt.SetSourceData Source:=pvtTbl.TableRange1
End Sub
So your this line
MyChart.SetSourceData wksMainPivot.PivotTables(PivotName).SourceData
becomes
MyChart.SetSourceData wksMainPivot.PivotTables(PivotName).TableRange1

Chart won't update in Excel (2007)

I have an Excel document (2007) with a chart (Clustered Column) that gets its Data Series from cells containing calculated values
The calculated values never change directly, but only as a result of other cells in the sheet changing
When I change other cells in the sheet, the Data Series cells are recalculated, and show new values - but the Chart based on this Data Series refuses to update automatically
I can get the Chart to update by saving/closing, or toggling one of the settings (such as reversing x/y axis and then putting it back), or by re-selecting the Data Series
Every solution I have found online doesn't work
Yes I have Calculation set to
automatic
Ctrl+Alt+F9 updates everything fine, EXCEPT the chart
I have recreated the chart several times, and on different computers
I have tried VBA scripts like:
Application.Calculate
Application.CalculateFull
Application.CalculateFullRebuild
ActiveWorkbook.RefreshAll
DoEvents
None of these update or refresh the chart
I do notice that if I type over my Data Series, actual numbers instead of calculations, it will update the chart - it's as if Excel doesn't want to recognize changes in the calculations
Has anyone experienced this before or know what I might do to fix the problem?
Thank you
This is the only thing I've found to consistently update a chart. It cuts the root cause of the problem (I assume): the series data is getting cached in the chart. By forcing the chart to re-evaluate the series, we are clearing the cache.
' Force the charts to update
Set sht = ActiveSheet
For Each co In sht.ChartObjects
co.Activate
For Each sc In ActiveChart.SeriesCollection
sc.Select
temp = sc.Formula
sc.Formula = "=SERIES(,,1,1)"
sc.Formula = temp
Next sc
Next co
I have run into this same issue - not sure why, and when it happens the only way I have ever gotten the chart to force update is to change something in the chart definition itself, which can easily be done via VBA as in:
Dim C As ChartObject: Set C = Me.ChartObjects("chart name")
C.Chart.ChartTitle.Text = C.Chart.ChartTitle.Text + "1"
There may be a better answer that gets to the bottom of the problem - but I thought this might help. Working on the sheet I would do a quick Ctrl-X, Ctrl-V on a piece of the chart (or the whole thing) to force the chart to update.
I had this problem while generating 1000+ graphs through VBA. I generated the graphs and assigned a range to their series. However, when the sheet recalculated the graphs wouldn't update as the data ranges changed values.
Solution --> I turned WrapText off before the For...Next Loop that generates the graphs and then turned it on again after the loop.
Workbooks(x).Worksheets(x).Cells.WrapText=False
and after...
Workbooks(x).Worksheets(x).Cells.WrapText=True
This a great solution because it updates 1000+ graphs at once without looping through them all and changing something individually.
Also, I'm not really sure why this works; I suppose when WrapText changes one property of the data range it makes the graph update, although I have no documentation on this.
I had the same problem with a simple pie chart.
None of the macros worked that I tried. Nothing worked on cut, pasting, relocating chart.
The Workaround I found was to edit the chart text, remove the labels, then re-select the labels. Once they re-appeared, they were updated.
This is an absurd bug that is severely hampering my work with Excel.
Based on the work arounds posted I came to the following actions as the simplist way to move forward...
Click on the graph you want update - Select CTRL-X, CTRL-V to cut and paste the graph in place... it will be forced to update.
This works very well for me -- it flips axes on all charts and then flips them back, which causes them to refresh without changing at all.
'Refresh all charts
For Each mysheet In ActiveWorkbook.Sheets
mysheet.Activate
For Each mychart In ActiveSheet.ChartObjects
mychart.Activate
ActiveChart.PlotArea.Select
ActiveChart.PlotBy = xlRows
ActiveChart.PlotBy = xlColumns
ActiveChart.PlotBy = xlRows
Next
Next
This is a known Excel bug...
The best and fastest workaround is the Columns.AutoFit - Trick:
Sub Update_Charts()
Application.ScreenUpdating = False
Temp = ActiveCell.ColumnWidth
ActiveCell.Columns.AutoFit
ActiveCell.ColumnWidth = Temp
Application.ScreenUpdating = True
End Sub
I have another problem of refeshing charts. When generating the charts automatically, some charts appear over and cache the text in the sheet. It happens to be a problem of refreshing the generated charts. When I zoom in or zoom out, I can get the expected results. So I post the solution here if it interest someone.
Programmatically, I added this after generating charts :
ActiveWindow.Zoom = ActiveWindow.Zoom + 1
ActiveWindow.Zoom = ActiveWindow.Zoom - 1
Ok I have a solution, really....
I found that the problem with my charts not updating first occurred shortly after I had hidden some data columns feeding the chart, and checked "show data hidden in rows and columns" in the Chart's "Select Data Source" msg box).
I found that if I went back into the "Select Data Source" msg box and unchecked/rechecked the "show data hidden in rows and columns" that the chart refreshes.
Programatically I inserted the following into a Macro that I linked a button to, it refreshes all of my charts quick enough for a workaround to a known bug. This code assumes one chart per worksheet but another for statement for charts 1 to N could be added if desired:
Sub RefreshCharts()
Application.ScreenUpdating = False
For I = 1 To ActiveWorkbook.Worksheets.Count
Worksheets(I).Activate
ActiveSheet.ChartObjects("Chart 1").Activate
ActiveChart.PlotVisibleOnly = True
ActiveChart.PlotVisibleOnly = False
Next I
Application.ScreenUpdating = True
End Sub
I faced the same issue. The issue is due to restriction in no. of calculated formulas in your sheet. you can solved it using two ways:
Manual force re-calculate:
Press SHEFT + F9
Macro to force re-calculate:
add below code to the end of the function which changes the data
Activesheet.Calculate
I found the solution of it:
From excel options make sure to change the calculation options as below. It changed sometimes to manual after heavy work in excel.
We found a solution that doesn't involve VBA: multiplying some element of the chart's data range by TODAY()-TODAY()+1.
Even though the range was recalculating without this, the volatile nature of TODAY() somehow gives it an extra boost that triggers the chart recalc.
This problem is ridiculous! No one's solution worked for me in 2010, but I based mine off of tpascale's:
Dim C As ChartObject
Set C = ActiveSheet.ChartObjects("CTR_Chart")
C.Chart.SetSourceData Source:=Range( _
"KeywordBreakdown!$A$8:$A$12,KeywordBreakdown!$E$8:$E$12")
Simply redefined the Source Data range. If it's a named range, that could conceivably be reasonably clean. I guess the best solution to this is keep trying to modify different chart properties until it refreshes.
I had this problem and found that it was caused by having two excel applications running at the same time. If I closed everything and opened just the file I was having problems with the charts where dynamic like they should be. Maybe this helps
This worked for me, it cuts and re-pastes the charts on the active worksheet. I based this off of Jason's code and a blog post I found in a quick Google search.
Sub RepasteCharts()
Dim StrTemp As String
Dim IntTempTop As Integer
Dim IntTempLeft As Integer
Set sht = ActiveSheet
For Each co In sht.ChartObjects
'Activate the chart
co.Activate
'Grab current position on worksheet
IntTempTop = ActiveChart.Parent.Top
IntTempLeft = ActiveChart.Parent.Left
'Cut and paste
ActiveChart.Parent.Cut
ActiveSheet.Paste
'Reposition to original position
ActiveChart.Parent.Top = IntTempTop
ActiveChart.Parent.Left = IntTempLeft
Next co
End Sub
From Excel 2013 on, there is the Chart.Refreh method (https://msdn.microsoft.com/de-de/library/office/ff198180.aspx) which worked for me:
Dim cht As ChartObject
For Each cht In ThisWorkbook.ActiveSheet.ChartObjects
cht.Chart.Refresh
Next cht
Just spent half a day on this myself.
I have a macro that changes values that are the data for a chart. All worked fine in Excel 2003, but in Excel 2007 the chart seems to lose all connection to its data, although manually changing data values in two column triggered a recalc.
My solution has been to make all charts on the active sheet invisible before the change in data, then make them visible again and call chart refresh for good measure. ( It only seems to be visible charts that have this problem updating ).
This works for me and also handles similar issues with charts as well as chart objects. The refresh may not be necessary - more testing needed.
Dim chrt As Chart
Dim chrtVis As XlSheetVisibility
Dim sht As Worksheet
Dim bChartVisible() As Boolean
Dim iCount As Long
Dim co As ChartObject
On Error Resume Next
Set chrt = ActiveChart
If Not chrt Is Nothing Then
chrtVis = chrt.Visible
chrt.Visible = xlSheetHidden
End If
Set sht = ActiveSheet
If Not sht Is Nothing Then
ReDim bChartVisible(1 To sht.ChartObjects.Count) As Boolean
iCount = 1
For Each co In sht.ChartObjects
bChartVisible(iCount) = co.Visible
co.Visible = False
iCount = iCount + 1
Next co
End If
DO MACRO STUFF THAT CHANGES DATA
If Not sht Is Nothing Then
iCount = 1
For Each co In sht.ChartObjects
co.Visible = bChartVisible(iCount)
co.Chart.Refresh
iCount = iCount + 1
Next co
End If
If Not chrt Is Nothing Then
chrt.Visible = chrtVis
chrt.Refresh
If chrt.Visible Then
chrt.Select
End If
End If
On Error GoTo 0
I had the same issue as the poster. Basically I'm running a dashboard, and I have a bunch of named ranges that are populated with return values from some UDFs. On the dashboard, there are some pie charts with data series tied to cells which contain these named ranges (the problem also occurs if the data series target cells contain the UDFs directly, bypassing the named ranges).
I change a cell value which contains, for example, the date range to base the dashboard on, and the named ranges and UDFs are forced to calculate. However, the pie charts do not update--for some reason, other types of charts do. And by the way, these are chart objects, not chart sheets. Anyway, let's cut to the solution:
I didn't want to visibly change the chart title or some other aspect of it, and anyway I noticed this wasn't updating my charts consistently. Sometimes the first time I triggered the calculation the pies would update, but with subsequent calculations the pies would not. I did notice, however, that every time I made a change in the code my dashboard worked. Thus:
Solution:
With ActiveWorkbook.VBProject.VBComponents("ThisWorkbook").CodeModule
.AddFromString "'test"
.DeleteLines 1
End With
If you're using the Workbook module (I wasn't in this case), just create a new module and reference that instead.
I faced the same problem with my work last week when I added some more calculation to my sheet. After that, using radio buttons to select data to be presented on graphs did not update the graphs anymore.
The best explanation I have been able to find so far is this:
http://support.microsoft.com/kb/243495
If I understood it right, if there are more than 65536 formulas that have another cell as a reference in your file, Excel starts to optimize the calculation and in some cases graphs don't update correctly anymore.
If there is a workaround for this without using VBA macros, I would be glad to hear that (can't use those as the files need to be shared through SharePoint without VBA macros).
What worked for me was using a macro to insert/remove a column in the data table for the chart. This will cause the chart to update the data selection.
I found this to be the fastest way to fix it.
I had the same problem while working through a tutorial (very frustrating when you follow the steps and don't get the expected result).
The tutorial to create a pie chart wanted me to select range A3:A10, then also select non-adjacent range E3:E10. I did so. I got the chart.
It then asked me to change a value and watch the percentage change, then to look at the pie chart and see the update.
It didn't update.
I looked at the data source for the pie chart, and the range was bizarre. It had the A3:A10 range notated properly, but the E10 cell reference repeated several times, and it had all of the E cells listed in a random order. It looked like
=SERIES(,(Revenue!$A$3:$A$10,Revenue!$E$3,Revenue!$E$10,Revenue!$E$10,Revenue!$E$10,Revenue!$E$10,Revenue!$E$10,Revenue!$E$9,Revenue!$E$8,Revenue!$E$7,Revenue!$E$6,Revenue!$E$5,Revenue!$E$4),1
I changed the data source to read:
=SERIES(,Revenue!$A$3:$A$10,Revenue!$E$3:$E$10,1)
Problem solved. Sometimes it's a matter of cleaning up your code so the calculations processor has less to sort through.
I struggled with this problem, too. Finally solved it by recalculating the sheet that has the chart data AFTER the custom function has recalculated. So, in Sheet 1, I have a cell that contains
=ComputeScore()
In the VBA module, the function is defined as Volatile, to ensure that ComputeScore() runs after any updates to the spreadsheet.
Function ComputeScore() As Double
Application.Volatile True
. . . do some stuff to get a total . . .
ComputeScore = theTotal
End Function
Then, in the VBA of Sheet 1, this:
Private Sub Worksheet_Calculate()
'Recalculate the charts data page to force the charts to update.
'Otherwise, they don't update until the next change to a sheet, and so
'chart data is always one update behind the user's data changes.
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Calculation = xlManual
Sheets("Charts Data").Calculate
Application.Calculation = xlAutomatic
Application.EnableEvents = True
Application.ScreenUpdating = True
End Sub
So, the sheet named Charts Data, which references the custom function cell of Sheet 1, will do a recalculation AFTER the ComputeScore() function has updated the cell of Sheet 1, since Worksheet_Calculate() fires after the ComputeScore() recalc. This additional round of calculation of the chart data causes the chart to update now, rather than later or not at all. The setting of EnableEvents and xlManual keeps infinite recalc loops and other event problems from occurring.
This might look extremely basic but I just tried Manual Calculating on the spreadsheet where the charts were (by pressing F9) and it worked! Tha VBA code for it is simply:
Calculate
;)
As i tried pretty much ALL the presented solutions and since none worked in my case, I'll add my two cents here as well. Hopefully it helps someone else.
The consensus on this issue seems to be that we need to somehow force excel to redraw the graph since it is not doing it when it should.
My solution was to kill the X-Axis data and replace it with nothing, before changing it to what i wanted. Here my code:
With wsReport
.Activate
.ChartObjects(1).Activate
ActiveChart.FullSeriesCollection(1).XValues = "=" 'Kill data here
.Range("A1").Select 'Forwhatever reason a Select statement was needed
.ChartObjects(1).Activate
ActiveChart.FullSeriesCollection(1).XValues = "=tblRef[Secs]"
End With
End Sub
My two cents for this problem--I was having a similar issue with a chart on an Access 2010 report. I was dynamically building a querydef, setting that as the rowsource on my report and then trying to loop through each series and set the properties of each series. What I eventually had to do was to break out the querydef creation and the property setting into separate subs. Additionally, I put a
SendKeys ("{DOWN}")
SendKeys ("{UP}")
at the bottom of each of the two subs.
On changing the values of the source data, chart was not getting updated accordingly. Just closed all instances of excel and restarted, problem disappeared.
I had a similar problem - Charts didn't appear to update. I tried just about everything on this thread with no luck. I finally realized that the charts that I was copying and pasting were linked to the source data, and that is why they were all showing the same results.
Be sure you are copying and pasting pictures before you go through all the other motions....
I just had the same problem, and also found that the line would only display if I put in bad data (characters instead of numbers). This caused the line to appear, but changing back to valid data caused it to disappear again.
What I found is that if I double-clicked the line (appearing with bad data), it showed me that it was on the SECONDARY axis for some reason. Changing that to PRIMARY axis solved my problem.
I was having a similar problem today with a 2010 file with a large number of formulas and several database connections. The chart axis that were not updating references ranges with hidden columns, similar to others in this chain, and the labels displayed the month and year "MMM-YY" of the dynamic data. I tried all solutions listed except for the VBA options as I'd prefer to solve without code.
I was able to solve the issues by encapsulating my dates (the axis labels) in a TEXT formula as such: =TEXT(A10,"MMM-YY"). And everything immediately updates when values change. Happy days again!!!
From reading the other contributors issues above I started to think that the Charts were having problems with the DATE data type specifically, and therefore converting the values to text with the TEXT function resolved my issue. Hopefully this may help you as well. Just change the format within the double quotes (second argument of the TEXT function) to suit your needs.
Just activate the sheet where the chart is:
Sheets(1).Activate
and your problem disappears.
I had the same problem and none of the things you mentioned in question worked for me until I just activated sheet. The accepted answer didn't work for me neither.
Alternatively you can make:
ActiveCell.Activate
For me the macro didn't update the x-axis for all series, but only the first one. The solution I found was to update the x-axis for all series and then it refrehsed (also I had code to change the format of the x-axis, but I don't think that that was the problem).
ActiveSheet.ChartObjects("Diagram 7").Activate
ActiveChart.Axes(xlCategory).Select
ActiveChart.SeriesCollection(1).XValues = "={""""}"
ActiveChart.SeriesCollection(1).XValues = "=YYY!$BQ$85:$BQ$8844"
ActiveChart.SeriesCollection(2).XValues = "=YYY!$BQ$85:$BQ$8844"
ActiveChart.SeriesCollection(3).XValues = "=YYY!$BQ$85:$BQ$8844"
ActiveChart.SeriesCollection(4).XValues = "=YYY!$BQ$85:$BQ$8844"
ActiveChart.SeriesCollection(5).XValues = "=YYY!$BQ$85:$BQ$8844"
ActiveChart.SeriesCollection(6).XValues = "=YYY!$BQ$85:$BQ$8844"
ActiveChart.SeriesCollection(7).XValues = "=YYY!$BQ$85:$BQ$8844"
ActiveChart.SeriesCollection(8).XValues = "=YYY!$BQ$85:$BQ$8844"
Full macro;
Sub TEST()
'
' TEST Makro
'
ActiveSheet.ChartObjects("Diagram 7").Activate
ActiveChart.Axes(xlCategory).Select
Selection.TickLabels.NumberFormat = "#"
ActiveSheet.ChartObjects("Diagram 7").Activate
ActiveChart.SeriesCollection(1).XValues = "={""""}"
ActiveChart.SeriesCollection(1).XValues = "=YYY!$BQ$85:$BQ$8844"
ActiveChart.SeriesCollection(2).XValues = "=YYY!$BQ$85:$BQ$8844"
ActiveChart.SeriesCollection(3).XValues = "=YYY!$BQ$85:$BQ$8844"
ActiveChart.SeriesCollection(4).XValues = "=YYY!$BQ$85:$BQ$8844"
ActiveChart.SeriesCollection(5).XValues = "=YYY!$BQ$85:$BQ$8844"
ActiveChart.SeriesCollection(6).XValues = "=YYY!$BQ$85:$BQ$8844"
ActiveChart.SeriesCollection(7).XValues = "=YYY!$BQ$85:$BQ$8844"
ActiveChart.SeriesCollection(8).XValues = "=YYY!$BQ$85:$BQ$8844"
ActiveChart.Axes(xlCategory).Select
ActiveChart.Axes(xlCategory).TickMarkSpacing = 730
ActiveChart.Axes(xlCategory).TickLabelSpacing = 730
End Sub