按股票涨幅排序的代码


在笔者的发贴《实时看股代码(下)》的 Form3.frm 中,模仿了股票软件的布局,但美中不足的是不能排序。为了解决这个问题,笔者做了半天的试验,终于成功!
下面的代码是按股票涨幅排序,当然你可以稍加改动,按成交量排序或者按别的什么指标排序。
窗体和控件我就不多说了,请对照 Form3.frm,我只将与排序有关的代码发出来,你自己穿插到原代码之中去。

Dim tem() As String, centage() As Single, ranking() As Integer, r As Integer, j As Integer, i As Integer


List1.Clear
share = Split(s, vbCrLf)
j = UBound(share)
ReDim tem(j), centage(j), ranking(j)
For k = 0 To j: ranking(k) = k: Next


For k = 0 To j
  stUrl = "http://hq.sinajs.cn/list=" & IIf(Left(share(k), 1) = "6", "sh", "sz") & share(k)
  xmlobject.Open "GET", stUrl, False
  xmlobject.SEnd
  If xmlobject.ReadyState = 4 Then
    s = xmlobject.responsetext
    s = Mid(s, 22, Len(s) - 27)
    arry = Split(s, ",")
    arry(0) = arry(0) & Space(8 - lstrlen(arry(0)))
    arry(1) = Right(Space(9) & Format(arry(1), "##0.00"), 9)
    arry(4) = Right(Space(8) & Format(arry(4), "##0.00"), 8)
    arry(5) = Right(Space(9) & Format(arry(5), "##0.00"), 9)
    arry(6) = Right(Space(8) & Format(arry(3) - arry(2), "##0.00"), 8)
    arry(7) = Round(arry(6) / arry(2) * 100, 2)
    centage(k) = arry(7)
    arry(7) = Right(Space(8) & Format(arry(7), "##0.00"), 8)
    arry(3) = Right(Space(8) & Format(arry(3), "##0.00"), 8)
    arry(8) = Right(Space(11) & Format(arry(8) / 100, "#######0"), 11)
    arry(11) = Right(Space(8) & Format(arry(11), "##0.00"), 8)
    arry(21) = Right(Space(8) & Format(arry(21), "##0.00"), 8)
    tem(k) = share(k) & "  " & arry(0) & arry(3) & arry(6) & arry(7) & arry(8) & arry(1) & arry(4) & arry(5) & arry(11) & arry(21)
  End If
Next


Select Case j
  Case 0    '1只股票
    List1.AddItem tem(0)
  Case 1    '2只股票
    List1.AddItem IIf(centage(0) < centage(1), tem(1), tem(0))
    List1.AddItem IIf(centage(0) < centage(1), tem(0), tem(1))
  Case Else '3只股票以上
    For k = 0 To j - 1
      For i = k + 1 To j
        If centage(ranking(k)) < centage(ranking(i)) Then r = ranking(k): ranking(k) = ranking(i): ranking(i) = r
      Next
    Next
    For k = 0 To j
      List1.AddItem tem(ranking(k))
    Next
End Select