VBA将集合中的自定义对象添加到临时变量

时间:2018-06-28 16:03:54

标签: excel vba collections bubble-sort

因此,我遵循了一些关于对集合中的项目进行排序的示例,但是由于某些原因,当我尝试存储元素时,我从临时变量中收到了来自vba的警告“对象不支持此属性或方法”,我将temp变量设置为variant,但似乎并不在乎。我的对象类型可能有问题吗?

 Sub selectRange()
    Dim lastrow As Long
    Dim lastColumn As Long
    Dim j As Integer
    Dim i As Integer
    Dim streamColl As Collection
    Dim ws As Worksheet
    Dim rowCount As Integer
    Dim columnCount As Integer
    Dim tempStream As Stream

    Set ws = ActiveWorkbook.Sheets("Sheet1")

    Range("E5").Select

    lastrow = Range("E5", ActiveCell.End(xlDown)).Count + 5
    lastColumn = Range("E5", ActiveCell.End(xlToRight)).Count

    Set streamColl = New Collection

    For i = 1 To lastColumn
            Set tempStream = New Stream
                tempStream.StreamName = Cells(3, i + 4).value
                tempStream.Temperature = Cells(5, i + 4).value
                tempStream.Pressure = Cells(6, i + 4).value
                tempStream.VapGasFlow = Cells(7, i + 4).value
                tempStream.VapMW = Cells(8, i + 4).value
                tempStream.VapZFactor = Cells(9, i + 4).value
                tempStream.VapViscosity = Cells(10, i + 4).value
                tempStream.LightLiqVolFlow = Cells(11, i + 4).value
                tempStream.LightLiqMassDensity = Cells(12, i + 4).value
                tempStream.LightLiqViscosity = Cells(13, i + 4).value
                tempStream.HeavyLiqVolFlow = Cells(14, i + 4).value
                tempStream.HeavyLiqMassDensity = Cells(15, i + 4).value
                tempStream.HeavyLiqViscosity = Cells(16, i + 4).value
                streamColl.Add tempStream
    Next
    MsgBox streamColl(1).StreamName
    Call sortStream(streamColl) 
End Sub

Sub sortStream(ByVal pStreamColl As Collection)
    Dim i As Integer
    Dim j As Integer
    Dim k As Integer
    Dim lastColumn As Integer
    Dim vTemp as variant



  lastColumn = 0
  k = 1
  Do While IsNumeric(pStreamColl(k).StreamName)
    lastColumn = lastColumn + 1
    k = k + 1
  Loop

  MsgBox lastColumn

  For i = 1 To lastColumn
    For j = i + 1 To lastColumn
        If pStreamColl(j).StreamName < pStreamColl(i).StreamName Then
            vTemp = pStreamColl(j)


            pStreamColl.Remove j
            pStreamColl.Add vTemp, vTemp, i
        End If
    Next j
Next i

For Each Stream In pStreamColl
    Debug.Print Stream.StreamName
Next Stream
End sub

错误被抛出行上

vtemp = pStreamColl(j)

使用数组会更好吗?

1 个答案:

答案 0 :(得分:0)

Stream()

您可以使用流数组

ReDim Streams(1 To lastColumn) As New Stream

For i = 1 To lastColumn
    With Streams(i)
        .StreamName = Cells(3, i + 4).Value
        .Temperature = Cells(5, i + 4).Value
        .Pressure = Cells(6, i + 4).Value
        .VapGasFlow = Cells(7, i + 4).Value
        .VapMW = Cells(8, i + 4).Value
        .VapZFactor = Cells(9, i + 4).Value
        .VapViscosity = Cells(10, i + 4).Value
        .LightLiqVolFlow = Cells(11, i + 4).Value
        .LightLiqMassDensity = Cells(12, i + 4).Value
        .LightLiqViscosity = Cells(13, i + 4).Value
        .HeavyLiqVolFlow = Cells(14, i + 4).Value
        .HeavyLiqMassDensity = Cells(15, i + 4).Value
        .HeavyLiqViscosity = Cells(16, i + 4).Value
    End With
Next

使用StrComp对字符串进行排序

 Function StrComp(String1, String2, [Compare As VbCompareMethod = vbBinaryCompare])

参考:How to use the STRCOMP Function (VBA)

Sub sortStream(ByRef Streams() As Stream)
    Dim swapped As Boolean, st As Stream
    Dim n As Long
    Do
        swapped = False
        For n = LBound(Streams) + 1 To UBound(Streams)
            If StrComp(Streams(n - 1).StreamName, Streams(n).StreamName, vbTextCompare) = 1 Then
                Set st = Streams(n - 1)
                Set Streams(n - 1) = Streams(n)
                Set Streams(n) = st
                swapped = True
            End If
        Next
    Loop Until Not swapped

End Sub

我只建议使用Stream(),因为我想编写冒泡排序。我会使用SortedList

SortedList

MSDN SortedList

  

表示键/值对的集合,这些键/值对按键排序,并且可以通过键和索引进行访问。

其他参考:VBA SortedList

您需要进行以下更改才能使用SortedList

之前

Dim streamColl As Collection
Set streamColl = New Collection  
streamColl.Add tempStream

之后

Dim streamColl As Object
Set streamColl = CreateObject("System.Collections.SortedList")
streamColl.Add Key:=tempStream.StreamName, Value:=tempStream.