Sort Charts by Name
A recent post to the Microsoft Excel Charting Discussion Group asks "how can I sort charts on a worksheet by name?" The code below loops through all of the charts in the active sheet and loads their names into an array called "arrChartNames". The array is then sorted via the Array_Sort function. The sorted results are loaded into the Buffer variable and the charts are finally placed in alphabetical order via the For-Next loop.
Sub SortChartNames()
Dim arrChartNames()
Dim Cht As ChartObject
Dim Buffer As Variant
Dim Rng As Range
X = 0
For Each Cht In ActiveSheet.ChartObjects
ReDim Preserve arrChartNames(X)
arrChartNames(X) = Cht.Name
X = X + 1
Next Cht
Buffer = Array_Sort(arrChartNames)
Z = 2
For Each X In Buffer
ActiveSheet.Shapes(X).Top = Z
ActiveSheet.Shapes(X).Left = 10
Z = Z + 90
Next X
End Sub
Private Function Array_Sort(ByVal arry As Variant) As Variant
Dim i As Long
Dim j As Long
Dim k As Variant
For i = LBound(arry) To UBound(arry)
For j = i + 1 To UBound(arry)
If arry(i) > arry(j) Then
k = arry(j)
arry(j) = arry(i)
arry(i) = k
End If
Next
Next
Array_Sort = arry
End Function