+ Reply to Thread
Results 1 to 30 of 30

Limit character type and length in cell possible?

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    09-16-2009
    Location
    Glasgow, Scotland
    MS-Off Ver
    Excel XP, 2003, 2010
    Posts
    125

    Limit character type and length in cell possible?

    Hi Folks,

    I am wondering if it's possible to limit a cell's input so that it follows a certain format, the format being 1 letter, 2 numbers, 1 letter and 4 numbers.

    So, for example the cell value should look like this: 'A11A1111'.

    I already know you can limit the length of the input, which is halfway there, but something to limit the user to only input the value in the above format would be excellent.
    Pretty certain VBA is needed here if it can be done so just to let you know I'm not afraid!

    Cheers,
    Tony
    Last edited by Fidd$; 06-30-2010 at 12:01 PM. Reason: Question solved

  2. #2
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,984

    Re: Limit character type and length in cell possible?

    You could use a custom data validation formula:
    =AND(LEN(A1)=8,ISERROR(-LEFT(A1)),ISNUMBER(-MID(A1,2,2)),ISERROR(-MID(A1,4,1)),ISNUMBER(-MID(A1,5,4)))
    for example.
    Everyone who confuses correlation and causation ends up dead.

  3. #3
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606

    Re: Limit character type and length in cell possible?

    One approach, in the sheet module, will activate when any cell is changed.
    Private Sub Worksheet_Change(ByVal Target As Range)
    
    If Not Target Like "[A-Za-z]##[A-Za-z]####" Then
        MsgBox "Wrong"
        Application.Goto Target
        Application.EnableEvents = False
        Target.Clear
        Application.EnableEvents = True
    End If
    
    End Sub

  4. #4
    Forum Contributor
    Join Date
    09-16-2009
    Location
    Glasgow, Scotland
    MS-Off Ver
    Excel XP, 2003, 2010
    Posts
    125

    Re: Limit character type and length in cell possible?

    Hi Romper,

    That's excellent thanks for the quick response!
    What would be the VBA equivalent?

    Just thinking ahead here, the workbook could potentially hold up to 40 odd thousand rows off data and there will be other formulas and formatting in the book. Just trying to keep the file size as low as possible.

    Cheers,
    Tony

    (straight after posting this, StephenR came up with the above response!)


    STephenR,

    Thanks! How would I set the range? As in I only want the code to fire in certain columns and not in others?

    Cheers for the help guys!
    Last edited by Fidd$; 06-30-2010 at 06:30 AM.

  5. #5
    Valued Forum Contributor
    Join Date
    06-17-2009
    Location
    Chennai,India
    MS-Off Ver
    Excel 2003,excel 2007
    Posts
    678

    Re: Limit character type and length in cell possible?

    open a new workbook and expriment this

    rightclick sheet tab and click view code there you copy this evnet code

    Private Sub Worksheet_Change(ByVal Target As Range)
    Application.EnableEvents = False
    On Error GoTo eerr
    If Len(Target) = 8 And Not IsNumeric(Left(Target, 1)) _
    And IsNumeric(Mid(Target, 2, 2)) And Not IsNumeric(Mid(Target, 4, 1)) _
    And IsNumeric(Right(Target, 4)) Then
     MsgBox "valid"
     Else
      MsgBox "not valid"
      Target.Clear
     End If
    eerr:
    Application.EnableEvents = True
    End Sub
    the only problem when you enter something in any cell this event code runs. to restrict this only some range or column you can add a condition in the beginning.

    your comments please

  6. #6
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606

    Re: Limit character type and length in cell possible?

    Tony - if you add this line before the current If, it will only trigger when a cell in A1:A10 is changed.
    If Intersect(Target, Range("A1:A10")) Is Nothing Then Exit Sub

  7. #7
    Forum Contributor
    Join Date
    09-16-2009
    Location
    Glasgow, Scotland
    MS-Off Ver
    Excel XP, 2003, 2010
    Posts
    125

    Re: Limit character type and length in cell possible?

    Thanks for the amazing help all of you.

    Venkat, your code does exactly as what StephenR's does but added 'valid' messagebox (which isn't needed really, it's either going to be not valid or valid) and Stpehen's code is a bit more compact. Thanks a lot for the help though, it should come in handy some other time!

    StephenR, cheers for the extra line there, I was looking at previous workbooks I've created that had that exact line in it but again, you're quicker! I knew it had something to do with the 'Intersect' bit but wasn't sure what the rest of it should look like, I am still very much a VBA nubbin

    Thanks a lot for all the help and hints everyone!

    Tony

  8. #8
    Forum Contributor
    Join Date
    09-16-2009
    Location
    Glasgow, Scotland
    MS-Off Ver
    Excel XP, 2003, 2010
    Posts
    125

    Re: Limit character type and length in cell possible?

    I think I may have been a tad hasty in tagging this 'Solved'

    I'm trying to have the above code accommodate another column with a different format e.g: '12A/12345678' but I can't seem to get it to work.

    I thought about setting the ranges with names and the respective format they should be in but then it stops everything from working

    As mentioned I am VBA nubbin but not scared to learn!

    Cheers,
    Tony

  9. #9
    Forum Contributor
    Join Date
    09-16-2009
    Location
    Glasgow, Scotland
    MS-Off Ver
    Excel XP, 2003, 2010
    Posts
    125

    Re: Limit character type and length in cell possible?

    Just checking to see if anyone has an answer to the above

    Thanks,

  10. #10
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,984

    Re: Limit character type and length in cell possible?

    Which columns are you monitoring for which criteria?

  11. #11
    Forum Contributor
    Join Date
    09-16-2009
    Location
    Glasgow, Scotland
    MS-Off Ver
    Excel XP, 2003, 2010
    Posts
    125

    Re: Limit character type and length in cell possible?

    Hi Romper,

    Ok, I probably didn't think about this enough before I posted the original question - too eager!
    Full explanation below:

    I've got 7 columns, A:I.

    Columns B and C are freeform, anything goes into these babies
    Column F is a date field (simple enough)
    Columns A,C,E and G are where all the action's taking place

    Columns A and G needs to be like 'THSF12345' where the 'THSF' part never changes (only difference in the 2 columns is that in G 'it's 'THFB12345'
    Column C needs to be like 'A11B1234' where both letter and numbers will change (answered originally in this thread)
    Column E needs to be like '11A/12345678' where both numbers and the letter will change and the important part is the '/', this doesn't change.

    I tried to do it on my own and of course getting nowhere!

    I appreciate that my original post did not encompass everything I wanted so apologies.....I wanted to give it a try on my own first before asking for the rest.

    Thanks again

  12. #12
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Limit character type and length in cell possible?

    Private Sub Worksheet_Change(ByVal Target As Range)
        If Not Target Like "##[A-Za-z]/######" Then
            Application.Goto Target
            Application.EnableEvents = False
            Target.Clear
            Application.EnableEvents = True
        End If
    End Sub

  13. #13
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606

    Re: Limit character type and length in cell possible?

    Scratch this. I can't read.
    Private Sub Worksheet_Change(ByVal Target As Range)
    
    If Intersect(Target, Range("A:A,C:C,E:E,G:G")) Is Nothing Then Exit Sub
    
    Select Case Target.Column
        Case 1: If Not UCase(Target) Like "THSF#####" Then GoTo error
        Case 3: If Not Target Like "[A-Za-z]##[A-Za-z]####" Then GoTo error
        Case 5: If Not Target Like "##[A-Za-z]/########" Then GoTo error
        Case 7: If Not UCase(Target) Like "THFB#####" Then GoTo error
    End Select
    
    Exit Sub
    
    error:
    MsgBox "Wrong"
    Application.Goto Target
    Application.EnableEvents = False
    Target.Clear
    Application.EnableEvents = True
    
    End Sub
    I think this covers everything.
    Last edited by StephenR; 06-30-2010 at 10:36 AM.

  14. #14
    Forum Contributor
    Join Date
    09-16-2009
    Location
    Glasgow, Scotland
    MS-Off Ver
    Excel XP, 2003, 2010
    Posts
    125

    Re: Limit character type and length in cell possible?

    Quote Originally Posted by StephenR View Post
    Scratch this. I can't read.
    Or is it I who cannot explain in plain English???

    SNB, what's the point in posting the exact same code as in previous posts? Thanks for the help anyway

  15. #15
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,984

    Re: Limit character type and length in cell possible?

    Quote Originally Posted by Fidd$ View Post
    SNB, what's the point in posting the exact same code as in previous posts?
    it's not actually the same...

  16. #16
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606

    Re: Limit character type and length in cell possible?

    Or is it I who cannot explain in plain English???
    No, it's me who can't read English (let alone write French).

  17. #17
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,984

    Re: Limit character type and length in cell possible?

    See if this does what you need:
    
    Private Sub Worksheet_Change(ByVal Target As Range)
        Dim strPattern As String
        Dim rngcell As Range
        If Target.Count > 1 Then Exit Sub
        If Not Intersect(Target, Range("A:G")) Is Nothing Then
            Select Case Target.Column
                Case 1 ' col A
                    strPattern = "THSF#####"
                Case 3 ' col C
                    strPattern = "[A-Za-z]##[A-Za-z]####"
                Case 5 ' col E
                    strPattern = "##[A-Za-z]/########"
                Case 7 ' col G
                    strPattern = "THFB#####"
                Case Else
            End Select
            If Not Target Like strPattern Then
                MsgBox "Invalid value            "
                Application.Goto Target
                Application.EnableEvents = False
                Target.Clear
                Application.EnableEvents = True
            End If
        End If
    
    End Sub

  18. #18
    Forum Contributor
    Join Date
    09-16-2009
    Location
    Glasgow, Scotland
    MS-Off Ver
    Excel XP, 2003, 2010
    Posts
    125

    Re: Limit character type and length in cell possible?

    Hi Romper,

    I pasted the code in and it works but it fires in columns B,D and F as well.
    I only want it to look at columns A, C, E, G with the criteria mentioned in my earlier post.

    Any suggestions?

    Thanks,
    Tony

  19. #19
    Forum Contributor
    Join Date
    09-16-2009
    Location
    Glasgow, Scotland
    MS-Off Ver
    Excel XP, 2003, 2010
    Posts
    125

    Re: Limit character type and length in cell possible?

    HAH! Nice spot Romper, ok so it's not EXACTLY the same.....my mistake....missed out the messagebox bit
    But, otherwise....c'mon?

    StephenR, I only know "Stewardess!!! Quest qui ces't??!!" (sorry, a Lee Evans thing lol)

  20. #20
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,984

    Re: Limit character type and length in cell possible?

    Quote Originally Posted by Fidd$ View Post
    HAH! Nice spot Romper, ok so it's not EXACTLY the same.....my mistake....missed out the messagebox bit
    But, otherwise....c'mon?
    the pattern is different.

    Had a numpty moment with earlier code (always a risk with hair code). Change the Intersect line to:
        If Not Intersect(Target, Range("A:A,C:C,E:E,G:G")) Is Nothing Then

  21. #21
    Forum Contributor
    Join Date
    09-16-2009
    Location
    Glasgow, Scotland
    MS-Off Ver
    Excel XP, 2003, 2010
    Posts
    125

    Re: Limit character type and length in cell possible?

    YES!!!!!!!!!!!!!!!!!!!!!!
    Cool, thanks for all the help guys.

    Just a few points:
    Romper, what the hell is "hair code"? (some VBA term I'm not familiar with?)
    SNB, profound apologies

    Cheers,
    Tony

  22. #22
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,984

    Re: Limit character type and length in cell possible?

    "Hair code" is code that I make up off the top of my head.
    It's closely related to "air code" which I pull out of thin air.
    Both just mean that I have simply typed the code into a post without testing (which I do too often, I'm afraid).

  23. #23
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Limit character type and length in cell possible?

    Just for polishing:
    Private Sub Worksheet_Change(ByVal Target As Range)
        If Target.Count > 1 Then Exit Sub
        If Not Intersect(Target, Range("A:G")) Is Nothing Then
            If Target.Column Mod 2 = 0 Then Exit Sub
            If Not Target Like Split("THSF#####|[A-Za-z]##[A-Za-z]####|##[A-Za-z]/########|THFB#####", "|")(Target.Column \ 2) Then
                MsgBox "Invalid value"
                Application.Goto Target
                Application.EnableEvents = False
                Target.Clear
                Application.EnableEvents = True
            End If
        End If
    End Sub
    PS. This is definitely not exactly the same code (although the performance is)

  24. #24
    Forum Contributor
    Join Date
    09-16-2009
    Location
    Glasgow, Scotland
    MS-Off Ver
    Excel XP, 2003, 2010
    Posts
    125

    Re: Limit character type and length in cell possible?

    Well, the fact you can pull any code from hair or air and it works without testing is just showing off really
    Seriously though, appreciate the time spent on this and hope to one day be able to 'air code' as well as 'air guitar'!

    SNB, cheers for the 'polished' version. Will try that too. And yes, it's definitely not exactly the same

    Tony
    Last edited by Fidd$; 06-30-2010 at 11:27 AM.

  25. #25
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606

    Re: Limit character type and length in cell possible?

    snb: very neat.

  26. #26
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,984

    Re: Limit character type and length in cell possible?

    I think we'll agree to disagree on what 'polishing' is...

  27. #27
    Forum Contributor
    Join Date
    09-16-2009
    Location
    Glasgow, Scotland
    MS-Off Ver
    Excel XP, 2003, 2010
    Posts
    125

    Re: Limit character type and length in cell possible?

    SNB, where in your code does it state on what columns to run the code?
    I could see where it was going wrong in Romper's version before the change in the range was put in but not too sure where in yours it differentiates what columns to look at?

    Please excuse the questions, I just want to understand what each part does so that it will help me get a grip on VBA.

  28. #28
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Limit character type and length in cell possible?

    It takes two steps to tango...

    First is the target in columns A...G ?
       If Not Intersect(Target, Range("A:G")) Is Nothing Then
    Second: is the columnumber odd or even ?
    If it's even then skip the remaining code.

       If Target.Column Mod 2 = 0 Then Exit Sub

  29. #29
    Forum Contributor
    Join Date
    09-16-2009
    Location
    Glasgow, Scotland
    MS-Off Ver
    Excel XP, 2003, 2010
    Posts
    125

    Re: Limit character type and length in cell possible?

    How clever! Didn't even think for a second there that the columns I needed to look at coincided with odd numbers!

    Well done m8 and cheers for the help.

    I think that it's time now to mark this thread solved lol

    Tony

  30. #30
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Limit character type and length in cell possible?

    Private Sub Worksheet_Change(ByVal Target As Range)
        If Target.Count > 1 Then Exit Sub
        If Target.Column<8 and  Target.Column Mod 2 = 1 Then
            If Not Target Like Split("THSF#####|[A-Za-z]##[A-Za-z]####|##[A-Za-z]/########|THFB#####", "|")(Target.Column \ 2) Then
                MsgBox "Invalid value"
                Application.Goto Target
                Application.EnableEvents = False
                Target.Clear
                Application.EnableEvents = True
            End If
        End If
    End Sub

+ 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