Windows Vista Forums
Vista Forums Home Join Vista Forums Webcasts Windows 7 Forum Vista Tutorials Tags

Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > PowerShell

search string in Excel

Update your Vista Drivers Update Your Drivers Now!!
Closed Thread
 
Thread Tools Display Modes
Old 02-29-2008   #1 (permalink)
pjo
Guest


 

search string in Excel

Hi! I am trying to make a PowerShell script to search a string in Excel.

Excel looks like this
A B
-------------------
abcdv 7
zswd 2
dfergh 1
swqwer 8

1. check if $input is same as A1 then $output = 7 then exit
if not check next cell A2 if $input -eq A2 then $output = 2 then exit
if not check next cell A3 if $input -eq A3 then $output = 1 then exit

How can I write this after enabling Excel object like:
$a = New-Object -comobject Excel.Application

$a.Visible = $TRUE

$b = $a.Workbooks.Add()
$c = $b.Worksheets.Item(1)

Thanks in advance
pjo

My System SpecsSystem Spec
Old 02-29-2008   #2 (permalink)
Marco Shaw [MVP]
Guest


 

Re: search string in Excel

pjo wrote:
Quote:

> Hi! I am trying to make a PowerShell script to search a string in Excel.
>
> Excel looks like this
> A B
> -------------------
> abcdv 7
> zswd 2
> dfergh 1
> swqwer 8

Will it be this simple? I know in the past, I've had problems with
figuring out how to tell Excel to stop processing rows (when it hits the
first empty row). In other words, if you have a dynamic list of rows,
it becomes a bit more difficult.

Have you considered whether you want to save this data in a CSV format,
then you could easily import into PowerShell, and save you having to use
Excel's COM object?

Marco

--
Microsoft MVP - Windows PowerShell
http://www.microsoft.com/mvp

PowerGadgets MVP
http://www.powergadgets.com/mvp

Blog:
http://marcoshaw.blogspot.com
My System SpecsSystem Spec
Old 02-29-2008   #3 (permalink)
pjo
Guest


 

Re: search string in Excel

Thanks Marco,

Excel sheet will be dynamic.
So I should consider CSV.

pjo

"Marco Shaw [MVP]" wrote:
Quote:

> pjo wrote:
Quote:

> > Hi! I am trying to make a PowerShell script to search a string in Excel.
> >
> > Excel looks like this
> > A B
> > -------------------
> > abcdv 7
> > zswd 2
> > dfergh 1
> > swqwer 8
>
>
> Will it be this simple? I know in the past, I've had problems with
> figuring out how to tell Excel to stop processing rows (when it hits the
> first empty row). In other words, if you have a dynamic list of rows,
> it becomes a bit more difficult.
>
> Have you considered whether you want to save this data in a CSV format,
> then you could easily import into PowerShell, and save you having to use
> Excel's COM object?
>
> Marco
>
> --
> Microsoft MVP - Windows PowerShell
> http://www.microsoft.com/mvp
>
> PowerGadgets MVP
> http://www.powergadgets.com/mvp
>
> Blog:
> http://marcoshaw.blogspot.com
>
My System SpecsSystem Spec
Old 02-29-2008   #4 (permalink)
pjo
Guest


 

Re: search string in Excel

