接下来在excel vba中没有出现错误

挣扎着一点点的代码,我正在努力 – 我试图比较两个工作表,并根据所有提供的信息删除重复的行。 将PasteCSV与OriginalCSV进行比较的理想结构。 macros检查重复的行,然后删除行,如果所有的数据匹配 – 我试图把这拉断if语句,但不是100%确定如果我这样做是正确的:

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 

任何帮助将非常感激!

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

  1. 如果要避免使用End If并且只有条件为真时才执行一行,则只需将过程语句与If或嵌套的If条件放在同一行即可。

例:

 If x > y Then MsgBox z 
  1. 如果你想清楚地看到你的过程语句,或者你有多个处理语句,如果条件为真,那么你需要使用End If为每个对应的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 

我想,错误信息会导致你错误。 你失踪两个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 

另一种方法是把你的“如果…然后”这样一行:

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

如果你是删除行,你必须去button。 你可以在Code Bellow中看到如何做到这一点


这是带有变化的代码:

 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