+ Reply to Thread
Results 1 to 5 of 5

Comparing strings not working

  1. #1
    Registered User
    Join Date
    12-25-2012
    Location
    Los Angeles
    MS-Off Ver
    Excel 2013
    Posts
    77

    Comparing strings not working

    I've hit a brick wall

    Reduced to the simplest terms, when I code If "04148" < "3" I get a true response. If 04148 < 3 gives me a false response.

    My actual code uses String variables that contain those values, and I do need to compare them as a/n strings.
    I am reading though the files in a folder, and I want to loop until while the file name in the folder is < a value in a variable. The directory lists the "3" first because it is < "04148" but a Do While, or If disagrees.

    How do I get this to work?

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Comparing strings not working

    Are '04148' and '3' meant to be represent the numbers 4148 and 3 respectively?

    If they are trying using Val.
    Please Login or Register  to view this content.
    If posting code please use code tags, see here.

  3. #3
    Valued Forum Contributor
    Join Date
    09-19-2008
    Location
    It varies ...
    MS-Off Ver
    Office365 - 64bit
    Posts
    862

    Re: Comparing strings not working

    Think of it this way: if you called a file "04148" and another one "3" and in file explorer you sorted by name, the 04148 would be above 3 because 0 < 3. It's a text sort.

    If you have numbers and text sorted in Excel, the numbers will come first like this:

    34
    555
    4356
    "04356"
    "34"
    "555"

    If you expect 04148 > 3 then you have to change them to numbers, which is the Val function Jeff suggested.

    HTH. MM.
    MatrixMan.
    --------------------------------------
    If this - or any - reply helps you, remember to say thanks by clicking on *Add Reputation.
    If your issue is now resolved, remember to mark as solved - click Thread Tools at top right of thread.

  4. #4
    Registered User
    Join Date
    12-25-2012
    Location
    Los Angeles
    MS-Off Ver
    Excel 2013
    Posts
    77

    Re: Comparing strings not working

    Yes, I see your point. As a/n values, it seem to left justify the contents and look at them as "3bbbb" and "04148". However, I still need them in the sequence as they are in the directory.

    When I'm done with the project, it may not be a problem because the real world file names are all in the format nnnnn-nnnn.csv. All being the same size. It's just that I'm doing this project for a friend and I'd want it to still work if he changes the file name format, which he probably won't.

  5. #5
    Registered User
    Join Date
    12-25-2012
    Location
    Los Angeles
    MS-Off Ver
    Excel 2013
    Posts
    77

    Re: Comparing strings not working

    I found a way around it. testfield = Format("3", "@@@@@") right justifies "3" in the variable testfield. That makes "3" < "04148" (tested). However after giving some more thought to what I described as the real world file in my last reply, I think I can count on the file name format being stable and just compare the values as they are.

    Thanks for the replies.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Comparing strings value
    By bryvba in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-21-2016, 07:03 AM
  2. [SOLVED] Comparing 2 Strings and output the difference
    By alugo004 in forum Excel Formulas & Functions
    Replies: 9
    Last Post: 11-25-2013, 11:52 AM
  3. [SOLVED] partially comparing strings vba
    By joetinger in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-19-2012, 08:54 PM
  4. Comparing text strings
    By gmalpani in forum Excel - New Users/Basics
    Replies: 3
    Last Post: 12-18-2011, 01:59 PM
  5. Comparing strings with conditions in 2 columns
    By jmul in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-14-2010, 05:41 PM
  6. Comparing Strings
    By dadio25 in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 03-30-2009, 11:13 AM
  7. Comparing slightly different strings
    By carg1 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-29-2005, 07:55 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1