VBA查找和替换不适用于所有列 [英] VBA find and replace not working for all columns
本文介绍了VBA查找和替换不适用于所有列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用一段遍历Excel工作表的代码,使用其中的键将另一组数据复制到其中.两个数据集(数据集A到数据集B)如下所示:
I am using a piece of code that loops through a Excel worksheet, uses the keys from it to copy another set of data into it. The two datasets (Dataset A to Dataset B) looks like following:
数据集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
运行脚本后,它将根据密钥复制数据.我的代码适用于Val1和Val2,但仅导致Val3的空白条目,这是意外的和不需要的.我的代码如下:
When the script is ran, it copies the data based on the Key. My code works for Val1 and Val2, but results in only blank entries for Val3, which is unexpected and unwanted. My code is as follows:
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作为字典键,并将多列组合到一个数组中作为字典项存储
Use Combined column A as the dictionary key and combine the multiple columns into an array to be stored as the dictionary Item
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
这篇关于VBA查找和替换不适用于所有列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文