I was trying to sort my legend and could not find any reliable answer. Here, Reordering Chart Data Series in Excel, you can find out how to do it manually. Also, a code was posted there, which was not working for me and it was too complicated for a task like this. Basically, we are trying to automate what is shown in the following picture.
Please propose your solutions or look-up my answer if you have the same problem.
This code gets the series names, put them into an array, sort the array and based on that defines the plotting order which will give the desired output. You can change
ActiveChart
to chart name to be more explicit. Feel free to apply any improvement.Excel treats name of the series as text. So you would encounter the problem that instead of having
1,2,3,...
you end up getting1,10,11,...,19,2,20,...
. In that case, convert the array to number. There are questions here, like convert an array to number, that would do the job. You can also simply compareCdbl
of each element with the one of the other. (i.e.If Cdbl(Arr(r2)) > Cdbl(rval) Then ...
)