VBA类模块排序对象访问数组

时间:2014-05-16 14:58:17

标签: arrays class vba sorting

在VBA中需要一些自定义类的帮助......


我有两节课 第一个 - 转移

'.... class k_s
'.... 
Private pNav As Double
''********************************************** NAV
Public Property Get nav_val() As Double
nav_val = pNav
End Property
Public Property Set nav_val() As Double
nav_val = pNav
End Property

下一课是一个计划 - 将包含上述类的数组:

'...class cPlan
'************* ATTR
Private plan() As k_s
'********* Add - this method is called from the master form to populate the array
Public Sub add_s(pol As k_s)
ReDim Preserve plan(UBond(plan) + 1)
Set plan(UBond(plan)) = pol
End Sub

' ------排序方法 Public Sub seradit_polozky()

QSort计划(),0,UBound(极点) ' serazeny =真 结束子


我想调整smink的排序子,这样我就可以在cPlan方法中随时对它进行排序(移位)。

' ********** VBA array sort function?

Private Sub QuickSort(vArray As Variant, inLow As Long, inHi As Long)

  Dim pivot   As Variant
  Dim tmpSwap As Variant
  Dim tmpLow  As Long
  Dim tmpHi   As Long

  tmpLow = inLow
  tmpHi = inHi

  pivot = vArray((inLow + inHi) \ 2)

  While (tmpLow <= tmpHi)

 While (vArray(tmpLow) < pivot And tmpLow < inHi)
    tmpLow = tmpLow + 1
 Wend

 While (pivot < vArray(tmpHi) And tmpHi > inLow)
    tmpHi = tmpHi - 1
 Wend

 If (tmpLow <= tmpHi) Then
    tmpSwap = vArray(tmpLow)
    vArray(tmpLow) = vArray(tmpHi)
    vArray(tmpHi) = tmpSwap
    tmpLow = tmpLow + 1
    tmpHi = tmpHi - 1
 End If

  Wend

  If (inLow < tmpHi) Then QuickSort vArray, inLow, tmpHi
  If (tmpLow < inHi) Then QuickSort vArray, tmpLow, inHi

End Sub

这个Sub,所以它可以根据里面对象的nav_val属性对(class cPlan)数组中的对象(k_s)进行排序。

我尝试修复函数的定义,方法是将.nav_val添加到vArray(xxx).nav_val,pivot.nva_val但是我错误&#34; 91对象变量或未设置的块变量&#34;

你试过解决类似的问题吗?

1 个答案:

答案 0 :(得分:0)

我知道这个问题真的很老了,但无论如何我会回答它。 您的问题是,在使用类字段时,您不会使用set。

无论如何,这里是一个完整的动态快速排序版本,其名称为参数:

 Public Sub QuickSort(vArray As Variant, inLow As Long, inHi As Long, field As String)

Dim pivot   As Variant
Dim tmpSwap As Person
Dim tmpLow  As Long
Dim tmpHi   As Long

tmpLow = inLow
tmpHi = inHi

pivot = CallByName(vArray((inLow + inHi) \ 2), field, VbGet)

While (tmpLow <= tmpHi)

 While (CallByName(vArray(tmpLow), field, VbGet) < pivot And tmpLow < inHi)
    tmpLow = tmpLow + 1
 Wend

 While (pivot < CallByName(vArray(tmpHi), field, VbGet) And tmpHi > inLow)
    tmpHi = tmpHi - 1
 Wend

 If (tmpLow <= tmpHi) Then
    Set tmpSwap = vArray(tmpLow)
    Set vArray(tmpLow) = vArray(tmpHi)
    Set vArray(tmpHi) = tmpSwap
    tmpLow = tmpLow + 1
    tmpHi = tmpHi - 1
 End If

Wend

If (inLow < tmpHi) Then QuickSort vArray, inLow, tmpHi, field
If (tmpLow < inHi) Then QuickSort vArray, tmpLow, inHi, field

End Sub