2017-09-05 46 views
1

挣扎着一点点的代码,我正在做我的头 - 我试图比较两个工作表,并根据所提供的所有信息删除重复的行。理想的结构是将PasteCSV与OriginalCSV进行比较。重复的行,然后将宏检查删除了该行,如果所有的数据匹配 - 我试图用if语句来退出这个功能,但不是100%肯定,如果我做是正确的:接下来没有错误在循环中的excel vba

Sub DeleteDuplicates() 
Dim Row As Long 
Dim Vendor As Range 
Dim Software As Range 
Dim Version As Range 

Sheets("PasteCSV").Select 

Columns("A").Delete 

For Row = Range("A65536").End(xlUp).Row To 1 Step -1 

    Set Vendor = Sheets("OriginalCSV").Range("A").Find(Cells(Row, 1), LookIn:=xlValues, lookat:=xlWhole) 
If Not Vendor Is Nothing Then 
    Set Software = Sheets("OriginalCSV").Range("B").Find(Cells(Row, 1), LookIn:=xlValues, lookat:=xlWhole) 
If Not Software Is Nothing Then 
    Set Version = Sheets("OriginalCSV").Range("C").Find(Cells(Row, 1), LookIn:=xlValues, lookat:=xlWhole) 
If Not Version Is Nothing Then 
    Cells(Row, 1).EntireRow.Delete 

End If 

Next Row 
Sheets("PasteCSV").Cells.Copy 

Sheets(Sheets.Count).Select 

Range("A1").Select 


ActiveSheet.Paste 
Application.CutCopyMode = False 

End Sub 

任何帮助将不胜感激!

+1

我认为,该错误信息导致你错了。因为每个'If'都需要他自己,所以你缺少两个'End If'。 – IQV

+1

如果您缺少两个结尾。或者你应该用Else替换你的第二和第三个IF。 – Luuklag

回答

0

我想,错误信息会导致你错误。你是缺席两场End If为每If需要自己:

For Row = Range("A65536").End(xlUp).Row To 1 Step -1 

    Set Vendor = Sheets("OriginalCSV").Range("A").Find(Cells(Row, 1), LookIn:=xlValues, lookat:=xlWhole) 
    If Not Vendor Is Nothing Then 
     Set Software = Sheets("OriginalCSV").Range("B").Find(Cells(Row, 1), LookIn:=xlValues, lookat:=xlWhole) 
    End If 
    If Not Software Is Nothing Then 
     Set Version = Sheets("OriginalCSV").Range("C").Find(Cells(Row, 1), LookIn:=xlValues, lookat:=xlWhole) 
    End If 
    If Not Version Is Nothing Then 
     Cells(Row, 1).EntireRow.Delete 
    End If 

Next Row 
0

如果您Delting行,你必须去巴顿了。你可以看到如何在代码娄


这里做,这是与变化的代码:

Sub DeleteDuplicates() 
Dim Row As Long 
Dim rng As Range 
Dim rng2 As Range 
Dim rngSearch As Range 
Dim Vendor As Range 
Dim Software As Range 
Dim Version As Range 


Sheets("PasteCSV").Select 
Columns("A").Delete 
Row = Cells(Rows.Count, 1).End(xlUp).Row 

For I = Row To 1 Step -1 

    Set Vendor = Sheets("OriginalCSV").Columns(1).Find(Range("A" & I).Value, LookIn:=xlValues, lookat:=xlWhole) 
    If Not Vendor Is Nothing Then 
     If Vendor.Offset(0, 1).Value = Range("B" & I).Value And _ 
      Vendor.Offset(0, 2).Value = Range("C" & I).Value Then 
      Rows(I).EntireRow.Delete 
     End If 

    End If 
Next I 

Sheets("PasteCSV").Cells.Copy 
Sheets(Sheets.Count).Select 

Range("A1").Select 


ActiveSheet.Paste 
Application.CutCopyMode = False 

End Sub 
+0

谢谢大家 - 尝试过所有的例子,但没有骰子! Moosil - 现在运行你的代码给了我一个无效的限定符上rng.Row.EntireRow.Delete 修改我的现有代码结束如果语句仍返回“下一步不为”的问题.... *叹* 任何更多的想法? – siliconphoenix

+0

@siliconphoenix ok更新了代码。现在它应该工作。 – Moosli

+0

还没有 - 代码没有定义'我'(我纠正与Dim我长) - 但这只是导致它直接复制数据,而不是删除重复。 现在,我需要检查代码来查看列A,B和C是否匹配。如果是这样,请删除整行。我在我的研究中尝试了多个选项,但是您的代码似乎正在变得最接近 – siliconphoenix

1

为了更好地解释使用的VBA语句If ..

  1. 如果您希望避免使用End If,并且如果条件为真只能执行一行,只需将过程语句放在与If相同的行或嵌套If c onditions。

例子:

If x > y Then MsgBox z 
  • 如果你想清楚地看到你的过程中声明,或您有多个处理语句,如果条件为真,那么你就需要对每个相应的If条件使用End If
  • 例子:

    If x > y Then 
        MsgBox z 
    End If 
    
    If x > y Then 
        MsgBox x 
        MsgBox y 
        MsgBox z 
    End If 
    
    If x > y Then 
        MsgBox x 
    Else 
        MsgBox y 
    End If 
    
    If x > y Then 
        MsgBox x 
    Else If x < y Then 
        MsgBox y 
    Else 
        MsgBox z 
    End If 
    
    0

    另一种方式是你的 “如果...那么” 投入这样一行:

    If Not Version Is Nothing Then Cells(Row, 1).EntireRow.Delete