VBA 将存储在字典中的单元格粘贴到另一个工作表中的单元格

use*_*788 3 excel vba dictionary

我试图从一个工作表中搜索一列单元格,找到所有唯一值,然后将这些值粘贴到另一个工作表中的列中。到目前为止,我的代码可以创建字典,搜索所需的列,并选择该列中的所有唯一值。

Function UniqueRequest() As Long

        myReqIDCol = ColSearch("id")

        'Creates a dictionary filled with each unique value in the "TaskIDList" column and counts them to determine how many unique keys are in the document
        Set dic = CreateObject("Scripting.Dictionary")
        For i = 1 To LastRow
            tmp = Cells(i, myReqIDCol).Value
            If Not dic.exists(tmp) Then
                dic.Add tmp, 1
            End If
        Next i

End Function
Run Code Online (Sandbox Code Playgroud)

我还有一个功能,可以选择要粘贴单元格的工作表并进行设置,以便将值粘贴到所需列中的每个连续空白单元格中。

Function ReqSheet(input_column As Integer, input_value As Long) As Long

        Dim rv As Long

            rv = 1

            Sheets("Request Results").Activate
            Do While Cells(rv, input_column).Value <> ""
                rv = rv + 1
            Loop
            Cells(rv, input_column).Value = input_value

    ReqSheet = input_value

    End Function
Run Code Online (Sandbox Code Playgroud)

我遇到的问题是我不完全确定如何将这两者联系起来。我想用字典的每个值调用 ReqSheet 函数,但我尝试的一切都失败了。抱歉,如果这是一个简单的解决方案,但我无法从互联网上找到一个好的解决方案,而且我对 VBA 还很陌生。

Dic*_*ika 5

字典的好处之一是,您可以将它们的值和键提取到一个数组中,然后将其一次性全部写入一个范围,而无需循环。

Sub GetUnique()

    Dim dc As Scripting.Dictionary
    Dim rCell As Range

    Set dc = New Scripting.Dictionary

    For Each rCell In Selection.Cells
        If Not dc.Exists(rCell.Value) Then
            dc.Add rCell.Value, rCell.Value
        End If
    Next rCell

    ThisWorkbook.Worksheets("Request Results").Range("A1").Resize(UBound(dc.Keys), 1).Value = _
        Application.Transpose(dc.Keys)

End Sub
Run Code Online (Sandbox Code Playgroud)