2016-08-15 213 views
-1

我目前正在比較2個手動輸入郵政編碼的數據庫。我需要比較每個數據庫中數百個帳戶的郵政編碼,以檢查是否有任何遺漏。我已經在Excel中按升序排列了所有的值,但似乎找不到一個快速檢查缺失的方法。比較2個單元格並在Excel中查找缺失值

Column A: Database A ZIPS (The correct ZIPs) 
14464, 14515, 14612, 14615, 14626 

Column B: Database B ZIPS (Manually Entered) 
14464, 14612, 14615, 14626 

Column C: Missing ZIPs 
14515 

編輯:我應該澄清,該數據被以這種方式存儲的每個..拉鍊沒有被存儲在單獨的列中,存在用於每個代理的多個拉鍊。

Image of worksheet

我知道一定有發現使用Excel VBA該值的方式!

感謝

+1

這將幫助:https://msdn.microsoft.com/en-us/library/office/ff194701.aspx – cyboashu

+1

@pnuts一個是Microsoft CRM,另一個位置是谷歌雲存儲服務,需要下載後修改 – CHopp

回答

0

給這個一去 Public Function ShowMissingZips(rngSource As Range, _ rngMatch As Range) As String Dim colSource As Collection Dim colMatch As Collection Dim colOutput As Collection Dim varSource As Variant Dim varMatch As Variant Dim varOutput As Variant Dim intCounter As Integer Dim blnMatched As Boolean Dim strSource As String Dim strMatch As String Dim strOutput As String

Set colSource = New Collection 
Set colMatch = New Collection 
Set colOutput = New Collection 

strSource = Replace(rngSource.Value, " ", "") 
For Each varSource In Split(strSource, ",") 
    colSource.Add varSource 
Next 

' Clean up source data 
strMatch = Replace(rngMatch.Value, " ", "") 
For Each varSource In Split(strMatch, ",") 
    colMatch.Add varSource 
Next 

' Clean up match data  
For Each varSource In colSource 
    blnMatched = False 
    For Each varMatch In colMatch 
     If varSource = varMatch Then 
      blnMatched = True 
      Exit For 
     End If 
    Next 

    ' Note if it's not matched 
    If Not blnMatched Then 
     colOutput.Add varSource 
    End If 
Next 

' Only output if there's anything present 
If colOutput.Count > 0 Then 
    For Each varOutput In colOutput 
     strOutput = strOutput & CStr(varOutput) & ", " 
    Next 

    strOutput = Left$(strOutput, Len(strOutput) - 2) 
End If 

ShowMissingZips = strOutput 

End Function

要使用它,按Alt + F11去VBA編輯器。在項目瀏覽器中找到您的工作簿(如果不可見,請按Ctrl-R),然後在頂部的菜單上單擊插入...,模塊。

粘貼此代碼。

返回到您的工作簿,假設你已經把列作爲前(A,B & C和2行作爲第一個數據行),進入到小區C2並鍵入

=ShowMissingZips(A2,B2)

你應該會看到你以後。這不太好,我通常會添加錯誤處理,但它會做一個快速修復。

當您保存它時,請務必使用XLSM格式(Excel 2007+),以便保留VBA。

+0

工作完美!非常感謝你! – CHopp

1

答案之前看到作者的數據格式 幸運的是,任務不太難。你只需要簡單地使用:

=IF(COUNTIF(list,value),"Output if it exists","Output if missing") 

所以你的情況,你使用定義列...

=IF(COUNTIF($B:$B,$A1),"",A1) 

然後套用公式進行正確的拉鍊列的長度。

看到:https://exceljet.net/formula/find-missing-values

Example picture here

+0

我應該澄清,所有的值都包含在同一個單元格中。因此,列A中的所有拉鍊都在A1中,而列B中的所有拉鍊都在B1中 – CHopp

+0

您可以轉到數據 - >文本到列 - >分隔 - > csv 然後應用公式 – WRosko

+0

我明白這可行,但我正在尋找一個VBA來節省時間。爲此任務執行文本操作將爲每個代理創建數百列,並創建一個不可能的環境來比較值 – CHopp

相關問題