【问题标题】:VBA Adding Custom Object in Collection to Temp VariableVBA将集合中的自定义对象添加到临时变量
【发布时间】:2018-06-28 16:03:53
【问题描述】:

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

 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)

使用数组会更好吗?

【问题讨论】:

  • 使用Set vTemp = ...来存储对象。
  • 现在 pstreamcoll.add vtemp vtemp I 上抛出错误当我删除 j 时,是否会破坏对 vtemp 的引用?
  • 集合的“键”必须是字符串,不能将对象作为键传递。也许使用 vTemp.StreamName?
  • 如果键是字符串的数字,那么我该如何排序呢?
  • 我不相信您可以根据集合的键对集合进行排序。无法访问密钥。您需要根据集合项进行排序。

标签: excel vba collections bubble-sort


【解决方案1】:

流()

你可以使用一个流数组

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

排序列表

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.

【讨论】:

  • 是的,我决定使用数组,但我必须尝试排序列表。我没有太多数据,所以我认为气泡现在可以工作。谢谢!
  • 另一种选择是使用CreateObject("Scripting.Dictionary") 并按其键对其进行排序。字典比 SortedLists 更容易使用。祝你好运!
猜你喜欢
  • 1970-01-01
  • 2015-10-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-23
  • 1970-01-01
  • 2014-12-09
相关资源
最近更新 更多