Re-sorting the order of real-time data?

danjuma

Active Member
Joined
Sep 21, 2008
Messages
251
Hello,

I have some columns of data of any number of rows updating every few seconds. The data is returned in ascending order based on the date/time (i.e. oldest at the top and newest at the bottom). Is there a way I could display this in descending order (i.e. the newest at the top and oldest at the bottom) automatically? Many thanks
 
Can you post your current code with some sample data?
Adding the sort routine to your current feed should not be too difficult.

Thanks for your reply. The code for the 'Historical Data' sheet is below. It is quite a lenghty code. Thanks

Code:
'=================
' local constants
'=================
' table constants
Const RANGE_HEADER = "A6:M6"
' contract description
Const COLUMN_SYMBOL = "A"
Const COLUMN_SECTYPE = "B"
Const COLUMN_EXPIRY = "C"
Const COLUMN_STRIKE = "D"
Const COLUMN_RIGHT = "E"
Const COLUMN_MULTIPLIER = "F"
Const COLUMN_EXCH = "G"
Const COLUMN_PRIMEXCH = "H"
Const COLUMN_CURRENCY = "I"
Const COLUMN_LOCALSYMBOL = "J"
Const COLUMN_COMBOLEGS = "K"
Const COLUMN_UNDERCOMP = "L"
Const COLUMN_INCLUDEEXPIRED = "M"
Const COLUMN_STATUS = "N"
' query specification
Const COLUMN_ENDDATETIME = "O"
Const COLUMN_DURATION = "P"
Const COLUMN_BARSIZE = "Q"
Const COLUMN_WHATTOSHOW = "R"
Const COLUMN_RTHONLY = "S"
Const COLUMN_DATEFORMAT = "T"
Const COLUMN_SHEETNAME = "U"
Const COLUMN_ACTIVATESHEET = "V"
'==============
' methods
'==============
' create ticker
Private Sub CreateTicker_Click()
    TickerForm.Show
End Sub
' create combo legs
Private Sub ComboLegs_Click()
   ComboLegForm.ShowForm (RANGE_HEADER)
End Sub
' cancel historical data
Private Sub CancelHistoricalData_Click()
    If Not (objTWSControl Is Nothing) Then
        If objTWSControl.m_isConnected Then
            Dim id As Integer
            id = ActiveCell.row
            Call objTWSControl.m_TWSControl.CancelHistoricalData(id + ID_HISTDATA)
            Cells(id, Columns(COLUMN_STATUS).column).value = STR_CANCELLED
            ActiveCell.Offset(1, 0).Activate
        Else
            MsgBox (STR_TWS_CONTROL_NOT_CONNECTED)
        End If
    Else
        MsgBox (STR_TWS_CONTROL_NOT_INITIALIZED)
    End If