How to escape "," in CSV ?
The original Excel cell has "abscdf, gerfs, trf" and this is put as "abscdf,
gerfs, trf"
and this causes confusion to match
$temp = Import-Csv d:\work\test.csv| Where-Object {$_.name -eq "abscdf,
gerfs, trf"}

"pjo" wrote:
Quote:

> Thanks Marco,
>
> Excel sheet will be dynamic.
> So I should consider CSV.
>
> pjo
>
> "Marco Shaw [MVP]" wrote:
>
Quote:

> > pjo wrote:
Quote:

> > > Hi! I am trying to make a PowerShell script to search a string in Excel.
> > >
> > > Excel looks like this
> > > A B
> > > -------------------
> > > abcdv 7
> > > zswd 2
> > > dfergh 1
> > > swqwer 8
> >
> >
> > Will it be this simple? I know in the past, I've had problems with
> > figuring out how to tell Excel to stop processing rows (when it hits the
> > first empty row). In other words, if you have a dynamic list of rows,
> > it becomes a bit more difficult.
> >
> > Have you considered whether you want to save this data in a CSV format,
> > then you could easily import into PowerShell, and save you having to use
> > Excel's COM object?
> >
> > Marco
> >
> > --
> > Microsoft MVP - Windows PowerShell
> > http://www.microsoft.com/mvp
> >
> > PowerGadgets MVP
> > http://www.powergadgets.com/mvp
> >
> > Blog:
> > http://marcoshaw.blogspot.com
> >
My System SpecsSystem Spec
Old 02-29-2008   #5 (permalink)
pjo
Guest


 

Re: search string in Excel

$temp = Import-Csv d:\work\test.csv| Where-Object {$_.name -eq "abscdf`,
gerfs`, trf"}


I tried "`" but did not work.

"pjo" wrote:
Quote:

> How to escape "," in CSV ?
> The original Excel cell has "abscdf, gerfs, trf" and this is put as "abscdf,
> gerfs, trf"
> and this causes confusion to match
> $temp = Import-Csv d:\work\test.csv| Where-Object {$_.name -eq "abscdf,
> gerfs, trf"}
>
> "pjo" wrote:
>
Quote:

> > Thanks Marco,
> >
> > Excel sheet will be dynamic.
> > So I should consider CSV.
> >
> > pjo
> >
> > "Marco Shaw [MVP]" wrote:
> >
Quote:

> > > pjo wrote:
> > > > Hi! I am trying to make a PowerShell script to search a string in Excel.
> > > >
> > > > Excel looks like this
> > > > A B
> > > > -------------------
> > > > abcdv 7
> > > > zswd 2
> > > > dfergh 1
> > > > swqwer 8
> > >
> > >
> > > Will it be this simple? I know in the past, I've had problems with
> > > figuring out how to tell Excel to stop processing rows (when it hits the
> > > first empty row). In other words, if you have a dynamic list of rows,
> > > it becomes a bit more difficult.
> > >
> > > Have you considered whether you want to save this data in a CSV format,
> > > then you could easily import into PowerShell, and save you having to use
> > > Excel's COM object?
> > >
> > > Marco
> > >
> > > --
> > > Microsoft MVP - Windows PowerShell
> > > http://www.microsoft.com/mvp
> > >
> > > PowerGadgets MVP
> > > http://www.powergadgets.com/mvp
> > >
> > > Blog:
> > > http://marcoshaw.blogspot.com
> > >
My System SpecsSystem Spec
Old 02-29-2008   #6 (permalink)
pjo
Guest


 

Re: search string in Excel

This was solved by myself.
Thanks !

pjo

"pjo" wrote:
Quote:

> $temp = Import-Csv d:\work\test.csv| Where-Object {$_.name -eq "abscdf`,
> gerfs`, trf"}
>
>
> I tried "`" but did not work.
>
> "pjo" wrote:
>
Quote:

> > How to escape "," in CSV ?
> > The original Excel cell has "abscdf, gerfs, trf" and this is put as "abscdf,
> > gerfs, trf"
> > and this causes confusion to match
> > $temp = Import-Csv d:\work\test.csv| Where-Object {$_.name -eq "abscdf,
> > gerfs, trf"}
> >
> > "pjo" wrote:
> >
Quote:

> > > Thanks Marco,
> > >
> > > Excel sheet will be dynamic.
> > > So I should consider CSV.
> > >
> > > pjo
> > >
> > > "Marco Shaw [MVP]" wrote:
> > >
> > > > pjo wrote:
> > > > > Hi! I am trying to make a PowerShell script to search a string in Excel.
> > > > >
> > > > > Excel looks like this
> > > > > A B
> > > > > -------------------
> > > > > abcdv 7
> > > > > zswd 2
> > > > > dfergh 1
> > > > > swqwer 8
> > > >
> > > >
> > > > Will it be this simple? I know in the past, I've had problems with
> > > > figuring out how to tell Excel to stop processing rows (when it hits the
> > > > first empty row). In other words, if you have a dynamic list of rows,
> > > > it becomes a bit more difficult.
> > > >
> > > > Have you considered whether you want to save this data in a CSV format,
> > > > then you could easily import into PowerShell, and save you having to use
> > > > Excel's COM object?
> > > >
> > > > Marco
> > > >
> > > > --
> > > > Microsoft MVP - Windows PowerShell
> > > > http://www.microsoft.com/mvp
> > > >
> > > > PowerGadgets MVP
> > > > http://www.powergadgets.com/mvp
> > > >
> > > > Blog:
> > > > http://marcoshaw.blogspot.com
> > > >
My System SpecsSystem Spec
Old 03-01-2008   #7 (permalink)
Flowering Weeds
Guest


 

Re: search string in Excel


"pjo"
Quote:

>
> Excel sheet will be dynamic.
> So I should consider CSV.
>
And since others (besides the OP)
are reading these messages too,
one can also get this information (using
almost any Windows language) by using
the free IIS's Microsoft's data parser,
Log Parser 2.2 (with built-in Microsoft
ChartSpace chart maker) and chart the
output too!

Notice: IIS does not need to be running
or installed in order to use Log Parser
for either data parsing or chart making.

And using PowerShell:

# Remove any old test chart.
remove-item testChart.gif

$data = @("A,B",
"abcdv,7",
"zswd,2",
"dfergh,1"
"swqwer,8")

# Log Parser command line version.
$data | LogParser.exe "SELECT
A AS Name,
TO_INT(B) AS Number
FROM STDIN " -i:csv -statsff

# Log Parser command line ChartSpace version.
$data | LogParser.exe "SELECT
A AS Name,
TO_INT(B) AS Number
INTO testChart.gif
FROM STDIN " -i:csv -o:chart `
-chartType:ColumnClustered `
-statsff -viewn -valuesn

# Or if the data is in a file:

$data | out-file testData.csv -encoding ascii

# Did we create the CSV file? To see file,
# use Log Parser's datagrid.
LogParser.exe "SELECT text
FROM testData.csv " `
-i:textline -statsff -o:datagrid

# For general Log Parser CSV information.
LogParser.exe -h -i:csv testData.csv -headerRown

# Remove any old test chart.
remove-item testChart.gif

# Log Parser command line ChartSpace version.
LogParser.exe "SELECT
A AS Name,
TO_INT(B) AS Number
INTO testChart.gif
FROM testData.csv " -i:csv -o:chart `
-chartType:ColumnClustered `
-statsff -viewn -valuesn

Exit

Search the Internet for usage of:

Microsoft's Log Parser
that is fully script enabled
(from the IIS group)

and

Microsoft's ChartSpace Charting
as used within Log Parser, Excel
or even stand alone scripting usage
(via the Office group)

Just two other PowerShell
fully script enabled tools!


My System SpecsSystem Spec
Closed Thread

Thread Tools
Display Modes



Similar Threads
Thread Thread Starter Forum Replies Last Post
search and replace string Ron PowerShell 1 06-16-2008 09:29 AM
Can Select-String search in-use files? Chris Harris PowerShell 0 05-01-2008 11:00 AM
How to search and replace text in a string Jason Murray PowerShell 8 01-07-2008 04:29 AM
Re: get-eventlog search string problem Brandon Shell [MVP] PowerShell 1 12-03-2007 05:17 PM
Search for string in CSV and delete line if string found in line sherlock PowerShell 2 07-02-2007 03:42 PM


Vistax64.com 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 2005-2008

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 47 48 49 50 51