从excel中查找另一个excel中的号码是否存在

'******************************************
'*处理从既存数据中查找是否存在曾经用过的数据
'*既存数据excel要求:
'*   1.既存数据的列名为"MSN"
'*  2.列名所在行为sheet页中第2行
'*查询数据excel要求:
'*   1.查找的MSN的列名为"MSN"
'*   2.列名所在行为sheet页中第1行
'*   3.记录文件名的列为msn列的下一列
'******************************************

 

Option Explicit

Const COL_NAME = "MSN"                  '既存数据的列名为"MSN";  查找的MSN的列名为"MSN"
Const ORIGINAL_TITLE_ROW_NUM = 2  '既存数据EXCEL列名所在行为sheet页中第2行
Const RESULT_TITLE_ROW_NUM = 1   '查询数据excel列名所在行为sheet页中第1行


Dim WshShell
set WshShell = CreateObject("wscript.Shell")


'打开既存数据的Excel文件=============
Call WshShell.Popup("准备选择既存数据的Excel", 2)
dim OriginalPath
OriginalPath = GetFilePath
'打开要查询的excel文档
Dim oOriginalExcel
Dim oOriginalBook
Call OpenExcel(oOriginalExcel, oOriginalBook, OriginalPath)
'=======================================

 

'打开需查询数据的Excel文件=============
Call WshShell.Popup("准备选择查询数据的Excel", 2)
dim ResultPath
ResultPath = GetFilePath
'打开要查询的excel文档
Dim oResultExcel
Dim oResultBook
Call OpenExcel(oResultExcel, oResultBook, ResultPath)
'=======================================


Dim oResultSheet
Set oResultSheet = oResultExcel.activesheet
Dim inResultMSNCol
inResultMSNCol = GetColNumByName(oResultSheet, RESULT_TITLE_ROW_NUM, COL_NAME)


dim i
For i = 2 To oResultSheet.UsedRange.rows.Count
 Dim strFind
 strFind = CStr(oResultSheet.cells(i, inResultMSNCol))
 If (strFind = "") Then
  
 ElseIf FindStrFromExcel(oOriginalBook, strFind) Then
  oResultSheet.cells(i, inResultMSNCol + 1).value = oResultSheet.cells(i, inResultMSNCol + 1).value & vbCrLf & OriginalPath
 End if
Next

 

oResultExcel.save
Call WshShell.Popup("数据查询已结束!数据已保存!", 2)

 

oResultExcel.quit
oOriginalExcel.quit
Set oResultExcel = nothing
Set oOriginalExcel = Nothing
Set WshShell = nothing
WScript.Quit

 

 

 


'==================sub & function==================


sub OpenExcel(ByRef obj, ByRef workbook, path)
 On Error Resume Next
 '打开excel文档
 Set obj = CreateObject("excel.application")
 obj.Visible = false
 Set workbook = obj.Workbooks.Open(path)
 If Err.Number <> 0 Then
  obj.Quit
  WScript.Quit
 End If

End sub

 

Function GetFilePath
 Dim objDialog
 Set objDialog = CreateObject("UserAccounts.CommonDialog")
 objDialog.Filter = "Excel97-2003|*.xls|Excel2007|*.xlsx"
 objDialog.InitialDir = "c:/"
 Dim tfile
 tfile=objDialog.ShowOpen
 if tfile then
  GetFilePath = objDialog.FileName
 else
  WScript.Quit
 end if
End Function

 

'获得处理字段所在列
Function GetColNumByName(ByRef sheet, byval inTitleRow, ByVal strColName)

 GetColNumByName = 0
 Dim index
 For index = 1 To sheet.UsedRange.columns.Count
  If sheet.cells(inTitleRow, index) = strColName Then
   GetColNumByName = index
   Exit Function
  End if
 Next
End Function

 

'查看此sheet中是否存在MSN
Function StrExistsInSheet(ByRef sheet, byval inCol, ByVal strMSN)
 StrExistsInSheet = False
 Dim index
 For index = 2 To sheet.UsedRange.rows.Count
  If CStr(sheet.cells(index, inCol)) = strMSN Then
   StrExistsInSheet = true
   Exit Function
  End if
 Next
End Function

 

'查看此book中是否存在MSN
Function FindStrFromExcel(byref book, byval str)

 FindStrFromExcel = false

 Dim sheet
 For Each sheet In book.sheets
  Dim inMSNCol
  inMSNCol = GetColNumByName(sheet, ORIGINAL_TITLE_ROW_NUM, COL_NAME)
  If inMSNCol <> 0 Then
   If (StrExistsInSheet(sheet, inMSNCol, str)) Then
    FindStrFromExcel = True
    Exit function
   End if
  End If
 Next
 
End function

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值