简体   繁体   中英

Sort Chart In Descending Order

Wondering if it was possible to sort chart data display in descending order:

在此处输入图像描述

I've no Idea of how to do so.

The only thing I know is how to browse series values:

Set s = cht.FullSeriesCollection(1)
    For i = 1 To s.Points.Count
        If s.Values(i) < 0 Then 'JustAnExample
            'WhateverIwant
        End If
    Next i

In addition, the above chart is built with data from a worksheet:

在此处输入图像描述

Please, test the next solution. Since you did not post your chart creation code, I imagined something doing that:

Sub createStackedColChart_Arrays()
 Dim sh As Worksheet, arr1, arr2, arrN, arrD
 Dim chartName As String, arrSort, i As Long
 
 Set sh = ActiveSheet 'use here the necessary sheet
 chartName = "MyChartSorted"
 arr1 = sh.Range("A2:D2").value 'first series array
 arr2 = sh.Range("A3:D3").value 'second series array
 arrN = sh.Range("A1:D1").value 'X axes values array

 'Create the reference array of summarized values per column:
 ReDim arrSort(1 To UBound(arr1, 2))
 For i = 1 To UBound(arr1, 2)
      arrSort(i) = arr1(1, i) + CLng(arr2(1, i))
 Next i
 '_______________________________________________

 'sort arrays according to reference one (arrSort):
 sortArrs arrSort, arrN, arr1, arr2 

 'if the (testing) chart exists, delete it:
 On Error Resume Next
   ActiveSheet.ChartObjects(chartName).Delete
 On Error GoTo 0
 
 'create the necessary chart:
 With ActiveSheet.ChartObjects.Add(left:=100, width:=375, top:=75, height:=225).Chart
    .Parent.Name = chartName                  'name it to have a reference when delete it
    .SeriesCollection.NewSeries.Values = arr1 'add first series
    .SeriesCollection.NewSeries.Values = arr2 'add first series
    .HasTitle = True                          'set it to allow a Title
    .chartTitle.text = "My Sorted Chart"      'set the Title
    .ChartType = xlColumnStacked              'set the chart type
    .SeriesCollection(1).XValues = arrN       'add values to X axis
 End With
End Sub

Sub sortArrs(arrS, arrN, arr1, arr2) 'being passed byRef, the initial arrays are filtered
    Dim i As Long, nxtEl As Long, tmp, tmpN, tmp1, tmp2
    For i = LBound(arrS) To UBound(arrS) - 1 'iterate between the arrS elements (except the last):
        For nxtEl = i + 1 To UBound(arrS)    'iterate between the arrS elements (starting from the second one):
            If arrS(i) < arrS(nxtEl) Then    'sort the arrays according to the element values (< means descending)
                tmp = arrS(i): tmpN = arrN(1, i): tmp1 = arr1(1, i): tmp2 = arr2(1, i)
                arrS(i) = arrS(nxtEl): arrN(1, i) = arrN(1, nxtEl)
                     arr1(1, i) = arr1(1, nxtEl): arr2(1, i) = arr2(1, nxtEl)
                arrS(nxtEl) = tmp: arrN(1, nxtEl) = tmpN
                    arr1(1, nxtEl) = tmp1: arr2(1, nxtEl) = tmp2
            End If
        Next nxtEl
    Next i
End Sub

Please, send some feedback after testing it.

If you need the chart being dynamic, meaning to refresh it in case of any value changed in the referenced range (A1:D3, in your example), sheet Change event can be used. If a change in the above mentioned range, the event will call the above function. If need it, please copy the next code in the involved sheet code module:

Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Me.Range("A1:D3")) Is Nothing Then
        createStackedColChart_Arrays 'if need to change the Sub name, please adapt it here...
    End If
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM