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
此外,圖像(至少對我來說),不會在寄存器可達... –
@Tiago卡多佐你能再試一次嗎?看看它有效嗎? – Vivian
@Vivian我剛試過,仍然需要註冊 –