如何使用access vba在excel行中查找指定的数据

时间:2014-02-26 17:58:30

标签: vba access-vba

我正在尝试在excel行中查找指定的数据。与行范围A1:A1J一样,只有一个单元格具有“处理”数据。如果我发现给定范围内的数据,我需要弹出消息“找到”这是我的代码

         Dim ExcelApp As New Excel.Application
         Dim ExcelBook As New Excel.Workbook
         Dim rng As Excel.Range
         Dim rngDefine As Excel.Range

         Set ExcelBook = ExcelApp.Workbooks.Open("C\temp\find.xlsm")
         ExcelApp.Visible = False       
        'Define your own Range
         Set rngDefine = ExcelBook.Worksheets("Datatab").Range("A1:AJ1")

         'ExcelBook.Worksheets("Datatab").Range ("A1:AJ1")

         Set c = .Find("Process", LookIn:=xlValues)
         For Each rng In rngDefine
         If c = "Process" Then
         MsgBox "Found"
         End If
           Next

         ExcelApp.Quit
         Set ExcelApp = Nothing

不工作。我需要添加任何更新的代码吗?

1 个答案:

答案 0 :(得分:2)

第一种方法(稍快):

Set rngDefine = ExcelBook.Worksheets("Datatab").Range("A1:AJ1")

If IsError(ExcelApp.Match("Process", rngDefine, 0)) Then
    MsgBox "Not found"
Else
    MsgBox "Found"
End If

第二种方法:

Dim c As Excel.Range

Set rngDefine = ExcelBook.Worksheets("Datatab").Range("A1:AJ1")

Set c = rngDefine.Find(What:="Process", LookIn:=xlValues, LookAt:=xlWhole, MatchCase:=False)

If c Is Nothing Then
    MsgBox "Not found"
Else
    MsgBox "Found"
End If