2011-07-19 63 views
2

Sheet1Sheet2如何比較Excel-VBA中兩個單獨的工作表之間的字符串差異?

上面我有兩個圖像聯繫起來,我已經從我的Excel文件(Sheet1中,表2)

這裏有一個簡短的描述基本上,我只是希望我的宏比較型號(C列捕獲)從兩張表中找出差異。當在兩張紙上檢測到字符串差異時,它將突出顯示兩張BOM清單上的行,以向用戶指示零件號(C列)中的差異。但是,這也是一個問題,在圖像中看到有一些帶有「空間」的行,循環必須注意防止比較空字符串,從而給出錯誤的結果。

對不起,如果我不清楚你的英語和不好的英語命令和解釋。有人可以指導我做這件事,我對於在哪裏或如何着手,而沒有事先了解有關excel-vba編程的理解,我必須在一週內完成此任務。

更新時間:

我已經更新了我的帖子能有人來看看,並給我你的意見如何,我可以更改代碼以列A的整排突出爲P,而不是C列的範圍值只有差異?

Sub differences() 

    Dim ws1 As Worksheet, ws2 As Worksheet 
    Dim lastRow1 As Integer, lastrow2 As Integer 
    Dim rng1 As Range, rng2 As Range, temp As Range, found As Range 

    Application.ScreenUpdating = False 

    Set ws1 = ThisWorkbook.Sheets("Sheet1") 
    Set ws2 = ThisWorkbook.Sheets("Sheet2") 

    lastRow1 = ws1.Range("A" & Rows.Count).End(xlUp).Row 
    lastrow2 = ws2.Range("A" & Rows.Count).End(xlUp).Row 

    Set rng1 = ws1.Range("C21:C" & lastRow1) 
    Set rng2 = ws2.Range("C21:C" & lastrow2) 

    For Each temp In rng1 
     Set found = Find_Range(temp.Value, rng2, , xlWhole) 
     If found Is Nothing Then 
      temp.Interior.ColorIndex = 3 
     End If 
    Next temp 

    For Each temp In rng2 
     Set found = Find_Range(temp.Value, rng1, , xlWhole) 
     If found Is Nothing Then 
      temp.Interior.ColorIndex = 3 
     End If 
    Next temp 


End Sub 

Function Find_Range(Find_Item As Variant, Search_Range As Range, Optional LookIn As Variant, Optional LookAt As Variant, Optional MatchCase As Boolean) As Range 

    Dim c As Range 
    Dim firstAddress As String 

    If IsMissing(LookIn) Then LookIn = xlValues 'xlFormulas 
    If IsMissing(LookAt) Then LookAt = xlPart 'xlWhole 
    If IsMissing(MatchCase) Then MatchCase = False 

    With Search_Range 
     Set c = .Find(What:=Find_Item, LookIn:=LookIn, LookAt:=LookAt, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=MatchCase, SearchFormat:=False) 
     If Not c Is Nothing Then 
      Set Find_Range = c 
      firstAddress = c.Address 
      Do 
       Set Find_Range = Union(Find_Range, c) 
       Set c = .FindNext(c) 
      Loop While Not c Is Nothing And c.Address <> firstAddress 
     End If 
    End With 
End Function 
+0

此外,圖像(至少對我來說),不會在寄存器可達... –

+0

@Tiago卡多佐你能再試一次嗎?看看它有效嗎? – Vivian

+0

@Vivian我剛試過,仍然需要註冊 –

回答

0

正如我不能看到的圖像,我會假設你正在嘗試做的是檢查,如果在其他列表中存在一個零件號,如果沒有那麼突出了。

關閉我的頭頂,這是你基本上需要做的。

Public Sub Test() 
    CompareRange Sheet1.Range("A2", "A8"), Sheet2.Range("A2", "A8") 
End Sub 


Public Sub CompareRange(range1 As Range, range2 As Range) 
    Dim CompareCell As Range 
    Dim CheckCell As Range 
    Dim CellFound As Boolean 
    For Each CompareCell In range1.Cells 
     CellFound = False 
     For Each CheckCell In range2.Cells 

      If CheckCell.Text = CompareCell.Text Then 
       CellFound = True 
      End If 
     Next CheckCell 
     If Not CellFound Then 
      CompareCell.Interior.Color = vbYellow 
     End If 
    Next CompareCell 
End Sub 

有一點要注意的是,這個函數假定你有一個單獨的列範圍。否則它會檢查您的範圍內的所有單元格,可能會突出顯示超出您的意圖。

編輯
至於突出的行
嘗試添加這對您的發現循環

Dim CompareSheet as Worksheet 'Add at top of function 

'Add to the For Each Loop 
Set CompareSheet = temp.Worksheet 
CompareSheet.Range("A" & temp.Row, "P" & temp.Row).Interior.ColorIndex = 3 
+0

根據Vivian提供的信息,這可能是一個可能的解決方案(只需將A列替換爲C列,@Nathan因爲看不到圖像而無法知道)。 –

+0

@Nathan Fisher我在這裏有另一個問題http://stackoverflow.com/questions/6784915/how-to-import-excel-related-bom-list-content-into-a-excel-sheet-in-excel-vba你可以看看,並給我一些關於如何解決它的建議嗎? – Vivian

相關問題