在两列

时间:2017-08-08 17:11:34

标签: vba function loops vlookup

我目前停留在我正在使用的功能上: 我希望创建一个函数,在concantened Strings上执行VLOOKUP,然后返回一个数字。

不用多说,这是我的意图:

Function CusVlookup(lookupval As String, columna1 As Range, columna2 As Range, indexcol As Long)

Dim x, y As Range
Dim result As Long

For Each x In columna1
    For Each y In columna2
         If x.Value & y.Value = lookupval Then
         result = Cell(x.Row, indexcol).Value
         End If
    Next y
Next x
CusVlookup = result
End Function

所以我的问题是:我哪里错了?它是否与值串联并与字符串进行比较?

第二个问题是:我如何使用MsgBox& amp;来处理/测试开发中的函数?循环?找不到办法做试验..

(说实话,它已经是第二种选择了,因为我一直试图将INDEX-MATCH与Evaluate函数组合,但是在失败24小时后(即使我找到了很简单的线程)我认为这可能是更容易,符合我的需求,实际上很容易重复使用。)

感谢能帮助我的人!

1 个答案:

答案 0 :(得分:0)

我忘了回答,抱歉。我得到了它的工作:

Function double_vlookup(table As Range, p1 As Range, c1 As Integer, p2 As 
Range, c2 As Integer, indice As Integer)
Dim i, j As Integer
For i = 1 To table.Rows.Count
If table.Cells(i, c1) = p1 And table.Cells(i, c2) = p2 Then double_vlookup = table.Cells(i, indice)
Next i
End Function

感谢那些帮助我的人!