目标:在重复数据中按照一定规则提取、组合。
Sub match()
Dim i, j, z, n, flag, A, B, C
Set A = Worksheets("Sheet1").UsedRange
Set B = Worksheets("Sheet2").UsedRange
Set C = Worksheets("Sheet3").UsedRange
For i = 3 To A.Rows.Count
For j = 2 To B.Rows.Count
If A.Cells(i, 1) = B.Cells(j, 2) Then
'险种名称
Debug.Print B.Cells(j, 2)
Select Case B.Cells(j, 3)
Case Is = A.Cells(1, 3)
'城镇职工基本医疗保险
'缴费基数 赋值列不同
A.Cells(i, 2) = Val(B.Cells(j, 6))
'缴纳金 赋值列不同
A.Cells(i, 3) = Val(B.Cells(j, 8))
A.Cells(i, 4) = Val(B.Cells(j, 9))
Case Is = A.Cells(1, 6)
'城镇企业职工基本养老保险
'缴费基数
A.Cells(i, 5) = Val(B.Cells(j, 6))
'缴纳金
A.Cells(i, 6) = Val(B.Cells(j, 8))
A.Cells(i, 7) = Val(B.Cells(j, 9))
Case Is = A.Cells(1, 8)
'公务员医疗补助
'缴费基数
A.Cells(i, 5) = Val(B.Cells(j, 6))
'缴纳金
A.Cells(i, 8) = Val(B.Cells(j, 8))
A.Cells(i, 9) = Val(B.Cells(j, 9))
Case Is = A.Cells(1, 10)
'生育保险
'缴费基数
A.Cells(i, 5) = Val(B.Cells(j, 6))
'缴纳金
A.Cells(i, 10) = Val(B.Cells(j, 8))
A.Cells(i, 11) = Val(B.Cells(j, 9))
Case Is = A.Cells(1, 12)
'工伤保险
'缴费基数
A.Cells(i, 5) = Val(B.Cells(j, 6))
'缴纳金
A.Cells(i, 12) = Val(B.Cells(j, 8))
A.Cells(i, 13) = Val(B.Cells(j, 9))
Case Is = A.Cells(1, 14)
'失业保险
'缴费基数
A.Cells(i, 5) = Val(B.Cells(j, 6))
'缴纳金
A.Cells(i, 14) = Val(B.Cells(j, 8))
A.Cells(i, 15) = Val(B.Cells(j, 9))
Case Else
End Select
End If
Next
Next
'把名字找到,然后将属性整合
j = 3
flag = 0
'Debug.Print C.Cells(j, 1)
For i = 3 To A.Rows.Count
If C.Cells(j, 1) = "" Then
For z = 3 To j 'z用于控制待写入数据和前面数据的重复比较次数
'Debug.Print z
If C.Cells(z, 1) = A.Cells(i, 1) Then
If C.Cells(z, 1) <> "" Then
flag = 1 'flag用于判断待写入数据是否和前面的数据重复
Debug.Print z
End If
End If
Next
If flag = 0 Then '只有经过上面的重复数据循环判断,才能确定这个数据不是重复数据
C.Cells(j, 1) = A.Cells(i, 1)
For n = 2 To 15 'n 用于控制写出数据的列数
C.Cells(j, n) = A.Cells(i, n)
Next
Debug.Print C.Cells(j, 1)
j = j + 1 '
End If
End If
flag = 0
Next
Debug.Print "Done"
End Sub