数组中的唯一字符串,ubound过滤器的问题

时间:2015-06-30 06:26:51

标签: arrays excel-vba vba excel

好吧我想出了一种非常复杂的方法来为我的桌子分配唯一的数字,例如RB-001,RB-002 ......因此,代码基本上将所有现有的数据以连接的形式添加到数组(TypeNumArr)中,这种形式基于我从debug.print看到的内容。然而,第二位代码在UBound(filter(TypeNumArr,cred))失败并出现错误13类型不匹配。我以前曾使用Ubound(过滤器)来搜索数组中的唯一条目,但这些数组严格来说是一维的。有谁知道这个问题是什么?我怀疑它与TypeNumArr的非一维性有关,因为它从SBArr中绘制了二维定义。

如果有人有一种不那么复杂的方法来为前缀分配唯一的数字,即" RB - " &安培; " 001"," 002"等等也会很棒:P

'Making a new entry on SB & Reimb
Set TSB = .Worksheets("SB & Reimb").ListObjects(1) 'table name
Set RBrow = TSB.ListRows.Add(1) 'the new row, always add to the top

'Assigning the next # for the RB
SBArr = TSB.DataBodyRange.Value2

For lcount = LBound(SBArr) To UBound(SBArr)
SBArr(lcount, 2) = Right("00" & SBArr(lcount, 2), 3)
TypeNumArr = SBArr(lcount, 1) & "-" & SBArr(lcount, 2)
Debug.Print TypeNumArr
Next lcount

For cred1 = 1 To 999
    cred2 = Right("00" & cred1, 3)
    cred2 = "RB-" & cred2
    Select Case UBound(Filter(TypeNumArr, cred2))
    Case 0
        RBrow.Range(1, 2) = cred1 'all that hard work just to assign a value to the RB
        Exit For
    Case Else
    End Select
Next cred1

1 个答案:

答案 0 :(得分:0)

如果我想从带有重复项的列表中获取唯一项目列表,我将列表放入字典的键中,任何重复项都将被丢弃。字典对象位于microsoft脚本运行时库中。

Dim d As Object
Set d = CreateObject("Scripting.Dictionary")
'Set d = New Scripting.Dictionary

Dim i As Long
For i = LBound(myArray) To UBound(myArray)
    d(myArray(i)) = 1
Next i

Dim v As Variant
For Each v In d.Keys()
    'd.Keys() is a Variant array of the unique values in myArray.
    'v will iterate through each of them.
Next v