+ Reply to Thread
Results 1 to 7 of 7

Limit Characters

  1. #1
    Registered User
    Join Date
    04-18-2012
    Location
    no
    MS-Off Ver
    Excel 2007
    Posts
    47

    Question Limit Characters

    Hello

    I'm trying to limit the characters in column A to 7 for example:

    12345678901242 will become 1234567 or Abcdefghijk will become Abcdefg

    I'd rather not use the =LEFT() function or validation because I'm importing the values from a text file and the user will be able to enter their values

    Can anyone help with a loop or function for this?

    Thanks in advance

  2. #2
    Registered User
    Join Date
    08-03-2005
    Location
    Harpers Ferry, WV
    Posts
    26

    Re: Limit Characters

    Are you importing by hand or via a macro? If it's the macro, then just fix it on the import step.

    If you want to have it done automagically either during the import AND during user input, then you'd need to put an OnChange even trigger but not sure that's what you're looking to do.

    If you can provide clearer requirements, it should be easy to do.
    Old Programmers Never Die ... They Just Lose Their Bits

  3. #3
    Forum Expert mrice's Avatar
    Join Date
    06-22-2004
    Location
    Surrey, England
    MS-Off Ver
    Excel 2013
    Posts
    4,967

    Re: Limit Characters

    Try pasting this into the appropriate Sheet tab (e.g. Sheet1) in the VBA editor (ALT F11).

    Please Login or Register  to view this content.
    Adjust the third line as appropriate to get the desired range.
    Martin

  4. #4
    Registered User
    Join Date
    04-18-2012
    Location
    no
    MS-Off Ver
    Excel 2007
    Posts
    47

    Re: Limit Characters

    Hi,

    Thank you both for your response.

    The code above works but as the sheet that I added the code to is created using a macro it wont work if I add a new sheet.
    Is it possible to automatically add this code when a new sheet is created so the user won't have to paste the code?

    Thanks in advance


  5. #5
    Forum Expert mrice's Avatar
    Join Date
    06-22-2004
    Location
    Surrey, England
    MS-Off Ver
    Excel 2013
    Posts
    4,967

    Re: Limit Characters

    Try pasting this into the ThisWorkbook tab

    Please Login or Register  to view this content.
    I've made some additional improvements to cope with entries into more than one cell.

  6. #6
    Registered User
    Join Date
    04-18-2012
    Location
    no
    MS-Off Ver
    Excel 2007
    Posts
    47

    Re: Limit Characters

    Hi,

    Thanks for your help, this works perfectly!
    Just have a question about the Sh object is it possible to define the sheet you want to use eg Sheet("name") for this type of Change event?

    Thanks

  7. #7
    Forum Expert mrice's Avatar
    Join Date
    06-22-2004
    Location
    Surrey, England
    MS-Off Ver
    Excel 2013
    Posts
    4,967

    Re: Limit Characters

    The Sh object is the object on which you have made the change. For instance, if you have edited a cell on A1 on sheet2, the event will fire with Sh being set to sheet2 and Target being set to the range A1 on that sheet. If you want to disable the functionality on certain sheets then you might consider a select statement based upon sheet name or index number.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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