比较两个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