Link to home
Start Free TrialLog in
Avatar of ckelsoe
ckelsoeFlag for United States of America

asked on

Finding Logical True with Asc function

HI,

I need some help with this logic.

Sub testfindengine()
Dim strSource As String
Dim x As Integer
strSource = "s3.4l 5speed"
For x = 1 To 3
MsgBox Asc(Mid(strSource, x, 1)) & " - " & Mid(strSource, x, 1)
    If Asc(Mid(strSource, x, 1)) > 47 And Asc(Mid(strSource, x, 1)) < 58 Or Asc(Mid(strSource, x, 1)) = 46 Then
       Debug.Print "Writing " & Asc(Mid(strSource, x, 1)) & " / String Source: " & strSource
    Else
    
    End If
    
Next x

End Sub

Open in new window


Basically, I only want the write to happen if the first three characters are between 0 and 9 or is a "-". I am not clear on why in the example code that starts outside of a number or a dash is TRUE on the if statement.

Help clarifying this is appreciated.

Charles
Avatar of SiddharthRout
SiddharthRout
Flag of India image

Your code can also be written as

Sub testfindengine()
    Dim strSource As String, x As Integer
    
    strSource = "s3.4l 5speed"
    
    For x = 1 To 3
        Debug.Print Asc(Mid(strSource, x, 1)) & " - " & Mid(strSource, x, 1)
        If Asc(Mid(strSource, x, 1)) > 45 And Asc(Mid(strSource, x, 1)) < 58 Then
           Debug.Print "Writing " & Asc(Mid(strSource, x, 1)) & " / String Source: " & strSource
        End If
    Next x
End Sub

Open in new window


These are the results that I am getting...

115 - s
51 - 3
Writing 51 / String Source: s3.4l 5speed
46 - .
Writing 46 / String Source: s3.4l 5speed

Isn't this what you expect?

Sid
Avatar of ckelsoe

ASKER

Thanks Sid

To many hours with out sleep!!

However, I need it not to write if any of the first three charactors of the string are not 0-9 or "-". It seems my logic is allowing it to write any of the values if it passes. So is there a way to exit the for loop in the event one of the test fails?
ASKER CERTIFIED SOLUTION
Avatar of SiddharthRout
SiddharthRout
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ckelsoe

ASKER

Thank you very much. Amazing how a person can over complicate the resolution of a problem - especially against a deadline and with lack of sleep!
Happens to me most of the time ... lolzzz

Sid
Avatar of Dale Fye
You could also have done:

Sub testfindengine()

    strSource = "s3.4l 5speed"

    if (strSource Like "-*") Or (strSource Like "[0-9][0-9][0-9]*") Then
           Debug.Print "Writing " & Asc(Mid(strSource, x, 1)) & " / String Source: " & strSource
    End If

End Sub
 
fyed... You have lost me with that code...

1) What is the value of "x" ?

2) And how would that just check if each character in the first three characters is a numeric or is a "."?

Sid
Sorry, just cut the OPs Debug.Print statement.

Change the debug.print to read whatever you want.

The point is that using ASC to test for values iteratively is much more tedious than using a Like evaluation.
I agree. but it still doesn't address the main issue...

2) And how would that just check if each character in the first three characters is a numeric or is a "."?
What do you mean, how would that check?

The IF statement:  

if (strSource Like "-*") Or (strSource Like "[0-9][0-9][0-9]*") Then

contains two parts.  The first part:

(strSource Like "-*")

checks to see whether the first character is a hyphen.  If it is, that portion evaluates to TRUE.  The second portion:  

(strSource Like "[0-9][0-9][0-9]*")

checks to see whether strSource contains at least three characters, and that the first three characters all fall in the range of zero to nine [0-9].  If the string being tested has numeric values as the first three characters, then the second portion of the IF clause will evaluate to TRUE.  If either of these portions evaluates to TRUE then the entire expression is TRUE.
fyed...

The OP want to check for a "." and not "-"

Sid
i.e if the first 3 characters are numbers or a dot/decimal

Sid
you need to reread the original question, and look at the original code.
fyed, I agree with you but later on I realized what the OP actually wanted and suggested the solution which is in ID: 34456967 which was accepted...

He meant "." and not "-"

Sid