簡體   English   中英

為什么我使用Vlookup收到運行時錯誤1004?

[英]Why am I getting Run time error 1004 using Vlookup?

我在宏中瀏覽了Newest_closed工作表的每一行,並且:-在A列中獲取NCASTS字符串-使用Vlookup在Newest_opened工作表中查找字符串-如果找到,則獲取K列的內容並將其命名為“ Resp_Unit2”-粘貼Newest_closed工作表的K列中“ Resp_Unit2”的值注意:Newest_opened和Newest_closed是公共維度的字符串,並且工作表的實際名稱在另一個Sub中創建。

當到達使用Vlookup的代碼行時,出現“運行時錯誤1004:應用程序定義或對象定義的錯誤”。 為什么會出現此錯誤?

我使用“立即”窗口來查詢Last_row_1,Last_column_1和NCASTS的值(對於循環計數器x = 2)。 所有查詢均返回有效值。

我還使用“立即”窗口執行:

Worksheets(Newest_opened).Range(Cells(1, 1), Cells(Last_row_1, Last_column_1)).Select

Excel將顯示在工作表中選擇的單元格的正確范圍。

    Sub Assign_Resp_Unit_2()
    'This sub does the following:
    '
    '   - for each NCASTS in the Newest_closed worksheet:
    '       - searches in the Newest_opened worksheet for that NCASTS
    '       - if NCASTS found, gets the value from the "Responsible Unit"    column and pastes it in the same column in the Newest_closed worksheet
    '
    Dim x As Integer
    Dim NCASTS As String
    Dim Resp_Unit2 As String
    Dim Last_row_1 As Long
    Dim Last_column_1 As Long
    Dim New_opened_rng As Range
   '
   'Find range of Newest_opened worksheet
   Worksheets(Newest_opened).Activate
   Last_row_1 = Cells(Rows.Count, 1).End  (xlUp).Row                           'get number of last row in table
   Last_column_1 = Cells(1, Columns.Count).End(xlToLeft).Column              'get number of last column in table
   '
   'Set the variable "Newest_opened_range" to be the Range of the Newest_opened worksheet
   Set New_opened_rng = Worksheets(Newest_opened).Range(Cells(1, 1), Cells(Last_row_1, Last_column_1))
   '
   'search Newest_opened worksheet for Responsible Unit
   '
   'Loop through each cell in Column A of Newest_closed until a blank cell is encountered (i.e. end of entries)
   '
   Worksheets(Newest_closed).Activate
   x = 2   'initialize row counter
   Do Until IsEmpty(Cells(x, 1))
       '
       'Get NCASTS number from Column A of Newest_closed worksheet
       NCASTS = Cells(x, 1)
       '
       'Search for NCASTS number in Newest_opened worksheet and find  corresponding Responsible Unit entry;
       'Preset string variable Resp_Unit2 to "Not found" for case where NCASTS number is not in the range
       '
       Resp_Unit2 = "Not found"
       On Error Resume Next
       Resp_Unit2 = Application.WorksheetFunction.VLookup(NCASTS, New_opened_rng, 11, False)
       '
       'Paste value of Resp_Unit2 to Responsible Unit column (column L) for same NCASTS in Newest_closed worksheet
       '
       Sheets(Newest_closed).Cells(x, 12).Value = Resp_Unit2
       '
       'Move to next row of Newest_closed worksheet
       '
       x = x + 1
   '
   Loop
   '
   End Sub

而不是vlookup ,因為您要在yonder中的特定列中工作(並且要確保該范圍的第11列是它自己的PITA(我知道是主觀的)),所以您可以嘗試進行index / match以確保自己只需指定您的查找和輸出范圍。 將進行一些更改,例如確保限定所有范圍(甚至是rangecells

Dim OutputRng as Range, SearchRng as Range, NCASTS as String, Resp_Unit2 as String, Last_row_1 as Long
With Worksheets(Newest_opened)
    Set SearchRng = .Range(.Cells(1,1),.Cells(Last_row_1,1)) 'Assumes Column A is searched
    Set OutputRng = .Range(.Cells(1,11),.Cells(Last_row_1,11)) 'Assumes Column K has otuput
    Last_row_1 = .Cells(.Rows.Count, 1).End(xlUp).Row  'qualified
    Do Until IsEmpty(.Cells(x, 1)) 'qualified this, but may want to try a for or for each loop?
        NCASTS = .Cells(x, 1).Value 'qualify it, don't use .activate and added .value
        Resp_Unit2 = Application.Index(OutputRng, Application.Match(NCASTS, SearchRng, 0))
        x = x+1
    Loop
End With

與您的錯誤相關,您可能沒有完全匹配,這將引發錯誤; 使用WorkSheetFunction您將得到一個標記錯誤,類似於工作表的#VALUE FoxFire在帖子的評論部分中談到了這一點,這一點非常重要。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM