Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find an exact value in an Excel sheet

Tags:

vbscript

How could I find a string value in an excel sheet. I am trying objRange.Find() but this give me the wrong address too. As example i want the address of 'Object_paint' but it gives also the address of 'Object_paint_and_stk'

How should i get only exact value..?

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
like image 544
mhs Avatar asked Oct 03 '22 11:10

mhs


1 Answers

First hit for

excel range.find exact match site:microsoft.com

is:

Range.Find

There you find

LookAt Type: System.Object

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

and following the LookAt link, you see:

xlPart  Match against any part of the search text.
xlWhole Match against the whole of the search text.

BTW: Who is objTarget?

Update:

You have to 'port' (some hints here) the VBA code snippet you mention in your comment to 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

output:

cscript excelfind.vbs
Range Title10
Range Title1
like image 155
Ekkehard.Horner Avatar answered Oct 19 '22 12:10

Ekkehard.Horner