+ Reply to Thread
Results 1 to 8 of 8

VBA not reading correct 2 strings of data

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    04-13-2017
    Location
    Buchares
    MS-Off Ver
    2016
    Posts
    117

    VBA not reading correct 2 strings of data

    Hey guys, I have a problem and i don't understand where is the issue.

    There are 2 simple texts : on sheet 1 in A1 cell : i1​86​59 and in sheet 2 in A1 cell : i18680 , and i want to simple compare them who's the bigger one by using the following code :

    If Left(Worksheets(1).Cells(1, 1), 1) = Left(Worksheets(2).Cells(1, 1), 1) Then ' This line checks if the first letter is the same
    
    number1= Right(Worksheets(1).Cells(1, 1), 5)
    number2= Right(Worksheets(2).Cells(1, 1), 5)
    
    If number1 - (number2+ 1) > 0 Then
    
    msgbox number1 & "is higher"
    
    endif
    
    endif

    The issue is that the first number in debugger is read as : "i1?86?59" . What is with the "?" sign ? Why is displayed on the 1st string and not on the other one?

    I tried anything i found on google , using INT , causes error, and VAL returns 1.

    **** The 1st text is received and formated from copying data from another excel.

  2. #2
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: VBA not reading correct 2 strings of data

    possibly there are some special characters in the value from sheet1? Just a guess...

    If I type your numbers into sheet1 and 2, it works fine, but if I COPY your number for the first value and paste it in Excel, I get the same results as you.
    Please help by:

    Marking threads as closed once your issue is resolved. How? The Thread Tools at the top
    Any reputation (*) points appreciated. Not just by me, but by all those helping, so if you found someone's input useful, please take a second to click the * at the bottom left to let them know

    There are 10 kinds of people in this world... those who understand binary, and those who don't.

  3. #3
    Forum Contributor
    Join Date
    04-13-2017
    Location
    Buchares
    MS-Off Ver
    2016
    Posts
    117

    Re: VBA not reading correct 2 strings of data

    how...I can make a script to fix it or is none ?

  4. #4
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: VBA not reading correct 2 strings of data

    Is your goal to run this on a large set of data? you could just fix the cell(s) manually if it is very little, or we can use code to fix it, but:
    Do you want code to go fix the data on the sheet?
    Or do you want the code to just ignore the (what I am guessing are) non-printable characters?

  5. #5
    Forum Contributor
    Join Date
    04-13-2017
    Location
    Buchares
    MS-Off Ver
    2016
    Posts
    117

    Re: VBA not reading correct 2 strings of data

    i dont know what is the difference between the 2 options : If the "?" sign disappears doesnt fix the issue to the remaining things : i1​86​59 vs i18680 => 1​86​59 > 18680?

  6. #6
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: VBA not reading correct 2 strings of data

    Ok well this code would work but one caveat: It will always ignore question maks (real ones just like these weird ones you have now) in any of the strings when performing the Right function. If that does not interfere with your use then it should work:

    Sub test()
    
    If Left(Worksheets(1).Cells(1, 1).Value, 1) = Left(Worksheets(2).Cells(1, 1).Value, 1) Then ' This line checks if the first letter is the same
        number1 = Right(CleanUp(Worksheets(1).Cells(1, 1).Value), 5)
        number2 = Right(CleanUp(Worksheets(2).Cells(1, 1).Value), 5)
        If number1 - (number2 + 1) > 0 Then MsgBox number1 & "is higher"
    End If
    
    End Sub
    
    Function CleanUp(myVal) As String
    
    Dim i As Long
    
    For i = 1 To Len(myVal)
        If Asc(Mid(myVal, i, 1)) <> 63 Then CleanUp = CleanUp & Mid(myVal, i, 1)
    Next i
    
    End Function
    I tried using the CLEAN worksheetfunction, and simple Replace in VBA but could not get either to work.
    Last edited by Arkadi; 05-02-2017 at 01:44 PM.

  7. #7
    Forum Contributor
    Join Date
    04-13-2017
    Location
    Buchares
    MS-Off Ver
    2016
    Posts
    117

    Re: VBA not reading correct 2 strings of data

    I tried to use the Clean option and copy-paste as values before but i didn't write them, sorry

    About the cleanup function, works like a charm ! (Saved to favorites) !

    Thanks, solved my problem !
    Alex

  8. #8
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: VBA not reading correct 2 strings of data

    Happy I could help out alxegoaga
    Please remember to mark the thread as solved? Thanks!

    P.S. Thanks for the rep

+ 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. [SOLVED] Month function not returning/reading correct month
    By D-smoke in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 06-03-2016, 07:04 AM
  2. [SOLVED] Problem with reading strings with ADODB connection containing quotes
    By Gerard66 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 02-17-2014, 10:49 AM
  3. Replies: 4
    Last Post: 03-21-2013, 11:35 AM
  4. Replies: 3
    Last Post: 06-07-2010, 10:53 PM
  5. Reading Numbers as Strings from .csv file
    By andrewlong in forum Excel General
    Replies: 3
    Last Post: 01-07-2010, 11:06 PM
  6. Reading in Number Strings with Dashes in SUMPRODUCT
    By rackun in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 04-23-2008, 01:50 AM
  7. Custom Function If..Else and reading text strings
    By Dan in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 01-11-2005, 02:06 PM

Tags for this Thread

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