2010-01-03 104 views
0

基本上我有3个工作表。从第一张工作表中,我得到一个值(rotid),并通过使用匹配函数,我尝试在第二张工作表中找到该值,并将与该值关联的行复制到工作表3中,其中y是工作表中行的索引2对应于值(rotid)。但有时在工作表2中找不到值,程序崩溃。我该如何处理这个错误?如何处理vba excel中的错误1004?

工作表1是列表,
表2是rotmain2,
表3是IMDB

顺便说一句,这是我的代码看起来像。我不知道如何使用代码标签。

Sub combine_imdb_rot_data() 

' y is the index of the row in rotmain2 which corresponds to rotid 

Dim y As Variant 

Sheets("imdbmain").Select 

For i = 1 To 4415 

    ' select sheet list and assign rotid for value of the cell in row i+1 and column 7 
    rotid = Sheets("list").Cells(i + 1, 7).Value 

    ' if rotid is not empty, 
    If Len(rotid) > 0 Then 

    'look for a row that corresponds to the rotid in worksheet "rotmain2" 
    Sheets("rotmain2").Select 
    'x = Sheets("list").Cells(i + 1, 7).Row 
    y = WorksheetFunction.Match(rotid, Range("B1:B4218"), 0) 

    If IsNumeric(y) Then        
     ' copy the information in the row    
     Range(Cells(y, 1), Cells(y, 13)).Select 
     Selection.Copy     
     ' paste it into row i+1 in worksheet "imdbmain" 
     Sheets("imdbmain").Select 
     'select row i+1 in imdbmain 
     Range(Cells(i + 1, 9), Cells(i + 1, 21)).Select 
     Workbooks(1).Sheets(1).Paste 
     Application.CutCopyMode = False 
    Else 
     ' copy the information in the row     
     Range(A4220, M4220).Select 
     Selection.Copy 

     ' paste it into row i+1 in worksheet "imdbmain" 
     Sheets("imdbmain").Select 
     'select row i+1 in imdbmain 
     Range(Cells(i + 1, 9), Cells(i + 1, 21)).Select 
     Workbooks(1).Sheets(1).Paste 
     Application.CutCopyMode = False 
    End If 
End If 
Next 
End Sub 

我也试着用Remou建议的另一种方法。 这是.find方法的工作原理吗?林不知道,但是当我使用它,我得到一个运行时错误13类型不匹配:

Sub combine_imdb_rot_data() 

    ' y is the index of the row in rotmain2 which corresponds to rotid 

    Dim y As Variant 

    Sheets("imdbmain").Select 

    For i = 1 To 4415 

    ' select sheet list and assign rotid for value of the cell in row i+1 and column 7 
    rotid = Sheets("list").Cells(i + 1, 7).Value 

    ' if rotid is not empty, 
    If Len(rotid) > 0 Then 

     'look for a row that corresponds to the rotid in worksheet "rotmain2" 
     Sheets("rotmain2").Select 
     'x = Sheets("list").Cells(i + 1, 7).Row 
     Set y = Range("B1:B4218").Find(rotid) 


     If y Is Nothing Then 

      ' copy the information in the row 
      'Range("1:4," & x & ":" & x).Copy 
      'Range("A&x"&:&"M&x").Copy 
      'Copy empty row 

      Range("A101:M101").Select 
      Selection.Copy 


      ' paste it into row i+1 in worksheet "imdbmain" 
      Sheets("imdbmain").Select 
      'select row i+1 in imdbmain 
      Range(Cells(i + 1, 9), Cells(i + 1, 21)).Select 
      Workbooks(1).Sheets(1).Paste 


     Else 

      ' copy the information in the row 
      'Range("1:4," & x & ":" & x).Copy 
      'Range("A&x"&:&"M&x").Copy 

      Range(Cells(y, 1), Cells(y, 13)).Select 
      Selection.Copy 


      ' paste it into row i+1 in worksheet "imdbmain" 
      Sheets("imdbmain").Select 
      'select row i+1 in imdbmain 
      Range(Cells(i + 1, 9), Cells(i + 1, 21)).Select 
      Workbooks(1).Sheets(1).Paste 
      Application.CutCopyMode = False 




     End If 

    End If 
    Next 


End Sub 
+0

见http://stackoverflow.com/editing-help关于如何格式化代码的细节。 – 2010-01-03 18:30:13

回答

0

你可能想要做的第一件事就是把On Error Resume Next在您的模块的顶部。先试一试。

+0

请告诉我,这是尴尬。 – Aaronaught 2010-01-03 18:42:30

+0

我该如何实现它? 这个想法是,我想复制工作表2中的一个空行到工作表3,如果发生错误,这意味着无法使用匹配函数找到值,并且如果没有错误则定期运行程序(这意味着匹配返回一个数值) – excel34 2010-01-03 18:46:47

+0

非常严重,这就是你如何处理非MATCH(如果找不到匹配则返回“#N/A” - 错误1004)。把这个 - 在接下来的错误恢复 - 在你的代码中,右下方的子行。不要误解我的意思,这可能不是excel34所要求的最优化方式,但它很快处理错误。如果代码在此之后仍然无法工作,那么还有其他问题需要考虑。 – 2010-01-03 22:04:10

1

你可以捕获错误,或者使用查找:

rotid=5 ''Let us say 

With Worksheets(1).Range("B1:B4218") 
    ''Find returns a range object, so we use Set 
    Set y = .Find(rotid, LookIn:=xlValues, lookAt:=xlWhole) 
    If y Is Nothing Then 
     Debug.Print "Not found" 
    Else 
     ''This will print a cell, $b$15, for example 
     Debug.Print y.Address 
     ''This will print 5 
     Debug.Print y.Value 
    End If 
End With 

Futher信息:http://msdn.microsoft.com/en-us/library/aa195730%28office.11%29.aspx

+0

嗨Remou,我仍然不熟悉VBA。你可以向我解释什么是Set y = Range(“B1:B4218”)。Find(rotid)是什么意思?我记得有一个excel函数FIND(find_text,within_text,start_num),但它只能找到文本中的字符串而不是单元格中的位置。它与该功能有关吗? – excel34 2010-01-03 20:29:15

+0

我已经添加了一些注释和MSDN链接。我希望这更清楚。 – Fionnuala 2010-01-03 20:58:09