Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > VB Script

Vista - Vbscript to Search Excel

Reply
 
Old 01-23-2009   #1 (permalink)
Codeblack


 
 

Vbscript to Search Excel

Hi,

I am trying to write a Vbscript which will search for some keywords in all
the worksheets of a excel file and if a match is found then the script should
write that row to a text file. I am able to find in which sheet the keywords
are in. But i am not able to find a way to write the value of that row to
text file. I am completely lost. Can anyone of you please help me in writing
the matched value to text file. Thanks in advance. Below is the code.

Const ForReading = 1
strSearchTerm = ["abc","pqrs","test"]
strPath = Replace(WScript.ScriptFullName, WScript.ScriptName, "") & "test.xls"
boolMatchCase = False


Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = false
objExcel.DisplayAlerts = FALSE
objExcel.ScreenUpdating = False
Const xlFormulas = -4123
Const xlPart = 2
Const xlByRows = 1
Const xlNext = 1


Set objWorkbook = objExcel.Workbooks.Open(strPath, False, True)
For Each objWorkSheet In objWorkBook.Sheets
intFoundRow = -1
objWorkSheet.Activate

Set objCell = objWorkSheet.Cells(1, "A")
Set objCell = objWorkSheet.Cells.Find(strSearchTerm, objCell, xlFormulas,
xlPart, xlByRows, xlNext, boolMatchCase)

If Not objCell Is Nothing Then

If objCell.Row > intFoundRow Then
strResults = strResults & VbCrLf & objWorkSheet.Name
intFoundRow = objCell.Row

Else
Set objCell = Nothing
End If

End If
Next
objWorkBook.Close
objExcel.ScreenUpdating = True
objExcel.Quit



If strResults <> "" Then
Wscript.echo strSearchTerm & "Keyword was found on the following sheets:" &
strResults
Else
Wscript.echo strSearchTerm & " Keyword was not found"
End If


My System SpecsSystem Spec
Old 01-23-2009   #2 (permalink)
Pegasus \(MVP\)


 
 

Re: Vbscript to Search Excel


"Codeblack" <Codeblack@xxxxxx> wrote in message
news:EC354DDB-D2E7-48C8-8779-13EE7AD86919@xxxxxx
Quote:

> Hi,
>
> I am trying to write a Vbscript which will search for some keywords in all
> the worksheets of a excel file and if a match is found then the script
> should
> write that row to a text file. I am able to find in which sheet the
> keywords
> are in. But i am not able to find a way to write the value of that row to
> text file. I am completely lost. Can anyone of you please help me in
> writing
> the matched value to text file. Thanks in advance. Below is the code.
>
> Const ForReading = 1
> strSearchTerm = ["abc","pqrs","test"]
> strPath = Replace(WScript.ScriptFullName, WScript.ScriptName, "") &
> "test.xls"
> boolMatchCase = False
>
>
> Set objExcel = CreateObject("Excel.Application")
> objExcel.Visible = false
> objExcel.DisplayAlerts = FALSE
> objExcel.ScreenUpdating = False
> Const xlFormulas = -4123
> Const xlPart = 2
> Const xlByRows = 1
> Const xlNext = 1
>
>
> Set objWorkbook = objExcel.Workbooks.Open(strPath, False, True)
> For Each objWorkSheet In objWorkBook.Sheets
> intFoundRow = -1
> objWorkSheet.Activate
>
> Set objCell = objWorkSheet.Cells(1, "A")
> Set objCell = objWorkSheet.Cells.Find(strSearchTerm, objCell, xlFormulas,
> xlPart, xlByRows, xlNext, boolMatchCase)
>
> If Not objCell Is Nothing Then
>
> If objCell.Row > intFoundRow Then
> strResults = strResults & VbCrLf & objWorkSheet.Name
> intFoundRow = objCell.Row
>
> Else
> Set objCell = Nothing
> End If
>
> End If
> Next
> objWorkBook.Close
> objExcel.ScreenUpdating = True
> objExcel.Quit
>
>
>
> If strResults <> "" Then
> Wscript.echo strSearchTerm & "Keyword was found on the following sheets:"
> &
> strResults
> Else
> Wscript.echo strSearchTerm & " Keyword was not found"
> End If
>
Have a look at the WriteLine method of the File System Object. You can see a
complete example in the downloadable help file script56.chm.


My System SpecsSystem Spec
Old 01-23-2009   #3 (permalink)
gimme_this_gimme_that


 
 

Re: Vbscript to Search Excel

My suggestion....

Write it all in VBA and keep the VBA in the Excel Workbook.

Use VBScript to start up Excel and to start off the VBA macro.

Only code in VBScript when Excel can't do the job.



My System SpecsSystem Spec
Old 01-25-2009   #4 (permalink)
Reventlov


 
 

