I can write you something to use from within Excel but because you already have the csv files you can just process them from the command line. Copy the following code and save it into a file with the name "ProductReport.vbs". If your csv file is named myfile.csv you can use it as follows from the command line:
ProductReport widget1 myfile.csv
Output is to the console. If this isn't quite what you want let me know and I'll tweak it.
'fso is used for file operations
'arg is for accessing command line arguments
set fso = CreateObject("Scripting.FileSystemObject")
set arg = Wscript.Arguments
'if a file name is not specified show command syntax and exit
if arg.Count <> 2 then
wscript.Echo "ProductReport product csvfile"
wscript.Quit
end if
'the product you are looking for is the first argument.
'the csv file name is the second argument
product = arg(0)
csvfile = arg(1)
if not fso.FileExists(csvfile) then
wscript.Echo "could not find file",csvfile
wscript.Quit
end if
'open the csv file and process it one line at a time
'I am assuming that thformat of the file is strictly as you specified
'where all lines but the customer line start with a comma
set tso = fso.OpenTextFile(csvfile)
'add a comma to the start and end of the product so we don't accidentally
'confuse two products. for example, without the commas, "widget1" would
'match both "widget1" and "widget12"
match = "," & product & ","
'I know that the first line contains the column headers and will be
'taken as a customer but because the next line contains a new
'(valid) customer this won't matter.
wscript.Echo "scanning",csvfile,"for",product
wscript.Echo
do until tso.AtEndOfStream
line = tso.ReadLine()
if left(line,1) = "," then
if instr(1,line,match,vbTextCompare) > 0 then
wscript.Echo product,"->",cust
end if
else
parsed = Split(line,",")
cust = parsed(0) & " " & parsed(1)
end if
loop
tso.Close Reverend Jim
Posting Shark
Moderator
1,161 posts since Aug 2010
Reputation Points: 253
Solved Threads: 158