当这两种方式中的任何一种:
MsgBox find_row(ws_month, bs, cat, subcat, item)
or
dim row as long
row = find_row(ws_month, bs, cat, subcat, item)
MsgBox row
输出此代码
Function find_row(ws As Worksheet, bs As String, _
cat As String, subcat As String, item As String) As Long
Dim vArr As Variant
Dim iCount As Long
Dim lr As Long
Dim tmp As Long
lr = get_last_row(ws)
vArr = Range("a1:d" & lr).Value
For iCount = LBound(vArr) To UBound(vArr)
If vArr(iCount, 1) = bs Then
If vArr(iCount, 2) = cat Then
If vArr(iCount, 3) = subcat Then
If vArr(iCount, 4) = item Then
'MsgBox iCount
tmp = iCount
End If
End If
End If
End If
Next iCount
fing_row = tmp
End Function
它返回0
我输出所有var进入函数以证明有效数据进入但仍为0
我还可以使用消息框从函数中打印iCount和tmp,但是可以将值返回给我的主代码吗?
有什么想法吗?
我在这段代码中对我通常使用的代码做的唯一不同的是增加了LBound和Ubound,但是我检查了很多例子,我确信我有那个部分正确
答案 0 :(得分:2)
您将变量fing_row
分配给tmp
。
您不应该分配find_row = tmp
吗?
修改强>
避免使用变量输入拼写错误的好方法是始终在代码顶部使用Option Explicit
。如果您已经使用过它,它将不允许您的代码进行编译(因为fing_row
不会被声明)。