下一步,excel vba in循环中无错误

下一步,excel vba in循环中无错误,vba,excel,Vba,Excel,我正在努力处理一些让我头疼的代码——我正在尝试比较两个工作表,并根据提供的所有信息删除重复的行。理想的结构是将CSV与原始的ALCSV进行比较。宏检查重复的行,然后在所有数据匹配时删除该行-我尝试使用if语句完成此操作,但无法100%确定是否正确执行此操作: Sub DeleteDuplicates() Dim Row As Long Dim Vendor As Range Dim Software As Range Dim Version As Range Sheets("PasteCSV"

我正在努力处理一些让我头疼的代码——我正在尝试比较两个工作表,并根据提供的所有信息删除重复的行。理想的结构是将CSV与原始的ALCSV进行比较。宏检查重复的行,然后在所有数据匹配时删除该行-我尝试使用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

任何帮助都将不胜感激

我想,这个错误信息会把你引向错误。您缺少两个
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

如果你正在挖掘行,你必须按上按钮。您可以在下面的代码中看到如何做到这一点


以下是更改后的代码:

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

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

  • 如果要避免使用
    End If
    ,并且如果条件为true,则只有一行要执行,只需将流程语句与
    If
    或嵌套
    If
    条件放在同一行即可
  • 例如:

    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
    
    如果x>y那么
    MsgBox z
    如果结束
    如果x>y那么
    MsgBox x
    MsgBox y
    MsgBox z
    如果结束
    如果x>y那么
    MsgBox x
    其他的
    MsgBox y
    如果结束
    如果x>y那么
    MsgBox x
    否则,如果x
    另一种方法是将“如果……那么”放在一行中,如下所示:

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

    我想,这个错误信息会让你犯错。您缺少两个
    End If
    ,因为每个
    If
    都需要自己的。您缺少两个End If。或者你应该用Else替换你的第二个和第三个IF。谢谢大家-尝试了所有的例子,但没有骰子!Moosil-现在运行代码会在rng.Row.EntireRow.Delete上给我一个无效的限定符,如果使用End If语句修改现有代码,仍然会返回“Next with For”问题。。。。还有什么想法吗?@siliconphoenix ok更新了代码。现在它应该可以工作了。仍然没有什么-代码没有定义“i”(我用Dim i作为Long进行了更正)-但这只会导致它直接复制数据,而不是删除重复的数据。目前,我需要代码来检查A、B和C列是否匹配。如果是,请删除整行。我在研究中尝试了多种选择,但您的代码似乎是最接近的