2014-05-16 45 views
0

o需要用VBA自定义类的帮助不大...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 

'------排序方法 公用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 

此子,所以可以我的(类cPlan)的基于对象的属性nav_val阵列内的对象(K_S)排序内。

我试着通过向vArray(xxx).nav_val,pivot.nva_val添加.nav_val来修复函数的定义,但是我的错误是“91 Object variable or WIth block variable not set”。

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

+0

哪一行抛出的错误? (请确保将您的选项设置为“Break in class module”)另请参阅此[常见问题](http://stackoverflow.com/help/mcve)和[this one](http://stackoverflow.com /帮助/如何对问)。我想帮助你,因为我认为这可能是一个非常有趣的问题,但我真的不明白你在问什么。 – RubberDuck

+0

你好。是的,对,在我睡一觉后我会重新定义这个问题。 – EvanS

回答

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