Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Check value against value above and below

Posted on 2004-04-22
16
212 Views
Last Modified: 2010-05-02
With the help of aelatik the below script was assembled.

Dim FSO, TEXTSTREAM, CSV_FILE, CSV_LINES, CSV_ITEMS, CSV_LINE
    Set FSO = CreateObject("Scripting.FileSystemObject")
    Set TEXTSTREAM = FSO.OpenTextFile("D:\pathtocsvfile\file.csv", 1, False, 0)
        CSV_FILE = TEXTSTREAM.ReadAll
    Set TEXTSTREAM = Nothing
    Set FSO = Nothing
        CSV_LINES = Split(CSV_FILE, vbCrLf)
       
        For Each CSV_LINE In CSV_LINES
            CSV_ITEMS = Split(CSV_LINE, ",")
            Dim X
            X = "<p>Dear " & CSV_ITEMS(2) " " & CSV_ITEMS(1) & vbCrLf
            X = X & "<BR><BR>Your <U>date here</U> web registration PIN is <b>F" &  CSV_ITEMS(3)  & vbCrLf
            X = X & "</B><BR><BR>Text text text<b>Text</B> text here <U>date underlined here</U> category." & vbCrLf
            X = X & "<BR><BR><BR>Thank You" & vbCrLf
            X = X & "<BR>Some More Text </P>" & vbCrLf
          Mail CSV_ITEMS(0), CSV_ITEMS(5), X ' Call the mailer
            wscript.echo "Mailed to " & CSV_ITEMS(5)
        Next

