如何在Excel工作表中查找确切的值

时间:2013-11-09 12:39:44

标签: vbscript

如何在Excel工作表中找到字符串值。 我正在尝试objRange.Find(),但这也给了我错误的地址。例如,我想要'Object_paint'的地址,但它也给出'Object_paint_and_stk'的地址

我应该如何得到确切的价值..?

Set objWorksheet = objWorkbook.Worksheets(worksheet)
Set objRange = objWorksheet.Range("B2:B600")    
Set objFind = objRange.Find("object_paint")

If Not objFind Is Nothing Then
    searchValue = objFind.AddressLocal(False, False)
end if

1 个答案:

答案 0 :(得分:4)

首先点击

excel range.find exact match site:microsoft.com

是:

Range.Find

你找到了

  

注视       键入:System.Object

Optional Object. Can be one of the following XlLookAt constants: xlWhole or xlPart.

并在LookAt link之后,您会看到:

xlPart  Match against any part of the search text.
xlWhole Match against the whole of the search text.
顺便说一句:谁是objTarget?

<强>更新

您必须'port'(一些提示here)您在对VBScript的评论中提到的VBA代码段:

Option Explicit

' Define Excel Consts unknown to VBScript
Const xlPart  = 2 
Const xlWhole = 1 

Dim oExcel : Set oExcel = CreateObject("Excel.Application")
Dim oWBook : Set oWBook = oExcel.Workbooks.Open("M:\lib\kurs0705\testdata\ut.xls")
Dim oRange : Set oRange = oWBook.Sheets("NewSheet").Range("A1:A11")
' translate named args to positional args
' expression.Find(What, After, LookIn, LookAt, SearchOrder, SearchDirection, MatchCase, MatchByte)
Dim oFnd1  : Set oFnd1  = oRange.Find("Title1", , , xlPart)
WScript.Echo TypeName(oFnd1), CStr(oFnd1)
Dim oFnd2  : Set oFnd2  = oRange.Find("Title1", , , xlWhole)
WScript.Echo TypeName(oFnd2), CStr(oFnd2)

oWBook.Close
oExcel.Quit

输出:

cscript excelfind.vbs
Range Title10
Range Title1