Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.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
Arrays 遍历范围中的值,在范围中搜索它们,在相应的行中查找值,然后将它们添加到数组中_Arrays_Vba_Excel - Fatal编程技术网

Arrays 遍历范围中的值,在范围中搜索它们,在相应的行中查找值,然后将它们添加到数组中

Arrays 遍历范围中的值,在范围中搜索它们,在相应的行中查找值,然后将它们添加到数组中,arrays,vba,excel,Arrays,Vba,Excel,我想查看D列中的一系列值,并取每个值: 对于每个值 在同一范围内检查其出现情况 在其出现的行中检查a列中的值 将列a中的此值添加到数组(或以其他方式保存数据) 转到D列中下一个出现的值,并将A列的下一个值保存到数组中 当我检查每个值的所有引用并将其添加到数组中时,我希望数组在单元格H1中给出(对于接下来的值,I1等等) 下面是我对一些虚拟值的理解: 到目前为止,我在VBA中的尝试如下(我第一次处理数组): 告诉我需要一个数据字段 知道如何解决我的问题吗?看看集合对象,因为它是存储唯一值的好方法

我想查看D列中的一系列值,并取每个值:

  • 对于每个值
  • 在同一范围内检查其出现情况
  • 在其出现的行中检查a列中的值
  • 将列a中的此值添加到数组(或以其他方式保存数据)
  • 转到D列中下一个出现的值,并将A列的下一个值保存到数组中
  • 当我检查每个值的所有引用并将其添加到数组中时,我希望数组在单元格H1中给出(对于接下来的值,I1等等)
  • 下面是我对一些虚拟值的理解:

    到目前为止,我在VBA中的尝试如下(我第一次处理数组):

    告诉我需要一个数据字段


    知道如何解决我的问题吗?

    看看
    集合
    对象,因为它是存储唯一值的好方法。您不需要运行多个
    Find
    函数或增量构建数组,只需读取一次列并将其写入相关集合即可

    必须从您的问题和代码中说明您希望如何编写输出,但下面的代码将为您指明正确的方向:

    Dim uniques As Collection
    Dim valueSet As Collection
    Dim valueD As String
    Dim valueA As String
    Dim v As Variant
    Dim r As Long
    Dim c As Long
    Dim output() As String
    
    'Read the data
    With ThisWorkbook.Worksheets("Tabelle1")
        v = .Range("A1", _
            .Cells(Rows.Count, "D").End(xlUp)) _
            .Value2
    End With
    
    'Populate the collections
    Set uniques = New Collection
    For r = 1 To UBound(v, 1)
    
        valueA = CStr(v(r, 1))
        valueD = CStr(v(r, 4))
    
        'Check if we have a collection for the D value
        Set valueSet = Nothing
        On Error Resume Next
        Set valueSet = uniques(valueD)
        On Error GoTo 0
    
        'If not then create a new one.
        If valueSet Is Nothing Then
            Set valueSet = New Collection
            uniques.Add valueSet, Key:=valueD
        End If
    
        'Add the A value to it
        valueSet.Add valueA
    Next
    
    'Compile the write array
    ReDim Preserve output(1 To 1, 1 To uniques.Count)
    c = 1
    For Each valueSet In uniques
        For Each v In valueSet
            '--> uncomment this 'If block', if you want
            '--> comma separated values.
    '            If Len(output(1, c)) > 0 Then
    '                output(1, c) = output(1, c) & ", "
    '            End If
            output(1, c) = output(1, c) & v
        Next
        c = c + 1
    Next
    
    'Write the output array
    ThisWorkbook.Worksheets("Tabelle1") _
        .Range("H1").Resize(, UBound(output, 2)) _
        .Value = output
    

    您已将临时定义为双精度。Dim temparray As Double A Double用于存储大的浮点值。将
    Dim temparray As Double
    更改为
    Dim temparray()As Double
    (是否确实要将Double作为数据类型?)。将
    set temparray(b,1)
    更改为
    temparray(b,1)
    并在该语句之前添加
    ReDim Preserve temparray(b-1,1)
    。。。另外,请进一步了解如何使用阵列…:)
    set temparray(b,1)
    
    Dim uniques As Collection
    Dim valueSet As Collection
    Dim valueD As String
    Dim valueA As String
    Dim v As Variant
    Dim r As Long
    Dim c As Long
    Dim output() As String
    
    'Read the data
    With ThisWorkbook.Worksheets("Tabelle1")
        v = .Range("A1", _
            .Cells(Rows.Count, "D").End(xlUp)) _
            .Value2
    End With
    
    'Populate the collections
    Set uniques = New Collection
    For r = 1 To UBound(v, 1)
    
        valueA = CStr(v(r, 1))
        valueD = CStr(v(r, 4))
    
        'Check if we have a collection for the D value
        Set valueSet = Nothing
        On Error Resume Next
        Set valueSet = uniques(valueD)
        On Error GoTo 0
    
        'If not then create a new one.
        If valueSet Is Nothing Then
            Set valueSet = New Collection
            uniques.Add valueSet, Key:=valueD
        End If
    
        'Add the A value to it
        valueSet.Add valueA
    Next
    
    'Compile the write array
    ReDim Preserve output(1 To 1, 1 To uniques.Count)
    c = 1
    For Each valueSet In uniques
        For Each v In valueSet
            '--> uncomment this 'If block', if you want
            '--> comma separated values.
    '            If Len(output(1, c)) > 0 Then
    '                output(1, c) = output(1, c) & ", "
    '            End If
            output(1, c) = output(1, c) & v
        Next
        c = c + 1
    Next
    
    'Write the output array
    ThisWorkbook.Worksheets("Tabelle1") _
        .Range("H1").Resize(, UBound(output, 2)) _
        .Value = output