+ Reply to Thread
Results 1 to 4 of 4

Validating the first letter with Vba Left function in a Textbox not giving desired result

  1. #1
    Registered User
    Join Date
    11-04-2017
    Location
    Upington, South Africa
    MS-Off Ver
    MS Office 2013
    Posts
    11

    Validating the first letter with Vba Left function in a Textbox not giving desired result

    Good day,
    Hope you are well.

    I am trying to validate the first letter with the Left function in a Textbox1, whether an user has added a "S" or an "A" in front of the value in a Textbox, before allowing submitting of the Textbox value. The problem is my validation code is not picking up whether there is a "S" or "A".

    I am not getting any errors, the Left function is not picking up whether there is or is'nt a "S" or "A" as the first letter.

    My code:
    Please Login or Register  to view this content.
    What I have tried to solve the issue:
    1] I have checked whether any Library References is missing. "There where none missing."
    2] I have tried different iterations of the Left function e.g. Application.WorksheetFunction.Left (does give an error), Left and VBA.Strings.Left
    3] I have referenced to Value rather than Text in the Textbox1 e.g. Textbox1.Value
    4] I have validated the boolean If iteration as If Not True or Not True rather than If False or False e.g.
    Please Login or Register  to view this content.
    Can someone please point me in the right direction or give advice on why the Left function is not functioning as it should?

    Regards,
    Last edited by Hmerman; 06-03-2018 at 09:46 AM.

  2. #2
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner VA USA
    MS-Off Ver
    MS 365 Family 64-bit 2505
    Posts
    27,626

    Re: Validating the first letter with Vba Left function in a Textbox not giving desired res

    The problem is your logic.

    It appears that you want your code to execute if the first letter is neither S nor A. That is not what your logic says. However, this error should cause your code to always run, and you are saying it never runs.

    Let's boil down you logic first.

    If Strings.Left(Textbox1.Text, 1) <> "S" Or Strings.Left(Textbox1.Text, 1) <> "A" Then

    If the first letter is S then your code will do this:

    If "S" <> "S" Or "S" <> "A" Then

    which evaluates to

    If False Or True Then

    which evaluates to

    If True Then

    Your If condition will always be True, no matter what the first letter is.

    Second, I have never seen nor used String before. I just use Left. Try this and report back.

    Please Login or Register  to view this content.
    If this does not work then you have some other problem that is not related to the code you showed us here. Please attach your file and describe the steps you take for this code to run.
    Jeff
    | | |·| |·| |·| |·| | |:| | |·| |·|
    Read the rules
    Use code tags to [code]enclose your code![/code]

  3. #3
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,529

    Re: Validating the first letter with Vba Left function in a Textbox not giving desired res

    You did not indicate what kind of textbox.
    I assume it is a textbox in a userform since you were using labels.

    If you use the TextBox1.exit event, this will ensure the entry right away.

    Please Login or Register  to view this content.

  4. #4
    Registered User
    Join Date
    11-04-2017
    Location
    Upington, South Africa
    MS-Off Ver
    MS Office 2013
    Posts
    11

    Re: Validating the first letter with Vba Left function in a Textbox not giving desired res

    Hello Jeff,
    You are correct, the problem was my logic. I forgot that a True And False = False.
    My code for completeness:
    Please Login or Register  to view this content.
    Thank you very much for your quick logic and answer.

    I will mark this question as Solved.

    Regards,
    Last edited by Hmerman; 06-03-2018 at 12:55 PM.

+ 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] IF(OR function keeps giving #VALUE! result
    By GHBrodbeck in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 02-27-2018, 08:42 PM
  2. LARGE function not giving desired results
    By jobdillon in forum Excel Formulas & Functions
    Replies: 8
    Last Post: 11-07-2016, 12:42 PM
  3. [SOLVED] SUMIFS Formula not giving desired result
    By lukemelville in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 10-12-2013, 09:15 AM
  4. [SOLVED] Typical Lookup function for getting desired result
    By paradise2sr in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 10-06-2013, 08:32 AM
  5. [SOLVED] Typical Lookup function for getting desired result
    By paradise2sr in forum Excel General
    Replies: 3
    Last Post: 10-06-2013, 08:32 AM
  6. [SOLVED] Using Offset function as the array in the PercentRank function is giving wrong result
    By Bobneil in forum Excel Formulas & Functions
    Replies: 12
    Last Post: 08-06-2013, 09:29 PM
  7. [SOLVED] vlookup left is not giving accurate result for relative text
    By Imran368 in forum Excel Formulas & Functions
    Replies: 13
    Last Post: 11-16-2012, 11:33 AM

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