您的位置:首页 > 其它

Excel 取两页中相同的数据项算法一 时间复杂度O(n2)

2011-06-15 12:53 447 查看
工作需要,有时要从Excel的两页中提取一些共同的数据项出来分析, 而两页中的数据都没有按某关键列排过过序, 简单的用VBA写了一个抽取数据的方法:

Sheet6 中存放Sheet3 和 Sheet5中相同的数据项.

public Sub GetMixedInfo()
Dim i As Integer
Dim j As Integer
Dim keyword As String
'copy the header
Sheet3.Range("1:1").Copy (Sheet6.Cells(1, 1))
Sheet3.Rows(2).Copy (Sheet6.Cells(2, 1))
'copy the body
For i = 3 To 53
keyword = Sheet2.Cells(i, 3)
For j = 3 To 251
If keyword = Sheet3.Cells(j, 3) Then
'Sheet3.Range(Cells(j, 1), Cells(j, 256)).Copy (Sheet6.Cells(i, 1))
Sheet3.Rows(j).Copy (Sheet6.Cells(i, 1))
Exit For
End If
Next j
Next i
'copy the ender
Sheet3.Rows(54).Copy (Sheet6.Cells(54, 1))
End Sub


算法的时间复杂度为O(m*n) = O(n2)

若果你不在意时间, 数据规模也不是很大, 用上面的函数就够用了.

下篇会介绍, 先对Sheet3 和 Sheet5中数据排序, 时间复杂度为O(m+n) = O(n)的情况.

Author:David

Mail:xiang.okay@gmail.com
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: