2017-09-05 48 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