我试图在下面的例子中做,但不能这样做。
Sheet1中的Excel数据:
id Sysid option status
XYZ XT Open status_1
US XT Close Status_1
US XT Open Stauts_2
NJ XT Open Status_2
IND VX Close Status_1
BAN VX Open Status_1
CHN XY Open Status_1
YST xy Close Status_1
在上面的数据中,Status_1和Sysid定义了唯一记录,根据这个条件,我必须找出哪个记录对于该唯一记录组合具有开放和差异的差异。如果选项不同,我必须使用excel宏将该数据复制到不同的表。这应该只适用于id的US和CHN.Suppose如果id有US,它应该与基于sysid和Status的匹配记录进行比较。任何帮助表示赞赏。
输出如下:
XYZ XT Open status_1
US XT Close Status_1
CHN XY Open Status_1
YST xy Close Status_1
答案 0 :(得分:3)
编辑包括“US”或“CHN”的测试
Sub Tester()
Const COL_ID As Integer = 1
Const COL_SYSID As Integer = 2
Const COL_STATUS As Integer = 4
Const COL_OPTION As Integer = 3
Const VAL_DIFF As String = "XXdifferentXX"
Dim d As Object, sKey As String, id As String
Dim rw As Range, opt As String, rngData As Range
Dim rngCopy As Range, goodId As Boolean
Dim FirstPass As Boolean, arr
With Sheet1.Range("A1")
Set rngData = .CurrentRegion.Offset(1).Resize( _
.CurrentRegion.Rows.Count - 1)
End With
Set rngCopy = Sheet1.Range("F2")
Set d = CreateObject("scripting.dictionary")
FirstPass = True
redo:
For Each rw In rngData.Rows
sKey = rw.Cells(COL_SYSID).Value & "<>" & _
rw.Cells(COL_STATUS).Value
If FirstPass Then
'Figure out which combinations have different option values
' and at least one record with id=US or CHN
id = rw.Cells(COL_ID).Value
goodId = (id = "US" Or id = "CHN")
opt = rw.Cells(COL_OPTION).Value
If d.exists(sKey) Then
arr = d(sKey) 'can't modify the array in situ...
If arr(0) <> opt Then arr(0) = VAL_DIFF
If goodId Then arr(1) = True
d(sKey) = arr 'return [modified] array
Else
d.Add sKey, Array(opt, goodId)
End If
Else
'Second pass - copy only rows with varying options
' and id=US or CHN
If d(sKey)(0) = VAL_DIFF And d(sKey)(1) = True Then
rw.Copy rngCopy
Set rngCopy = rngCopy.Offset(1, 0)
End If
End If
Next rw
If FirstPass Then
FirstPass = False
GoTo redo
End If
End Sub
答案 1 :(得分:1)
使用宏录制器并对数据执行独特的高级过滤器,然后您可以进行比较。
http://office.microsoft.com/en-us/excel-help/filter-for-unique-records-HP003073425.aspx?CTT=3