Solved

Check value against value above and below

Posted on 2004-04-22
16
204 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
Comment Utility
      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
Comment Utility
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
Comment Utility
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
 

Author Comment

by:Curtv
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
CSV_NextLine = Split(CSV_LINES(c+1))
                                                       ^
Forgot the parenthesis

S
0
 
LVL 19

Expert Comment

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

Sorry :(

S
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

Author Comment

by:Curtv
Comment Utility
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
Comment Utility
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
Comment Utility
Wow fast response, unfortunately.

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

Thanks
CurtV
0
 
LVL 19

Expert Comment

by:Shauli
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Bingo

Thank you for your assistance.

CurtV
0
 
LVL 19

Expert Comment

by:Shauli
Comment Utility
You are welcome :)

S

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

0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now