|
代码如下: 我的问题是如何将[a1:c7]、[a10:c13]变成动态区域分别装入2个数组。请高手答复,谢谢!!
Sub DctFind()
'excelhome vba编程学习与实践~看见星光
Dim d As Object, arr, brr, i&
Set d = CreateObject("scripting.dictionary")
'd.CompareMode = vbTextCompare '不区分字母大小写
arr = [a1:c7] '数据源装入数组arr
brr = [a10:c13] '查询区域装入数组brr
For i = 1 To UBound(arr)
'遍历数组arr
d(arr(i, 1)) = arr(i, 3)
'将考号作为key,姓名作为item装入字典
Next
For i = 2 To UBound(brr)
'标题行不用查询,所以从第二行开始遍历查询数值brr
If d.exists(brr(i, 1)) Then
'如果字典中存在考号
brr(i, 2) = d(brr(i, 1))
'根据考号从字典中取值
Else
brr(i, 2) = ""
'如果字典中不存在相关考号,则值返回为空白
End If
Next
With [a10:c13]
.NumberFormat = "@" ''设置文本格式,避免某些文本数值变形
.Value = brr '结果数组读入单元格区域
End With
MsgBox "查询完成"
Set d = Nothing '释放字典
End Sub
|
|