End Sub
' request historical data
Public Sub RequestHistoricalData_Click()
    If Not (objTWSControl Is Nothing) Then
        If objTWSControl.m_isConnected Then
 
            Dim id As Long
 
            id = ActiveCell.row
            ' create contract
            Set objTWSControl.m_contractInfo = objTWSControl.m_TWSControl.createContract()
 
            ' fill contract structure
            With objTWSControl.m_contractInfo
                .symbol = UCase(Cells(id, Columns(COLUMN_SYMBOL).column).value)
                .secType = UCase(Cells(id, Columns(COLUMN_SECTYPE).column).value)
                .expiry = Cells(id, Columns(COLUMN_EXPIRY).column).value
                .Strike = Cells(id, Columns(COLUMN_STRIKE).column).value
                .Right = UCase(Cells(id, Columns(COLUMN_RIGHT).column).value)
                .multiplier = UCase(Cells(id, Columns(COLUMN_MULTIPLIER).column).value)
                .exchange = UCase(Cells(id, Columns(COLUMN_EXCH).column).value)
                .primaryExchange = UCase(Cells(id, Columns(COLUMN_PRIMEXCH).column).value)
                .currency = UCase(Cells(id, Columns(COLUMN_CURRENCY).column).value)
                .localSymbol = UCase(Cells(id, Columns(COLUMN_LOCALSYMBOL).column).value)
                .includeExpired = Cells(id, Columns(COLUMN_INCLUDEEXPIRED).column).value
            End With
 
            ' combo legs
            If Cells(id, Columns(COLUMN_SECTYPE).column).value = SECTYPE_BAG And _
            Cells(id, Columns(COLUMN_COMBOLEGS).column) <> STR_EMPTY Then
                ' create combo leg list
                objTWSControl.m_contractInfo.ComboLegs = objTWSControl.m_TWSControl.createComboLegList()
 
                ' parse combo legs string
                Call Util.ParseComboLegsIntoStruct(Cells(id, Columns(COLUMN_COMBOLEGS).column).value, objTWSControl.m_contractInfo.ComboLegs)
            End If
 
            ' under comp
            If Cells(id, Columns(COLUMN_SECTYPE).column).value = SECTYPE_BAG And _
            Cells(id, Columns(COLUMN_UNDERCOMP).column) <> STR_EMPTY Then
                ' create under comp
                objTWSControl.m_contractInfo.underComp = objTWSControl.m_TWSControl.createUnderComp()
 
                ' parse under comp info
                Call Util.ParseUnderCompIntoStruct(Cells(id, Columns(COLUMN_UNDERCOMP).column).value, objTWSControl.m_contractInfo.underComp)
            End If
 
 
            ' query specification
            Dim endDateTime As String, duration As String, barSize As String, whatToShow As String
            Dim useRTH As Long
            Dim formatDate As Long
            endDateTime = STR_EMPTY
            duration = STR_EMPTY
            barSize = STR_EMPTY
            whatToShow = STR_EMPTY
            useRTH = True
            formatDate = 1
 
            If Cells(id, Columns(COLUMN_ENDDATETIME).column).value <> STR_EMPTY Then
                endDateTime = UCase(Cells(id, Columns(COLUMN_ENDDATETIME).column).value)
            Else
                endDateTime = Format(Now, "YYYYMMDD HH:mm:SS") + " GMT"
            End If
 
            duration = UCase(Cells(id, Columns(COLUMN_DURATION).column).value)
            barSize = Cells(id, Columns(COLUMN_BARSIZE).column).value
            whatToShow = UCase(Cells(id, Columns(COLUMN_WHATTOSHOW).column).value)
            useRTH = Cells(id, Columns(COLUMN_RTHONLY).column).value
            formatDate = Cells(id, Columns(COLUMN_DATEFORMAT).column).value
 
            ' call reqHistoricalDataEx method
            Call objTWSControl.m_TWSControl.reqHistoricalDataEx(id + ID_HISTDATA, objTWSControl.m_contractInfo, endDateTime, duration, barSize, whatToShow, useRTH, formatDate)
            Cells(id, Columns(COLUMN_STATUS).column).value = STR_PROCESSING
 
            ActiveCell.Offset(1, 0).Activate
        Else
            MsgBox (STR_TWS_CONTROL_NOT_CONNECTED)
        End If
    Else
        MsgBox (STR_TWS_CONTROL_NOT_INITIALIZED)
    End If
