Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba 加载仅具有唯一值的数组_Vba - Fatal编程技术网

Vba 加载仅具有唯一值的数组

Vba 加载仅具有唯一值的数组,vba,Vba,我在VBA中有一个循环范围: For Lrow = Firstrow To Lastrow Step 1 With .Cells(Lrow, "E") If Not IsError(.Value) Then End If End With Next Lrow 在该if语句中,我只需要加载一次每个值的数组 MB-NMB-ILA MB-NMB-ILA MB-NMB-STP MB-NMB-STP MB-NMB-

我在VBA中有一个循环范围:

For Lrow = Firstrow To Lastrow Step 1
        With .Cells(Lrow, "E")
            If Not IsError(.Value) Then

            End If
        End With
    Next Lrow
在该if语句中,我只需要加载一次每个值的数组

MB-NMB-ILA
MB-NMB-ILA
MB-NMB-STP
MB-NMB-STP
MB-NMB-WAS
MB-NMB-WAS
MB-NMB-WAS

因此,对于阵列,我只需要MB-NMB-ILA、MB-NMB-STP和MB-NMB-WAS


有人能帮我吗,我的大脑在星期一不正常工作!谢谢

假设我在单元格A1到A5中有以下内容,并且想要一个唯一值数组,即{a,b,c,d}

        A
1      "a"
2      "b"
3      "c"
4      "c"
5      "d"
以下两段代码将有助于实现这一点:

CreateUniqueArray-从每个单元格获取val,如果尚未在数组中,则添加到数组中

IsInArray-通过执行简单循环检查数组中的值

我不得不说,这是暴力的方式,并欢迎任何改进

Sub Test()
    Dim firstRow As Integer, lastRow As Integer, cnt As Integer, iCell As Integer
    Dim myArray()
    cnt = 0
    firstRow = 1
    lastRow = 10

    For iCell = firstRow To lastRow
        If Not IsInArray(myArray, Cells(iCell, 1)) Then
            ReDim Preserve myArray(cnt)
            myArray(cnt) = Cells(iCell, 1)
            cnt = cnt + 1
        End If
    Next iCell
End Sub

Function IsInArray(myArray As Variant, val As String) As Boolean
    Dim i As Integer, found As Boolean
    found = False

    If Not Len(Join(myArray)) > 0 Then
        found = False
    Else
        For i = 0 To UBound(myArray)
            If myArray(i) = val Then
               found = True
            End If
        Next i
    End If
    IsInArray = found
End Function

您可以使用过滤器来测试数组中是否存在某些内容

Dim arr As Variant: arr = Array("test1", "test2", "test3")
If UBound(Filter(arr, "blah")) > -1 Then
    Debug.Print "it is in the array"
Else
    Debug.Print "it's not in the array"
End If
您还可以使用集合并编写子集合,以便仅向集合中添加唯一的项

Dim col As New Collection
Sub addIfUnique(sAdd As String)
    Dim bAdd As Boolean: bAdd = True
    If col.Count > 0 Then
        Dim iCol As Integer
        For iCol = 1 To col.Count
            If LCase(col(iCol)) = LCase(sAdd) Then
                bAdd = False
                Exit For
            End If
        Next iCol
    End If
    If bAdd Then col.Add sAdd
End Sub
Private Sub Command1_Click()
    Dim a As Integer
    Dim b As Integer
    For a = 1 To 10
        addIfUnique "item " & a
        For b = 1 To 10
            addIfUnique "item " & b
        Next b
    Next a
    For a = 1 To col.Count
        Debug.Print col(a)
    Next a
End Sub

您可以使用字典获取唯一值,然后将它们复制回数组(或者直接使用字典,按照您喜欢的方式)。注意,这个问题已经得到了回答。Rgds