Re: Vbscript to Search Excel

Il giorno Fri, 23 Jan 2009 18:34:34 -0800 (PST), "gimme_this_gimme_that@xxxxxx"
<gimme_this_gimme_that@xxxxxx> ha scritto:
Quote:

>My suggestion....
>
>Write it all in VBA and keep the VBA in the Excel Workbook.
>
>Use VBScript to start up Excel and to start off the VBA macro.
filePath = "c:\Test.xls"
Set oExcel = CreateObject("Excel.Application")
oExcel.Workbooks.Open(filepath)
oExcel.Run "macro1"
oExcel.ActiveWorkbook.Save
oExcel.ActiveWorkbook.Close
oExcel.Quit
Set oExcel = Nothing
--
Giovanni Cenati (Bergamo, Italy)
Write to "Reventlov" at katamail com
http://digilander.libero.it/Cenati (Esempi e programmi in VbScript)
--
My System SpecsSystem Spec
Old 01-27-2009   #5 (permalink)
Codeblack


 
 

Re: Vbscript to Search Excel

Thanks for your suggestion. But i cannot use VBA in this case as the files in
which i search for keywords is not constant and there are hundreds of files
which in need to search. Any inputs please.
My System SpecsSystem Spec
Old 01-27-2009   #6 (permalink)
Pegasus \(MVP\)


 
 

Re: Vbscript to Search Excel


"Codeblack" <Codeblack@xxxxxx> wrote in message
news:9576CC20-9D38-4F87-9374-2C4F4BA01F3C@xxxxxx
Quote:

> Thanks for your suggestion. But i cannot use VBA in this case as the files
> in
> which i search for keywords is not constant and there are hundreds of
> files
> which in need to search. Any inputs please.
Here is the example from the help file I mentioned. Which part are you
having problems with?
Function WriteLineToFile
Const ForReading = 1, ForWriting = 2
Dim fso, f
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile("c:\testfile.txt", ForWriting, True)
f.WriteLine "Hello world!"
f.WriteLine "VBScript is fun!"
Set f = fso.OpenTextFile("c:\testfile.txt", ForReading)
WriteLineToFile = f.ReadAll
End Function


My System SpecsSystem Spec
Old 01-27-2009   #7 (permalink)
Codeblack


 
 

Re: Vbscript to Search Excel


Thanks MVP. I know how to write the data to text file but i dont know how to
retrive/copy the data from the excel cell when a match is found.


My System SpecsSystem Spec
Old 01-27-2009   #8 (permalink)
Pegasus \(MVP\)


 
 

Re: Vbscript to Search Excel


"Codeblack" <Codeblack@xxxxxx> wrote in message
news:A42B1190-DAF5-4AA6-85ED-85849724896D@xxxxxx
Quote:

>
> Thanks MVP. I know how to write the data to text file but i dont know how
> to
> retrive/copy the data from the excel cell when a match is found.
>
Try this modification to your code. Note that I dropped one line!
objWorkSheet.Activate
Set objCell = objWorkSheet.Cells.Find(strSearchTerm, objWorkSheet.Cells(1,
"A"), xlFormulas, xlPart, xlByRows, xlNext, boolMatchCase)
x = objCell.value



My System SpecsSystem Spec
Old 01-27-2009   #9 (permalink)
Matthias Tacke


 
 

Re: Vbscript to Search Excel

Codeblack wrote:
Quote:

> Thanks MVP. I know how to write the data to text file but i dont know how to
> retrive/copy the data from the excel cell when a match is found.
>
>
IIUR all you have to do is to append the found Row number to strResults

strResults = strResults&VbCrLf&objWorkSheet.Name&" Row:"&objCell.Row

And you should write that variable to a file before exiting th script.

Oh, and use a simple searchstr for testing like strSearchTerm = "abc"

With this and a prepared test.xls I got:

Tabelle1 Row:3
Tabelle2 Row:4
Tabelle3 Row:6

--
HTH
Matthias
My System SpecsSystem Spec
Old 01-29-2009   #10 (permalink)
Codeblack


 
 

Re: Vbscript to Search Excel

Thanks a lot for your timely help. this is working now. But the script is
finding only one occurence of the stirng in the excel sheet. If there are
multiple occurence of the string in the same sheet, then it is not showing
the details.

For example : if the search string "abc" is in Sheet1: Row1 and also in Row
45, the output is showing only the Row1 and not showing Row 45.

Do you have any clue why this is happening.
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Exporting MsSQL table to Excel using vbScript? VB Script
VBscript Returns wrong value from Excel Cell VB Script
VBScript to search LDAP from Excel function? VB Script
VBscript applying borders in Excel VB Script
new to excel and vbscript - need help VB Script


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46