Sub LOOKUP_UChur()
Dim i As Long
‘=== sourceWorksheet = 数据源表名称
Dim sourceWorksheet As Worksheet
Dim taskWorkSheet As Worksheet
Dim bgnTime, endTime As Date
‘*********************************************
‘ [1] ------ 数据源表 *** Sheet 名称
Set sourceWorksheet = ThisWorkbook.Worksheets("全县数据")
Const swsh_KeyColName = "R" ‘ 关键列 , 身份证号所在的列名称
Const swsh_BeginRow = 2 ‘ 开始行号
‘ [2] ------ 任务表 *** Sheet 名称
Set taskWorkSheet = ThisWorkbook.Worksheets("Sheet4")
Const twsh_KeyColName = "C" ‘ 关键列号 , 身份证号所在的列号
Const twsh_BeginRow = 2 ‘ 开始行号
‘*********************************************
bgnTime = Now()
Dim arrKeyData() As Variant ‘ 这种声明方式是声明一个动态数组
arrKeyData = sourceWorksheet.Range(swsh_KeyColName & swsh_BeginRow & ":" & swsh_KeyColName & sourceWorksheet.UsedRange.Rows.Count)
For i = twsh_BeginRow To taskWorkSheet.UsedRange.Rows.Count
Debug.Print "第 ["; i & "]行: 已找到数据:" & taskWorkSheet.Range(twsh_KeyColName & i).Text
DoEvents
curRow = GetRowNo(arrKeyData, taskWorkSheet.Range(twsh_KeyColName & i).Text)
If curRow > 0 Then
‘[3] ???? ===== 任务表 J --> 数据源 A
taskWorkSheet.Range("J" & i) = sourceWorksheet.Range("A" & swsh_BeginRow + (curRow - 1)).Text
‘[4] ????? ====== 任务表 K --> 数据源 P
taskWorkSheet.Range("K" & i) = sourceWorksheet.Range("P" & swsh_BeginRow + (curRow - 1)).Text
End If
Next i
endTime = Now()
MsgBox ("任务已完成, 处理所需的时间: 【" & bgnTime & " :==:" & endTime & "】 ::: " & DateDiff("s", bgnTime, endTime) & " 秒")
DoEvents
End Sub
Function GetRowNo(ByRef pArrKeyData As Variant, pFindValue As String) As Long
GetRowNo = 0
If Not (UBound(pArrKeyData) > 0) Then Exit Function
Dim j As Long
For j = LBound(pArrKeyData) To UBound(pArrKeyData)
If pArrKeyData(j, 1) = pFindValue Then
GetRowNo = j
Exit Function
End If
Next j
End Function
原文:http://www.cnblogs.com/m0488/p/7747710.html