excel - VBA查找和替换不适用于所有列

标签 excel vba loops

我正在使用一段循环通过 Excel 工作表的代码,使用其中的键将另一组数据复制到其中。两个数据集(数据集 A 到数据集 B)如下所示:

数据集 A:

Key  Val1  Val2  Val3
123  yes   up    right
324  no    down  right
314  no    up    left

数据集 B:
Key  Val1  Val2  Val3
123
314
324

当脚本运行时,它会根据 Key 复制数据。我的代码适用于 Val1 和 Val2,但只会导致 Val3 出现空白条目,这是意外且不需要的。我的代码如下:
    Sub copyData()
    Dim i As Long, arr As Variant, dict As Object

    Set dict = CreateObject("scripting.dictionary")
    dict.comparemode = vbTextCompare

    With Worksheets("COMBINED")
        'put combined!a:d into a variant array
        arr = .Range(.Cells(2, "A"), .Cells(.Rows.Count, "D").End(xlUp)).Value2
        'loop through array and build dictionary keys from combined!a:a, dictionary item from rows b:d
        For i = LBound(arr, 1) To UBound(arr, 1)
            dict.Item(arr(i, 1)) = arr(i, 2)
            dict.Item(arr(i, 2)) = arr(i, 3)
            dict.Item(arr(1, 3)) = arr(1, 4)
        Next i
    End With

    With Worksheets("All SAMs Backlog")
        arr = .Range(.Cells(3, "C"), .Cells(.Rows.Count, "C").End(xlUp).Offset(0, 3)).Value2
        'loop through array and if c:c matches combined!a:a then put combined!b:b into d:d
        For i = LBound(arr, 1) To UBound(arr, 1)
            If dict.exists(arr(i, 1)) Then
                arr(i, 2) = dict.Item(arr(i, 1))
                arr(i, 3) = dict.Item(arr(i, 2))
                arr(i, 4) = dict.Item(arr(i, 3))
            Else
                arr(i, 2) = vbNullString
                arr(i, 3) = vbNullString
                arr(i, 4) = vbNullString
            End If
        Next i
        'put populated array back into c3 (resized by rows and columns)
        .Cells(3, "C").Resize(UBound(arr, 1), UBound(arr, 2)) = arr
    End With

    MsgBox ("done")

End Sub

任何帮助表示赞赏。

最佳答案

使用组合列A作为字典键,将多列组合成一个数组存储为字典项

Sub tranferData()
    Dim i As Long, arr As Variant, dict As Object

    Set dict = CreateObject("scripting.dictionary")
    dict.comparemode = vbTextCompare

    With Worksheets("COMBINED")
        'put combined!a:d into a variant array
        arr = .Range(.Cells(2, "A"), .Cells(.Rows.Count, "D").End(xlUp)).Value2
        'loop through array and build dictionary keys from combined!a:a, dictionary item from rows b:d
        For i = LBound(arr, 1) To UBound(arr, 1)
            'add key and multiple items as array
            If not dict.exists(arr(i, 1)) Then _
              dict.Add Key:=arr(i, 1), Item:=Array(arr(i, 2), arr(i, 3), arr(i, 4))
        Next i
    End With

    With Worksheets("All SAMs Backlog")
        arr = .Range(.Cells(3, "C"), .Cells(.Rows.Count, "C").End(xlUp).Offset(0, 3)).Value2
        'loop through array and if c:c matches combined!a:a then put combined!b:d into d:f
        For i = LBound(arr, 1) To UBound(arr, 1)
            If dict.exists(arr(i, 1)) Then
                arr(i, 2) = dict.Item(arr(i, 1))(0)
                arr(i, 3) = dict.Item(arr(i, 1))(1)
                arr(i, 4) = dict.Item(arr(i, 1))(2)
            Else
                arr(i, 2) = vbNullString
                arr(i, 3) = vbNullString
                arr(i, 4) = vbNullString
            End If
        Next i
        'put populated array back into c3 (resized by rows and columns)
        .Cells(3, "C").Resize(UBound(arr, 1), UBound(arr, 2)) = arr
    End With

    MsgBox ("done")

End Sub

关于excel - VBA查找和替换不适用于所有列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50711552/

相关文章:

c# - 如何在 Excel VSTO 加载项中为 OnUndo 调用 C# 方法?

mysql - 为什么我导入的数据在 mysql 中变得困惑?

java - apache POI 将文本列读取为数字

vba - Excel VBA : How to solve Run-time error '91' ?

c# - 为什么当我尝试在列表中查找 .IndexOf(a) 时返回 -1?

loops - 在我的 Clojure 循环中什么也没有发生

excel - 如何使用 ","以外的分隔符将分隔文件加载到 Excel 特定工作表中

json - VBA:访问 JSON

excel - 为 Shapes 制作真正的公式、条件格式和控制提示

Python - 在并行字典中查找一个值的平均值