我有一个简单的2列数组,我要做的就是使用Vlookup在第1列中找到匹配的值,并从第2列中返回该值。我一直在获取#N / A,这意味着我的“答案”不在那里。我以为自己做的一切都正确,但是尽管阅读了很多#N / A问题,但似乎找不到答案。这是我的代码:-
Dim y as Variant
Dim Misc_Pay(1 to 16,1 to 2 ) as Variant.
' Populate the Misc_Pay array
Misc_Pay(1,2) = "Cancer,3"
Misc_Pay(2,2) = "Clerical,Last of Month"
Misc_Pay(3,2) = "Halifax,14"
Misc_Pay(4,2) = "Reward,1"
'and so on down to
Misc_Pay(16,2) = "Last Line,End of File"
'My Vlook up code is:-
y = (Application.Vlookup("Reward",Misc_Pay,2,False)
然后我一直在打印“ y”,所以我可以看到它正在拾取什么值,除了它似乎没有拾取任何东西外,例如显然,“奖励”在那里。
如果我可以使该行正常工作,则完整行代码将为:-
If(iserror(Application.Vlookup("Reward,Misc_Bay,2,False) then 'do something' Else 'do something else'
答案 0 :(得分:0)
首先,由于第2行末尾的句点以及最后一行的多余(
,因此您发布的代码将无法编译。
如果要在2D数组中进行查找,则需要同时设置key和lookup列的值(在这种情况下,分别为列1和2)。当前,您只是将整个字符串用逗号分配给第二列,因此在第一列中无需查找任何内容。
我想你想要的是
Dim y As Variant
Dim Misc_Pay(1 To 16, 1 To 2) As Variant
Misc_Pay(1, 1) = "Cancer"
Misc_Pay(1, 2) = "3"
Misc_Pay(2, 1) = "Clerical"
Misc_Pay(2, 2) = "Last of Month"
Misc_Pay(3, 1) = "Halifax"
Misc_Pay(3, 2) = "14"
Misc_Pay(4, 1) = "Reward"
Misc_Pay(4, 2) = "1"
Misc_Pay(16, 1) = "Last Line"
Misc_Pay(16, 2) = "End of File"
y = Application.VLookup("Reward", Misc_Pay, 2, False)