+ Reply to Thread
Results 1 to 12 of 12

Limit numbers and letters in a cell

  1. #1
    Registered User
    Join Date
    02-07-2016
    Location
    Malta
    MS-Off Ver
    Office 2013
    Posts
    27

    Red face Limit numbers and letters in a cell

    Dear all

    I would like a vba code that sets a rule in a specified column/rows. Example from A300 to A500

    The column must have 7 digits and a letter from A, G, F, L and M at end. Total 8. Example 1234567M or 7845123G

    Could you please help.

    Thanking you in anticipation

  2. #2
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,620

    Re: Limit numbers and letters in a cell

    In sheet code write event handler for Change event - for instance:
    Please Login or Register  to view this content.
    and in standard module function which will test if given string follows your pattern (exactly 7 digits and selected uppercase letters), for instance using Regular Expressions:
    Please Login or Register  to view this content.
    See sample file
    Attached Files Attached Files
    Best Regards,

    Kaper

  3. #3
    Registered User
    Join Date
    02-07-2016
    Location
    Malta
    MS-Off Ver
    Office 2013
    Posts
    27

    Re: Limit numbers and letters in a cell

    Quote Originally Posted by Kaper View Post
    In sheet code write event handler for Change event - for instance:
    Please Login or Register  to view this content.
    and in standard module function which will test if given string follows your pattern (exactly 7 digits and selected uppercase letters), for instance using Regular Expressions:
    Please Login or Register  to view this content.
    See sample file

    Thank you for your quick reply. I am getting error - sub or function not defined and line Private Sub Worksheet_Change(ByVal Target As Range) is being highlighted in yellow

    Please Login or Register  to view this content.

  4. #4
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,620

    Re: Limit numbers and letters in a cell

    This is Change event ahndler and shall be defined in worksheet code (one you access by right-click on worksheet tab and Display Code).

    Have you downloaded my sample attachment?

    PS. See also our Rule 12 https://www.excelforum.com/forum-rul...rum-rules.html - it is really not necessary to cite post directly above :-)

  5. #5
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,524

    Re: Limit numbers and letters in a cell

    Paste the code onto the sheet code module
    Please Login or Register  to view this content.

  6. #6
    Registered User
    Join Date
    02-07-2016
    Location
    Malta
    MS-Off Ver
    Office 2013
    Posts
    27

    Re: Limit numbers and letters in a cell

    Quote Originally Posted by Kaper View Post
    This is Change event ahndler and shall be defined in worksheet code (one you access by right-click on worksheet tab and Display Code).

    Have you downloaded my sample attachment?

    PS. See also our Rule 12 https://www.excelforum.com/forum-rul...rum-rules.html - it is really not necessary to cite post directly above :-)

    Yes I copied it and posted it in my file.

    I will check the link provided

  7. #7
    Registered User
    Join Date
    02-07-2016
    Location
    Malta
    MS-Off Ver
    Office 2013
    Posts
    27

    Re: Limit numbers and letters in a cell

    Quote Originally Posted by jindon View Post
    Paste the code onto the sheet code module
    Please Login or Register  to view this content.
    This vba code is working great.

    ....Is there a way to get an error message instead of it removing cell contents please?

  8. #8
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,524

    Re: Limit numbers and letters in a cell

    MsgBox and then what do you want to do?
    Please Login or Register  to view this content.

  9. #9
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,620

    Re: Limit numbers and letters in a cell

    Quote Originally Posted by roma83 View Post
    Yes I copied it and posted it in my file.
    Woulkd be nice if you post on the forum as attachment:

    Attach a sample workbook. Make sure there is just enough data to demonstrate your need. Include a BEFORE sheet and an AFTER sheet in the workbook if needed to show the process you're trying to complete or automate. Make sure your desired results are shown, mock them up manually if necessary.

    Remember to desensitize the data.

    Click on GO ADVANCED and then scroll down to Manage Attachments to open the upload window.

  10. #10
    Registered User
    Join Date
    02-07-2016
    Location
    Malta
    MS-Off Ver
    Office 2013
    Posts
    27

    Re: Limit numbers and letters in a cell

    Quote Originally Posted by jindon View Post
    MsgBox and then what do you want to do?
    Please Login or Register  to view this content.
    This is working as well. Many Many thanks.

    Sorry for this...I did not explain myself clearly as i am totally new to vba.

    Apart from the error I would also want it to remove the content after I click ok

    Again ...appolgies

  11. #11
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,524

    Re: Limit numbers and letters in a cell

    Then Change
    Please Login or Register  to view this content.
    to
    Please Login or Register  to view this content.

  12. #12
    Registered User
    Join Date
    02-07-2016
    Location
    Malta
    MS-Off Ver
    Office 2013
    Posts
    27

    Re: Limit numbers and letters in a cell

    Quote Originally Posted by Kaper View Post
    Woulkd be nice if you post on the forum as attachment:

    Attach a sample workbook. Make sure there is just enough data to demonstrate your need. Include a BEFORE sheet and an AFTER sheet in the workbook if needed to show the process you're trying to complete or automate. Make sure your desired results are shown, mock them up manually if necessary.

    Remember to desensitize the data.

    Click on GO ADVANCED and then scroll down to Manage Attachments to open the upload window.
    I am attaching an example
    Attached Files Attached Files
    Last edited by roma83; 02-15-2018 at 09:12 AM.

+ 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] count if x numbers present in cell with letters and numbers
    By augr in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 11-09-2016, 12:54 PM
  2. Replies: 8
    Last Post: 05-20-2015, 10:25 AM
  3. Replies: 11
    Last Post: 10-16-2013, 10:21 PM
  4. [SOLVED] Cell containing letters and numbers.
    By jeroenheki in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 11-05-2012, 07:38 AM
  5. Replies: 2
    Last Post: 12-01-2010, 02:29 PM
  6. Replies: 2
    Last Post: 05-10-2010, 03:17 PM
  7. HOW CAN I LIMIT QUTITY OF LETTERS IN A CELL?
    By YIZHENG in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 09-23-2005, 02:05 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