+ Reply to Thread
Results 1 to 29 of 29

Only Allow 2 digit state code & force upper case

  1. #1
    Registered User
    Join Date
    07-05-2019
    Location
    USA
    MS-Off Ver
    2010
    Posts
    157

    Only Allow 2 digit state code & force upper case

    Hello,

    Is there vba to force column to 2 digit state code? If the user types lower case also force to 2 digit uppercase also.

    Tx would be TX
    tx would be TX
    Texas would be TX
    texas would be TX and so on

    Thank you
    Last edited by AliGW; 07-21-2019 at 04:30 AM. Reason: Title improved.

  2. #2
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,628

    Re: Only Allow 2 digit state code & force upper case

    The first thing that comes to mind iw why not use a data validation list for this?
    Ali


    Enthusiastic self-taught user of MS Excel who's always learning!
    Don't forget to say "thank you" in your thread to anyone who has offered you help.
    You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.

    Forum Rules (updated August 2023): please read them here.

  3. #3
    Registered User
    Join Date
    07-05-2019
    Location
    USA
    MS-Off Ver
    2010
    Posts
    157

    Re: Only Allow 2 digit state code & force upper case

    Thank you Ali I will look into that.

  4. #4
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,628

    Re: Only Allow 2 digit state code & force upper case

    Sample workbook attached. DV list in Sheet 2 B1 - little bit of VBA forces upper case:

    Please Login or Register  to view this content.
    Attached Files Attached Files
    Last edited by AliGW; 07-21-2019 at 04:43 AM.

  5. #5
    Registered User
    Join Date
    07-05-2019
    Location
    USA
    MS-Off Ver
    2010
    Posts
    157

    Re: Only Allow 2 digit state code & force upper case

    Thank you but I already have a worksheet change code. Can that be applied to it? My states are in column F if that matters.

    Please Login or Register  to view this content.

  6. #6
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,628

    Re: Only Allow 2 digit state code & force upper case

    Try adding these lines to the bottom of your code AFTER End If and BEFORE End Sub:

    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    07-05-2019
    Location
    USA
    MS-Off Ver
    2010
    Posts
    157

    Re: Only Allow 2 digit state code & force upper case

    I tried that but it still allows lowercase. I attached so you can see. Thanks for your help.

  8. #8
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,628

    Re: Only Allow 2 digit state code & force upper case

    Sorry - it has me stumped. Hopefully one of the VBA experts can spot what's wrong (I am just a learner myself).

  9. #9
    Registered User
    Join Date
    07-05-2019
    Location
    USA
    MS-Off Ver
    2010
    Posts
    157

    Re: Only Allow 2 digit state code & force upper case

    Okay thanks for trying.

  10. #10
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,628

    Re: Only Allow 2 digit state code & force upper case

    I did try several methods - none worked. I have a feeling that it might be because of the DV list - I shall be interested to know myself!

  11. #11
    Forum Expert Roel Jongman's Avatar
    Join Date
    03-28-2015
    Location
    Netherlands
    MS-Off Ver
    Office 365
    Posts
    1,483
    You cannot manipulate the whole columm F in 1 codeline..
    You will have to use "Target" for cellreference as target represents the cell that is just changed

    Please Login or Register  to view this content.

  12. #12
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,628

    Re: Only Allow 2 digit state code & force upper case

    Tried that - it doesn't work in this workbook. Nothing changes after text is entered into a cell in column F.

  13. #13
    Forum Expert Roel Jongman's Avatar
    Join Date
    03-28-2015
    Location
    Netherlands
    MS-Off Ver
    Office 365
    Posts
    1,483
    Big chance that is because the macro freezed before and enableEvents is still in false state and then the worksheet_change code is not run..

    That lead to many frustrations for me testing codes

    Try my sheet..

    To fix in own sheet.. open immedeate window and copy the enable=true line in there and press eenter

    Or type "?application.enableevents" and enter. To verify the state is true
    Attached Files Attached Files
    Last edited by Roel Jongman; 07-21-2019 at 06:34 AM.

  14. #14
    Registered User
    Join Date
    07-05-2019
    Location
    USA
    MS-Off Ver
    2010
    Posts
    157

    Re: Only Allow 2 digit state code & force upper case

    Roel,

    I tried your workbook but it doesn't force to uppercase.

  15. #15
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,411

    Re: Only Allow 2 digit state code & force upper case

    This works for me in your sample workbook, having adapted the code Ali provide and Roel fixed.

    Replace your existing code with this

    Please Login or Register  to view this content.

    And, before you try to test it, enter this into the Immediate Window and press Enter

    Please Login or Register  to view this content.
    Last edited by TMS; 07-21-2019 at 06:56 AM.
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  16. #16
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,411

    Re: Only Allow 2 digit state code & force upper case

    Just to be clear, Roel's sample workbook works as it should (and as you wish).

    I'm guessing that event handling is disabled. Use the suggested fix in my last post.

  17. #17
    Registered User
    Join Date
    07-05-2019
    Location
    USA
    MS-Off Ver
    2010
    Posts
    157

    Re: Only Allow 2 digit state code & force upper case

    It isn't working for me. Also the auto number isn't working in column A either sorry.

  18. #18
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,411

    Re: Only Allow 2 digit state code & force upper case

    And, again, it is working for me.

    Maybe try closing Excel down and restarting it.

    I can see no reason why the various codes don't work for you.

    The Undo in column A works; entry in column B generates a new code: row number + 100, and the Upper Case is working.

    Can't fix it from this end 'cos it ain't broke here. Something is off in your environment.
    Attached Files Attached Files

  19. #19
    Registered User
    Join Date
    07-05-2019
    Location
    USA
    MS-Off Ver
    2010
    Posts
    157

    Re: Only Allow 2 digit state code & force upper case

    That does work and maybe closing and reopening is what it needed thank you.

    This is off topic but can the user tab from the last column cell to next row column B so a new entry can be made? (This should also generate a new ID)

  20. #20
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,411

    Re: Only Allow 2 digit state code & force upper case

    You're welcome. Thanks for the rep.

    This should have worked:

    And, before you try to test it, enter this into the Immediate Window and press Enter

    Please Login or Register  to view this content.


    The first block of code appears to preclude tabbing from the last cell in the last row. Not sure why but it is clearly being seen as a change in column A and your code Undoes that.

  21. #21
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,628

    Re: Only Allow 2 digit state code & force upper case

    Can anyone explain how event handling had become disabled? Is this something that is workbook specific? I ask because I haven't had any issues with upper case code working until this morning in the OP's workbook. Now, in your workbook, Trevor, it isn't working for me.

    How do I switch it back on permanently? And more to the point, how did it get switched off in the first place?

    And, lastly, where/what is the 'immediate window'?

    EDIT: Closing down Excel and restarting has fixed it - phew!
    Last edited by AliGW; 07-21-2019 at 08:24 AM.

  22. #22
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,411

    Re: Only Allow 2 digit state code & force upper case

    @Ali: Typically, event handling becomes disabled if the event handler does not terminate as expected. For example, if the event handling has been switched off and the code crashes or is forced to stop before it is reenabled. So, if you step through code to find an error/problem, and you have passed the enable=False, and you reset the code once you've identified and fixed the fault, voila, event handling disabled until you manually reenable it or you restart Excel.

    Last question next: the Immediate Window can be made visible using View | Immediate Window or pressing Ctrl-G. You close it by clicking the x in the top right corner. It normally appears below the code pane.

    When it is visible, you can use it to check the values of variables, see the output of Debug.Print statements and enter commands directly, including simple loops that can be completed on one line with the commands separated by colons.

    For example:
    Please Login or Register  to view this content.
    So, to switch event handling back on permanently, you can type:
    Please Login or Register  to view this content.
    in the Immediate Window and press Enter.

    Last resort, close and reload Excel.

    See: https://docs.microsoft.com/en-us/off...mediate-window

    Hope that covers all your questions.

  23. #23
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,628

    Re: Only Allow 2 digit state code & force upper case

    Thanks, Trevor - this is very illuminating. Thanks for taking the trouble.

  24. #24
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,411

    Re: Only Allow 2 digit state code & force upper case

    You're welcome. Thanks for the rep.

  25. #25
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,411

    Re: Only Allow 2 digit state code & force upper case

    Should have said, now you know how to fix it, try it.

    Comment out the EnableEvents=True in a simple change event handler and make a change. Make another change. Nothing happens. Go to the Immediate Window and reenable it.

  26. #26
    Forum Expert Roel Jongman's Avatar
    Join Date
    03-28-2015
    Location
    Netherlands
    MS-Off Ver
    Office 365
    Posts
    1,483

    Re: Only Allow 2 digit state code & force upper case

    Wow lot happend since my last reply..

    Great followup by TMS..

    I think the key sentence with regards to EnableEvents code in my last reply was

    "This has been very frustrating"
    It took me also quite a while to figure out why code stopped working.. and that it was the result of the code crashing and therefor not finishing to reset enableEvents to True to turn the eventmacros back on..

    So in General be carefull with the use of the application.Enable... type codes.
    When you use then always be sure you reset them to the original state at the end of your code and tell yourself 10 times a day that if the code crashes you have to reset them yourself before testing again..

    This is a more advanced coding statement where there is no user friendly warning or errormessage..

  27. #27
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,628

    Re: Only Allow 2 digit state code & force upper case

    Thanks for the extra information. It's something that a rookie coder like me would fall into without even realising it, so I for one really appreciate these thorough explanations.

    @Trevor - I'll give that a try tomorrow.

  28. #28
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,411

    Re: Only Allow 2 digit state code & force upper case

    Oh, Ali, how can you bear to wait?

  29. #29
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,411

    Re: Only Allow 2 digit state code & force upper case

    @Roel: you did in fact make all the same/correct suggestions. But, for whatever reason, it didn't appear to work for the OP. And, as Ali asked some specific questions, I just wanted to clarify what (I/you thought) happened and how to try to fix it without necessarily restarting Excel.

+ 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] City & State from Zip Code
    By brucemc777 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 08-24-2017, 02:51 PM
  2. Code to turn 4 digit year to 2 digit
    By phbryan in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-30-2015, 01:55 PM
  3. 50 States, Drop-Down List, select a state then produce the # for that state
    By Sir CHARLES in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 01-30-2015, 07:46 PM
  4. [SOLVED] Lookup State Name from State Code and and Display in column in COUNTIES worksheet.
    By Jim15 in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 06-11-2014, 02:41 PM
  5. [SOLVED] convert a 4 digit zip code to 5 digit zip code
    By maacmaac in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 04-25-2014, 02:36 PM
  6. [SOLVED] LOTTERY FILTER#4, Find if 1 Digit Sum of 2 Digit or 3 Digit, Single Cell w/ dash
    By david gonzalez in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 03-08-2014, 12:57 AM
  7. Separate Zip Code from City, State Zip Code
    By caunyd in forum Excel General
    Replies: 3
    Last Post: 08-04-2013, 05:14 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