End Sub
' update historical data table
Public Sub UpdateHistoricalData(reqId As Long, histDate As String, histOpen As Double, histHigh As Double, histLow As Double, histClose As Double, histVolume As Long, barCount As Long, WAP As Double, hasGaps As Long)
 
    reqId = reqId - ID_HISTDATA
 
    Dim sheetName As String
    sheetName = Cells(reqId, Columns(COLUMN_SHEETNAME).column).value
    Dim newSheet As Worksheet
    If sheetName <> STR_EMPTY Or Not IsNumeric(sheetName) Then
        ' add new sheet or get existed
        addNewSheet (sheetName)
        Set newSheet = ThisWorkbook.Sheets(sheetName)
        ThisWorkbook.Sheets(SHEET_NAME_HISTDATA).Activate
    End If
 
    If histOpen > 0 Then
        Dim newSheetRowId As Double
        newSheetRowId = Val((Sheets(sheetName).Cells(1, 1).value))
        newSheet.Cells(newSheetRowId, 1).value = histDate
        newSheet.Cells(newSheetRowId, 2).value = histOpen
        newSheet.Cells(newSheetRowId, 3).value = histHigh
        newSheet.Cells(newSheetRowId, 4).value = histLow
        newSheet.Cells(newSheetRowId, 5).value = histClose
        newSheet.Cells(newSheetRowId, 6).value = histVolume
        newSheet.Cells(newSheetRowId, 7).value = barCount
        newSheet.Cells(newSheetRowId, 8).value = WAP
        newSheet.Cells(newSheetRowId, 9).value = hasGaps
        newSheet.Cells(1, 1).value = newSheetRowId + 1
    Else
        ' finishing
        newSheet.Cells(1, 1).value = 0
        Cells(reqId, Columns(COLUMN_STATUS).column).value = STR_FINISHED
        If Cells(reqId, Columns(COLUMN_ACTIVATESHEET).column).value Then
            newSheet.Activate
        End If
    End If
 
End Sub
' this function finds sheet by name or adds new if sheet was not found
Private Sub addNewSheet(sheetName As String)
    Dim addNewSheet As Boolean
    addNewSheet = True
 
    Dim ws As Worksheet
    For Each ws In Worksheets
        If ws.Name = sheetName Then
            ' sheet was found
            addNewSheet = False
            If ws.Cells(1, 1).value = 0 Or ws.Cells(1, 1).value = STR_EMPTY Then
                ws.Cells.Clear
                Call AddHeaderToNewSheet(ws)
            End If
        End If
    Next
 
    If addNewSheet Then
        ' sheet was not found
        Sheets.Add Type:="Worksheet"
 
        With ActiveSheet
            .Move after:=Worksheets(Worksheets.Count)
            .Name = sheetName
        End With
 
        Call AddHeaderToNewSheet(ActiveSheet)
    End If
End Sub
Private Sub AddHeaderToNewSheet(newSheet As Worksheet)
    newSheet.Cells(1, 1).value = 3
    newSheet.Cells(2, 1).value = "Date/Time"
    newSheet.Cells(2, 2).value = "Open"
    newSheet.Cells(2, 3).value = "High"
    newSheet.Cells(2, 4).value = "Low"
    newSheet.Cells(2, 5).value = "Close"
    newSheet.Cells(2, 6).value = "Volume"
    newSheet.Cells(2, 7).value = "Count"
    newSheet.Cells(2, 8).value = "WAP"
    newSheet.Cells(2, 9).value = "HasGaps"
End Sub
Public Sub ProcessError(ByVal id As Long, ByVal errorCode As Long, ByVal errorMsg As String)
 
    Cells(id - ID_HISTDATA, Columns(COLUMN_STATUS).column).value = STR_ERROR + STR_COLON + Str(errorCode) + STR_SPACE + errorMsg
 
End Sub
 
Upvote 0

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Only had a chance for a quick look, but......
Assuming "Public Sub UpdateHistoricalData" returns your data, then just before the "end sub" on this section is where you'd put your sort.
It might be workwhile turning calculations to manual just before it and then putting them back to automatic.
 
Upvote 0
Only had a chance for a quick look, but......
Assuming "Public Sub UpdateHistoricalData" returns your data, then just before the "end sub" on this section is where you'd put your sort.
It might be workwhile turning calculations to manual just before it and then putting them back to automatic.


"It might be workwhile turning calculations to manual just before it and then putting them back to automatic"

Ummh, how do you do this? :confused:
 
Upvote 0
To turn off automatic calculations, you use:-
Application.Calculation=xlManual

To turn it back on, use:-
Application.Calculation=xlAutomatic
 
Upvote 0

Forum statistics

Threads
1,224,587
Messages
6,179,740
Members
452,940
Latest member
Lawrenceiow

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top