2015-08-27 128 views
1

在以下兩種方式:功能沒有返回龍

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

我有輸出的所有變種即將進行功能證明有效數據正在進入,但仍然0

我也可以打印iCount和tmp從withi通過使用消息框功能,但可以返回到我的主代碼的價值?

任何想法?

我在此代碼做不同的唯一的代碼我通常使用的是另外LBOUND和UBOUND的,但我已經檢查了許多實例,並且我相信我有部分權

+0

同時用'Range(「a1」)替換'Range(「a1:d」&lr).Value'。Resize(lr,4).Value' – ja72

回答

2

您指定變量fing_rowtmp

您不應該指定find_row = tmp嗎?

編輯: 一個好辦法避免錯字與變量,是總是在你的代碼的頂部使用Option Explicit。如果你已經使用過它,它不會允許你的代碼編譯(因爲fing_row不會被聲明)。

+0

And the typo:P.接得好。 – BruceWayne

+0

哦,親愛的主!我怎麼錯過了!非常感謝!另一個爲閱讀障礙編碼者協會! – user3661451

+0

@ user3661451,因爲您沒有啓用Option Explicit。你真的應該這樣做(在VBA IDE中去工具=>選項並勾選需要聲明框 – sous2817