Function Mail(M_NAME, M_TO, M_BODY)
    Set Mail = CreateObject("CDO.Message")
    Set CON = CreateObject("CDO.Configuration")
        CON.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "192.168.1.1"
        CON.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
        CON.Fields("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
        CON.Fields("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60
        CON.Fields.Update
    Set Mail.Configuration = CON
        Mail.From = "Registration.Distribution@ourdomain.com"
        Mail.To = M_TO
        Mail.CC = ""
        Mail.Subject = "Spring 2004 final exam number & Fall Pre-Registration number"
        Mail.HTMLBody = M_BODY
        Mail.Send
    Set Mail = Nothing
    Set CON = Nothing
End Function

I need to take this one step farther.

I need the CSV_ITEMS(3) to be compared to the same value up one row and down one row, if either one is = then insert "T" after that value CSV_ITEMS(3), if not equal just insert CSV_ITEMS(3).

Thanks
CurtV
0
Comment
Question by:Curtv
16 Comments
 
LVL 19

Expert Comment

by:Shauli
ID: 10891441
      For Each CSV_LINE In CSV_LINES
            CSV_ITEMS = Split(CSV_LINE, ",")
            Dim X
            If CSV_ITEMS(3) = CSV_ITEMS(1) Or CSV_ITEMS(3) = CSV_ITEMS(2) Then CSV_ITEMS(3) = CSV_ITEMS(3) & "T"
            X = "<p>Dear " & CSV_ITEMS(2) " " & CSV_ITEMS(1) & vbCrLf
            X = X & "<BR><BR>Your <U>date here</U> web registration PIN is <b>F" &  CSV_ITEMS(3)  & vbCrLf
            X = X & "</B><BR><BR>Text text text<b>Text</B> text here <U>date underlined here</U> category." & vbCrLf
            X = X & "<BR><BR><BR>Thank You" & vbCrLf
            X = X & "<BR>Some More Text </P>" & vbCrLf
         Mail CSV_ITEMS(0), CSV_ITEMS(5), X ' Call the mailer
            wscript.echo "Mailed to " & CSV_ITEMS(5)
        Next
0
 
LVL 19

Expert Comment

by:Shauli
ID: 10891466
Correction:
If CSV_ITEMS(3) = CSV_ITEMS(2) Or CSV_ITEMS(3) = CSV_ITEMS(4) Then CSV_ITEMS(3) = CSV_ITEMS(3) & "T"

S

0
 
LVL 14

Expert Comment

by:aelatik
ID: 10891876
Curtv,

What is the exact goal you wish to accomplish with that check ? By the way, can you also post the CSV file your using ( with fake data ofcourse ).
Maybe there's a better way to do it.

If i'm not wrong you mean matching CSV_ITEMS(3) against the CSV_ITEM(3) from previous and next line. Is this correct ?
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

Author Comment

by:Curtv
ID: 10892097
Aelatik

Man your active in the this group. Glad to be apart.

Short answer yes, previous and next lines. Better terminology.

The data will be somewhere in the format of:
rank,PIN,ssn,firstname,lastname,emailaddress
1,232,555-55-5555,me,melast,me@ourdomain.com

I don't have the data yet, I'm guessing.

As you can see this is for rankings. I need the "T" (tied) to be inserted if the rank below (next row down in csv), or above (row up in csv) are the same. It seems tricky to do this with while staying on the same data line.

Shauli

I need to check above and below. I use the term "row" meaning each line of data.

Thanks
CurtV
0
 
LVL 9

Expert Comment

by:SoftEng007
ID: 10892390
Once you have executed the line:
CSV_LINES = Split(CSV_FILE, vbCrLf)

you now have an array of X lines.
instead of using for each
use ubound(CSV_LINES) to get the total array count.

use special check for item 0 and item ubound(CSV_LINES)
0 item has no before and upper item has no below :)
use a loop for i =1 to ubound(CSV_LINES)-1
pull three lines in at a time  
CSV_ABOVE = Split(CSV_LINE(i-1, ",")
CSV_ITEMS = Split(CSV_LINE(i, ",")
CSV_BELOW= Split(CSV_LINE(i+1, ",")

'do your stuff --


0
 
LVL 19

Expert Comment

by:Shauli
ID: 10892812
My mistake, try this:

Dim CSV_LastLine as String, CSV_NextLine() As String, c As Long
On Error Resume Next           'this is for the first and last lines
        For c = LBound(CSV_LINES) To UBound(CSV_LINES)
            CSV_ITEMS = Split(CSV_LINES(c), ",")
            If CSV_ITEMS(3) = CSV_LastLine Or CSV_ITEMS(3) = CSV_NextLine(3) Then CSV_ITEMS(3) = CSV_ITEMS(3)  & "T"
            Dim X
            X = "<p>Dear " & CSV_ITEMS(2) " " & CSV_ITEMS(1) & vbCrLf
            X = X & "<BR><BR>Your <U>date here</U> web registration PIN is <b>F" &  CSV_ITEMS(3)  & vbCrLf
            X = X & "</B><BR><BR>Text text text<b>Text</B> text here <U>date underlined here</U> category." & vbCrLf
            X = X & "<BR><BR><BR>Thank You" & vbCrLf
            X = X & "<BR>Some More Text </P>" & vbCrLf
         Mail CSV_ITEMS(0), CSV_ITEMS(5), X ' Call the mailer
            wscript.echo "Mailed to " & CSV_ITEMS(5)
             CSV_LastLine =  CSV_ITEMS(3)
             CSV_NextLine = Split(CSV_LINES(c+1)
        Next c


I haven't tested it. Just of the screen. So check carefully.

S
0
 
LVL 19

Expert Comment

by:Shauli
ID: 10892935
CSV_NextLine = Split(CSV_LINES(c+1))
                                                       ^
Forgot the parenthesis

S
0
 
LVL 19

Expert Comment

by:Shauli
ID: 10893722
Woh this line
 CSV_NextLine = Split(CSV_LINES(c+1), ",")

Sorry :(

S
0
 

Author Comment

by:Curtv
ID: 10895379
I am confused as to what part of the script Shauli's code is replacing?
0
 
LVL 19

Accepted Solution

by:
Shauli earned 250 total points
ID: 10895410
Replace this part:

        For Each CSV_LINE In CSV_LINES
            CSV_ITEMS = Split(CSV_LINE, ",")
            Dim X
            X = "<p>Dear " & CSV_ITEMS(2) " " & CSV_ITEMS(1) & vbCrLf
            X = X & "<BR><BR>Your <U>date here</U> web registration PIN is <b>F" &  CSV_ITEMS(3)  & vbCrLf
            X = X & "</B><BR><BR>Text text text<b>Text</B> text here <U>date underlined here</U> category." & vbCrLf
            X = X & "<BR><BR><BR>Thank You" & vbCrLf
            X = X & "<BR>Some More Text </P>" & vbCrLf
         Mail CSV_ITEMS(0), CSV_ITEMS(5), X ' Call the mailer
            wscript.echo "Mailed to " & CSV_ITEMS(5)
        Next

With this code:

Dim CSV_LastLine as String, CSV_NextLine() As String, c As Long
On Error Resume Next           'this is for the first and last lines
        For c = LBound(CSV_LINES) To UBound(CSV_LINES)
            CSV_ITEMS = Split(CSV_LINES(c), ",")
            If CSV_ITEMS(3) = CSV_LastLine Or CSV_ITEMS(3) = CSV_NextLine(3) Then CSV_ITEMS(3) = CSV_ITEMS(3)  & "T"
            Dim X
            X = "<p>Dear " & CSV_ITEMS(2) " " & CSV_ITEMS(1) & vbCrLf
            X = X & "<BR><BR>Your <U>date here</U> web registration PIN is <b>F" &  CSV_ITEMS(3)  & vbCrLf
            X = X & "</B><BR><BR>Text text text<b>Text</B> text here <U>date underlined here</U> category." & vbCrLf
            X = X & "<BR><BR><BR>Thank You" & vbCrLf
            X = X & "<BR>Some More Text </P>" & vbCrLf
         Mail CSV_ITEMS(0), CSV_ITEMS(5), X ' Call the mailer
            wscript.echo "Mailed to " & CSV_ITEMS(5)
             CSV_LastLine =  CSV_ITEMS(3)
             CSV_NextLine = Split(CSV_LINES(c+1), ",")
        Next c

S
0
 

Author Comment

by:Curtv
ID: 10895461
Wow fast response, unfortunately.

Line: 9
Char: 26
Error: Expected end of statement

Thanks
CurtV
0
 
LVL 19

Expert Comment

by:Shauli
ID: 10895564
if you mean this line:
Dim CSV_LastLine as String, CSV_NextLine() As String, c As Long

Then try to replace with this line:

Dim CSV_LastLine, CSV_NextLine, c



0
 

Author Comment

by:Curtv
ID: 10895624
Line: 24
Char: 14
Expected end of statement

At the end of your code:
Next c

Also I had to take out the " " of the below line
X = "<p>Dear " & CSV_ITEMS(2) " " & CSV_ITEMS(1) & vbCrLf
Is there another way I can insert a space where the " " are"

Again Thanks
0
 
LVL 19

Expert Comment

by:Shauli
ID: 10895640
Replace

Next c

with

Next


You missed the & before the " ", it should be:

X = "<p>Dear " & CSV_ITEMS(2) & " " & CSV_ITEMS(1) & vbCrLf

S


0
 

Author Comment

by:Curtv
ID: 10895821
Bingo

Thank you for your assistance.

CurtV
0
 
LVL 19

Expert Comment

by:Shauli
ID: 10898284
You are welcome :)

S

ps. I think you need to accept  an answer if you got your solution :)

0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to create a duplicate finder Application 9 122
Microsoft Access combo box help 2 49
using web browser with BING 40 130
Collapse and expand table in Word 2010 2 27
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

792 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question