我试图从outlook脚本中搜索单元格的值并检查它是否存在于excel文件中,如果是,打开excel文件,否则什么也不做。我可以打开文件并搜索范围内的值。但我的问题是我不知道如何在范围内搜索该值并在没有打开excel文件的情况下获得它的位置。
编辑1: 这是我的详细问题:EX: 我在列#34;电话号码"处有一个电话号码。我想找一下"电话号码" (因为有时它会改变到另一列)。在找到列的位置后,我想搜索数字" 123876"该列中存在(直到此时excel文件仍然关闭)。现在,如果编号为" 123876"存在,打开excel文件,否则什么都不做。
这是我的代码,用于搜索文件是否打开
Sub test()
Dim objExcel As Object
Dim WB As Object
Dim WS As Object
'Open excel file
Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = True
Set WB = objExcel.Workbooks.Open("D:\Book1.xlsm")
WB.Activate
Set WS = WB.Worksheets("Sheet1")
'Search position of column "Phone Number"
Phone_Number_Col = Chr(WS.Range("A:Z").Find("Phone Number", LookIn:=xlValues).Column + 64) 'It will return 5 and change to "E" for this column
'Search whether does my number is exist in this file
Dim range_1 As Range
Set Found_Nprod = WS.Range(Phone_Number_Col & ":" & Phone_Number_Col).Find("123876", LookIn:=xlValues)
If Not Found_Nprod Is Nothing Then 'found my number
MsgBox ("This value is existed")
Else ' not find my number
MsgBox ("This value is not existed in this file")
End If
End Sub
以上代码可以在打开excel文件时找到。但我的问题是如何在没有打开文件的情况下找到这样的文件,它只是在该文件有我的号码时打开文件" 123876"
编辑2:
我发现代码的和平可以在不打开的情况下获得单元格的价值。它运行正常但我不知道如何使用它的查找功能。 这是我发现的功能
Sub ReadClosed()
'
' Credit this To Bob Umlas
'
Dim strPath As String
Dim strFile As String
Dim strInfoCell As String
strPath = "D:\"
strFile = "Book1.xlsm"
i = 3
strInfoCell = "'" & strPath & "[" & strFile & "]Sheet1'!R" & i & "C1"
MsgBox "In Cell A1 = " & ExecuteExcel4Macro(strInfoCell), vbInformation, strFile
答案 0 :(得分:0)
好的,这就是我可以帮助你的方法 - 这是我正在使用的代码,它会为你提供所需字符串的行,如果你在哪个列中搜索它。如果它不是想要的字符串,则返回-1。如果您希望第二个可重复的字符串,则应在可选参数l_more_values_found
中指定值2
。如果您的字符串为Phone
且表单中为Phones
,则应将look_for_part
设置为True
。很多,这就是它的工作原理。让我们想象你有这个:
如果您运行MyTest Sub,结果会得到4。 4是电话号码155,其作为l_locate_value_row
的参数给出。在您的情况下,您可以在知道必须搜索的列后检查,是否返回-1
。
代码如下:
Public Function l_locate_value_row(target As String, ByRef target_sheet As Worksheet, _
Optional l_col As Long = 2, _
Optional l_more_values_found As Long = 1, _
Optional b_look_for_part = False) As Long
Dim l_values_found As Long
Dim r_local_range As Range
Dim my_cell As Range
l_values_found = l_more_values_found
Set r_local_range = Nothing
target_sheet.Activate
Set r_local_range = target_sheet.Range(target_sheet.Cells(1, l_col), target_sheet.Cells(Rows.Count, l_col))
For Each my_cell In r_local_range
'The b_look_for_part is for the vertriebscase
If b_look_for_part Then
If target = Left(my_cell, Len(target)) Then
If l_values_found = 1 Then
l_locate_value_row = my_cell.Row
Exit Function
Else
l_values_found = l_values_found - 1
End If
End If
Else
If target = Trim(my_cell) Then
If l_values_found = 1 Then
l_locate_value_row = my_cell.Row
Exit Function
Else
l_values_found = l_values_found - 1
End If
End If
End If
Next my_cell
l_locate_value_row = -1
End Function
Public Sub MyTest()
Dim l_col As Long
l_col = l_locate_value_row("155", ActiveSheet, 3, 1, False)
Debug.Print l_col
End Sub
答案 1 :(得分:0)
ADODB查询非常适合从已关闭的工作簿中检索数据。
Function hasPhoneNumber(FilePath As String, PhoneNumber As Variant) As Boolean
Const adOpenStatic = 3, adLockOptimistic = 3, adCmdText = 1
Dim conn As Object, rs As Object
Set conn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")
conn.Open "Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=" & FilePath & _
";Extended Properties=Excel 12.0;"
rs.Open "SELECT (Count([Phone Number]) > 0) AS hasPhoneNumber FROM [Sheet1$]" & _
" WHERE Cstr([Phone Number])='" & PhoneNumber & "';", conn, adOpenStatic, adLockOptimistic, adCmdText
hasPhoneNumber = CBool(rs!hasPhoneNumber)
On Error Resume Next
rs.Close
Set rs = Nothing
conn.Close
Set conn = Nothing
End Function