2013-11-09 57 views
4

如何在Excel工作表中找到字符串值? 我正在嘗試objRange.Find(),但這也給我錯誤的地址。例如我想'Object_paint'的地址,但它也給出了'Object_paint_and_stk'的地址如何在Excel工作表中找到確切值

我應該如何得到只有確切的價值..?

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 
+0

你能提供一個樣本電子表格嗎? – Chelseawillrecover

回答

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. 

BTW:誰是objTarget?

更新:

你要 '口'(一些提示here)VBA代碼段,您在您的評論提到爲VBScript:

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 
+0

順便說一句:誰是objTarget? : 它應該是objFind。對不便(編輯) – mhs

+0

我找到了這個。 「查找(what:=」value「,lookat:= xlWhole)」。 這是我應該找的。但是當我嘗試將它應用於vbscript時,我失敗了。 – mhs

+0

@narak - 請參閱更新。 –

相關問題