2015-02-11 28 views
0

我正在Excel VBA中編寫一個子文件,並且在嘗試分配.Find()的結果時一直給我一個'類型不匹配'錯誤消息。到Range變量。我感到非常自信,我的類型是合適的,所以也許在某處有語法錯誤?'類型不匹配'錯誤與VBA中的.Find()方法

幫助將不勝感激: (線由星號前面就是引發錯誤)

Sub totalTiger(fCode As String, project As String, ttls() As Double) 

    'Set shorcuts to Worksheets 
    Dim this As Workbook: Set this = ThisWorkbook 
    Dim proj As Worksheet: Set proj = this.Worksheets(project) 

    'Dim req variables 
    Dim tRng As Range: Set tRng = proj.Range("A:A").Find(What:="Program Description") 'Establish where Staff data ends and Tiger data begins 
    ***Dim rng As Range: Set rng = proj.Range("C:C").Find(What:=fCode, After:=tRng) 'First fCode entry*** 

    'For each fCode entry BEFORE the Tiger data, sum the assignments by month 
    Do While Not rng Is Nothing And rng.row > tRng.row 

     'For each month 
     For col = 4 To 15 

      'Add this month's assignment to our running total 
      ttls(col - 4) = ttls(col - 4) + CDbl(proj.Cells(rng.row, col).Value) 

     Next 

     'Update the rng reference 
     Set rng = proj.Range("C:C").Find(What:=fCode, After:=rng) 

    Loop 

End Sub 

回答

0

我認爲這個問題是在「後:=真隨機數發生器」:它可能是出了範圍的「查找」

Dim rng As Range: Set rng = proj.Range("C:C").Find(What:=fCode, After:=tRng) 

嘗試刪除「之後:=真隨機數發生器」,如果它後工作刪除,然後嘗試插入一個正確的範圍。

Dim rng As Range: Set rng = proj.Range("C:C").Find(What:=fCode) 

我不知道這是你需要什麼,但你可以嘗試:

Dim rng: Set rng = proj.Range("C:C").Find(What:=fCode, After:=proj.Range("C" & tRng.Row)) 

它通過在那裏發現「程序說明」

+0

非常感謝你@genespos!我認爲問題在於tRng在列A中,我正在C列中尋找fCode。我最終在找到範圍列C後將tRng更新爲行tRng.row – AllyB 2015-02-11 18:38:15

0

這可能是真隨機數發生器設置爲無,所以這個問題是不是與find的返回值,而是用你給它的參數。

+0

行找到的第一個「FCODE」出發感謝El Scripto,我已確認tRng已設置,並且我也知道在所有搜索的電子表格中都必須顯示「程序描述」。 – AllyB 2015-02-11 18:12:51