2017-07-03 95 views
1

我对VBA非常陌生,并且一直在上个礼拜自学。我承担了一项对我来说可能有点复杂的任务。Excel VBA类型与If函数不匹配

我有一个列A - AE 的文档我需要通过此文档并将信息移动到单独的工作表上,具体取决于它是什么。 我现在试图在移动信息之前使用需要匹配2个要求的IF语句。我可以获取每个单独的需求,但不能同时处理类型不匹配错误。

我不知道我做错了什么。任何帮助都感激不尽。

Sub copyrows() 

    Dim Test As Range, Cell As Object 

    Set Test = Range("G2:Z4000") 'Substitute with the range which includes your True/False values 

    For Each Cell In Test 

     If IsEmpty(Cell) Then 
      Exit Sub 
     End If 

     If Cell.Value = "Refund" And "Commission" Then 
      Cell.EntireRow.Copy 
      Sheet3.Select 'Substitute with your sheet 
      ActiveSheet.Range("A65536").End(xlUp).Select 
      Selection.Offset(1, 0).Select 
      ActiveSheet.Paste 
     End If 

    Next 

End Sub 
+0

为什么'Cell'是一个'Object'?我会把它变成一个'Range',因为这就是你使用它的原因。尽管'Object' *可能会起作用,但我相信'Range'更好。 – BruceWayne

回答

3

If Cell.Value = "Refund" And "Commission" Then

应改为阅读:

If Cell.Value = "Refund" Or Cell.Value = "Commission" Then

你必须要明确与布尔运算符如AND或OR分隔每个条件。

2

原因你的错误已经在答复@IanL

上面提到然而,你的代码是被优化为止。

,可随时更换您的5行:

Cell.EntireRow.Copy 
Sheet3.Select 'Substitute with your sheet 
ActiveSheet.Range("A65536").End(xlUp).Select 
Selection.Offset(1, 0).Select 
ActiveSheet.Paste 

以1:

Cell.EntireRow.Copy Destination:=Sheet3.Range("A65536").End(xlUp).Offset(1) 

这是不使用SelectActiveSheet,或Selection,只使用完全合格Range对象。