2017-06-20 64 views
1

我想要做一個宏來比較一張紙上的值與另一張紙上的值並複製唯一的值。VBA宏來比較和增加值

說明:
我每週都會得到一堆ID(工作表A)。我想看看在過去的幾周裏,我已經使用了哪些這些ID(該列表位於Worksheet B上),並將Worksheet A中新的所有值複製到Worksheet B中。您可以將想要的結果看作Worksheet B(在運行宏之後)。

sample

,我想出了一些代碼,但因爲我是新來的VBA,這是行不通的,我現在很絕望。感謝任何人的幫助。

Sub Mymacro() 
    Dim lastRowC As Long 
    Dim foundTrue As Boolean 
    Dim Data As Worksheet 
    Dim GivenValues As Worksheet 
    Dim IDs As Long 
    Dim fVal As Range 

    Set Data = Sheets("Worksheet B") 
    Set GivenValues = Sheets("Worksheet A") 
    lastRowC = Data.Cells(Rows.Count, 1).End(xlUp).Row 
    IDs = GivenValues.Cells(Rows.Count, 1).End(xlUp).Row 
    'imagine data in Worksheet B are in the first column 

    For i = 1 To IDs 
     Set fVal = Data.Range("A1:A" & lastRowC).Find(GivenValues.Cells(i, 1).Value, LookIn:=xlValues, LookAt:=xlWhole) 
     If fVal Is Nothing Then 
      GivenValues.Cells(i, 1).Copy 
      Sheets(Data).Select 
      Range("A1").Select 
      Selection.End(xlDown).Select 
      ActiveCell.Offset(1, 0).Select 
      Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _ 
       :=False, Transpose:=False 
     Else: End If 
    Next i 
End Sub 
+1

爲什麼不使用'WORKSHEETFUNCTION.COUNTIF',然後複製,如果返回的是0。此外,'範圍(「A1」)。Select'你應該指的是2和選擇時,與張前綴的範圍。無需選擇,例如'Range(「A1」)。End(xlDown).offset(1,0).pastespecial' –

回答

0

代碼會是這樣的。

Sub Mymacro() 

    Dim lastRowC As Long 
    Dim foundTrue As Boolean 
    Dim Data As Worksheet 
    Dim GivenValues As Worksheet 
    Dim IDs As Long 
    Dim fVal As Range 
    Dim rngDB As Range, vDB, rngT As Range 
    Dim vR(), n As Long 

     Set Data = Sheets("Worksheet B") 
     Set GivenValues = Sheets("Worksheet A") 

     lastRowC = Data.Cells(Rows.Count, 1).End(xlUp).Row 
     IDs = GivenValues.Cells(Rows.Count, 1).End(xlUp).Row 
     Set rngDB = Data.Range("a1", "a" & lastRowC) 

     With GivenValues 
      vDB = .Range("a1", "a" & IDs) 
     End With 
'imagine data in Worksheet B are in the first column 
      For i = 1 To IDs 
       Set fVal = rngDB.Find(vDB(i, 1), LookIn:=xlValues, LookAt:=xlWhole) 
        If fVal Is Nothing Then 
         n = n + 1 
         ReDim Preserve vR(1 To n) 
         vR(n) = vDB(i, 1) 
        End If 
      Next i 
      Set rngT = Data.Range("a" & Rows.Count).End(xlUp)(2) 
      If n > 0 Then 
       rngT.Resize(n) = WorksheetFunction.Transpose(vR) 
      End If 
    End Sub 

如果你想除了重複,請看下一個代碼。

Sub Mymacro() 

    Dim lastRowC As Long 
    Dim foundTrue As Boolean 
    Dim Data As Worksheet 
    Dim GivenValues As Worksheet 
    Dim IDs As Long 
    Dim fVal As Range 
    Dim rngDB As Range, vDB, rngT As Range 
    Dim vR(), n As Long 
    Dim X As New Collection 

     Set Data = Sheets("Worksheet B") 
     Set GivenValues = Sheets("Worksheet A") 

     lastRowC = Data.Cells(Rows.Count, 1).End(xlUp).Row 
     IDs = GivenValues.Cells(Rows.Count, 1).End(xlUp).Row 
     Set rngDB = Data.Range("a1", "a" & lastRowC) 

     With GivenValues 
      vDB = .Range("a1", "a" & IDs) 
     End With 
'imagine data in Worksheet B are in the first column 
     On Error Resume Next 
      For i = 1 To IDs 
       Set fVal = rngDB.Find(vDB(i, 1), LookIn:=xlValues, LookAt:=xlWhole) 
        If fVal Is Nothing Then 
         Err.Clear 
         X.Add vDB(i, 1), CStr(vDB(i, 1)) 
         If Err.Number = 0 Then 
          n = n + 1 
          ReDim Preserve vR(1 To n) 
          vR(n) = vDB(i, 1) 
         End If 
        End If 
      Next i 
      Set rngT = Data.Range("a" & Rows.Count).End(xlUp)(2) 
      If n > 0 Then 
       rngT.Resize(n) = WorksheetFunction.Transpose(vR) 
      End If 
End Sub 
+0

謝謝,它現在可以工作,我會盡我所能學習你的代碼並從中學習。但是,仍然存在一個問題,即它將新值與工作表B上的原始範圍進行比較,它將粘貼所有重複的新值。例如,值「100」不在工作表B上,因此它在工作表A上粘貼五次,因爲它在工作表A上是五次。我只想要一次。我知道我現在可以運行一個簡單的宏來刪除重複項,但是有沒有更優雅的方法不能多次粘貼一些值?無論如何,大THX,你已經解決了我的問題 – Petanek333

+0

@ Petanek333:使用新的Collecton刪除重複。並修改我的代碼。 –