比較兩個excelsheets在同一工作簿中。比較兩個excelsheets基於公共'id'字段(列)
我要檢查從Sheet1記錄是否在Sheet2中記錄的基礎上共同Question_id(包括工作表的列A)
這question_id(列)完全相同具有值,如
1
1a
1a.1
1a.1a
1a.1b
1a.1c
2
2a
2a.1
2a.1a
2a.1b
2a.1c etc....
我想比較基於此Question_id(列A值)的記錄。
如果Question_id是相同的,並且記錄(剩餘行)不一樣,然後我在着色紅色背景的記錄(僅適用於特定的細胞,而不是整個行)
出於同樣的,我有以下的代碼。
Sub RunCompare()
Call compareSheets("Sheet1", "Sheet2")
End Sub
Sub compareSheets(shtSheet1 As String, shtSheet2 As String)
Dim mycell As Range
Dim mydiffs As Integer
Application.ScreenUpdating = false
'Color Uncommon records in Red Background
For Each mycell In ActiveWorkbook.Worksheets(shtSheet2).UsedRange
If Not mycell.Value = ActiveWorkbook.Worksheets(shtSheet1).Cells(mycell.Row, mycell.Column).Value Then
mycell.Interior.Color = vbRed
mydiffs = mydiffs + 1
End If
Next
'Display no. of differences
MsgBox mydiffs & " differences found", vbInformation
ActiveWorkbook.Sheets(shtSheet2).Select
MsgBox "Data Scrubbed Successfully..."
Application.ScreenUpdating = True
End Sub
上面的代碼運行正常時,我有在兩個excelsheets Question_id(和因此的記錄)的相同序列。
假設我在兩張表中都有不同順序的Question_id(以及記錄)。
那麼我該如何實現這個......?
像在我的代碼使用where子句Where Sheet1.Question_id = Sheet2.Question_id
即我從工作表Sheet1只有拿起question_id和全行,我會根據匹配Question_id(A列的值),對比較Sheet2中記錄。
有人可以告訴我哪裏可以放置條件和什麼類型的條件,即使這兩個excelsheets都有隨機序列的Question_id;我將能夠比較sheet1和sheet2中的記錄。
編輯:於2015年3月23日
我已經改變了使用代碼find()方法,而不是下面的循環: 不過我在解決方案沒有帶到達。 這裏我試圖列出從Sheet2的工作表Sheet 3中的所有不匹配行的Question_Ids - 列A.
Option Explicit
Sub test()
Dim rng As Range, c As Range, cfind As Range, mycell As Range, cfindRow As Range
On Error Resume Next
Worksheets("Sheet3").Cells.Clear
With Worksheets("Sheet2")
Set rng = .Range(.Range("A2"), .Range("a2").End(xlDown))
For Each c In rng
With Worksheets("Sheet1")
Set cfind = .Columns("A:A").Cells.Find _
(what:=c.Value, lookat:=xlWhole)
'Find method always returns Range; So the following line should be something If cfind is not Nothing OR cfind <> Nothing (Both the syntaxes are wrong. Suggest me the right syntax please.
If cfind = 1 Then
'Here please tell me how to reference a whole row based on Column A value
'Here using cfind and again using mycell is something wrong as mycell variable again compares rows in sheet2 with rows in sheet1 which include Question_Id too.
Set mycell = ActiveWorkbook.Worksheets("Sheet2").UsedRange.End(xlDown)
'My both the excelsheets have values from columns A to AD. Still I want to make the code for all used Ranges of columns instead of only A to AD.
Set cfindRow = Worksheets("Sheet1").Rows("A2:AD").Cells.Find _
(what:=mycell.Value, lookat:=xlWhole)
'Find method always returns Range; So the following line should be something If cfindRow is not Nothing OR cfindRow <> Nothing (Both the syntaxes are wrong. Suggest me the right syntax please.
If cfindRow = 1 Then
'MsgBox "Match Found" 'Right Now do Nothing
End If
Else
' mycell.Interior.Color = vbRed
' mydiffs = mydiffs + 1
'Copy the question numbers to sheet3 either if they are new in new sheet (Sheet2) or content against them (in the whole row-any column value) is changed.
cfind.Copy Worksheets("sheet3").Cells(Rows.Count, "A").End(xlUp).Offset(1, 0)
End If
End With
Next c
Application.CutCopyMode = False
End With
MsgBox "Data Scrubbed Successfully..."
End Sub
誰能告訴我如何引用基於鍵列值的範圍是多少?
我對解決方案的新方法:
(It may be a hint to give me answer on how to reference Row values based on key column
)
Getting row indices of both the sheets; column A values (Question_Id's) i.e.
c.Row and cfind.Row
Then
Check If(Sheet2.Cells(c.Row, Columns) = Sheet1.Cells(cfind.Row, Columns) (To compare columns against matching Question_Ids only.)
所以最後這個什麼都試圖實現:
1)比較基於鍵列兩頁:
從Sheet2 - 列A中提取Question_Id,並將其與列A在Sheet1中。如果來自兩張圖紙的關鍵列匹配,並且與它們對應的內容(完整行)匹配 - 則不執行任何操作。
如果鍵列值(Question_Id - 列A)的比賽,但反對它的值(行)不符合他們的顏色特定細胞(僅細胞),而不是整個行中紅色背景。
2)在sheet2中存在但不在sheet1中的Question_Id應該列在sheet3的第一列下。從A2開始。
3)在sheet1中有但是在sheet2中沒有的Question_Id應該在sheet3的第二列下面列出。從B2開始。
好吧,你想找到或突出顯示板的'Question_id's這些都不是在另一片? – 2015-03-19 08:43:56
您是否試圖首先自己解決問題?它看起來像你已經實施了一個問題的解決方案,然後要求我們實施一個解決方案,以解決相關但非常不同和更復雜的問題。這不是一個問題,那是一個代碼請求。 – Aiken 2015-03-19 09:36:30
@ shA.t不完全。相反,我想比較兩張表中的問題ID。如果它們匹配,我只想對照他們檢查剩餘的列值;和哪一列不匹配;我只想突出顯示那些單元格。 (目前上面的代碼在兩個excelsheets都具有相同的Question_id序列時都可以這樣做。) – Avidan 2015-03-19 09:57:27