接下来没有在循环中的excel vba中出错

时间:2017-09-05 06:52:42

标签: excel vba excel-vba

挣扎着一些正在努力的代码 - 我正在尝试比较两个工作表并根据显示的所有信息删除重复的行。理想的结构将PasteCSV与OriginalCSV进行比较。宏检查重复的行,如果所有数据都匹配,则删除该行 - 我试图用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

非常感谢任何帮助!

4 个答案:

答案 0 :(得分:1)

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

  1. 如果您想避免使用End If并且只有条件为true才能执行一行,只需将流程语句放在与If或嵌套{{1}相同的行上条件。
  2. 示例:

    If
    1. 如果您想清楚地看到您的流程声明,或者您有多个处理语句,如果条件为真,那么您需要对每个相应的If x > y Then MsgBox z 条件使用End If
    2. 示例:

      If

答案 1 :(得分: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

$query = "
    SELECT COUNT(*) AS value FROM `reviews`
    INNER JOIN users ON user_id = users.uid

    WHERE value = 1 AND user_id = '" . $logged_in_user_via_session . "'
";

$records = $this->db->query($query);

echo "Total Records ->".$records->num_rows();

to print all records use print_r($records->row_array());

答案 2 :(得分:0)

如果你要删除行,你必须按下按钮。你可以在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

答案 3 :(得分:0)

另一种方式是把你的" if ... then"分成这样一行:

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