【问题标题】:VBA class module sort array of objects accessVBA类模块排序对象数组访问
【发布时间】:2014-05-16 14:58:17
【问题描述】:

O 在 VBA 中的自定义类方面需要一点帮助...


我确实有两门课 第一个 - shift

'.... 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

'-----排序方式 公共子 seradit_polozky()

QSort plan(), 0, UBound(pole) '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 属性对(cPlan 类)数组中的我的对象(k_s)进行排序。

我尝试通过将 .nav_val 添加到 vArray(xxx).nav_val、pivot.nva_val 来修复函数的定义,但我出错“91 对象变量或未设置块变量”。

您是否尝试过解决类似的问题?

【问题讨论】:

  • 哪一行会报错? (请确保将您的选项设置为“Break in class module”)另外,请参阅FAQthis one。我想帮助你,因为我认为这可能是一个非常有趣的问题,但我真的不明白你在问什么。
  • 你好。是的,对,我会在睡一会后重新定义问题。

标签: arrays class vba sorting


【解决方案1】:

我知道这个问题很老了,但我还是要回答。 您的问题是您在使用类字段时没有使用 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-01-07
    • 2022-01-28
    • 2020-02-20
    • 1970-01-01
    • 1970-01-01
    • 2018-05-21
    • 2018-12-28
    • 1970-01-01
    相关资源
    最近更新 更多