+ Reply to Thread
Results 1 to 7 of 7

Limit characters in cells with VBA and require action

Hybrid View

  1. #1
    Registered User
    Join Date
    05-04-2010
    Location
    Atlanta
    MS-Off Ver
    Excel 2003
    Posts
    6

    Limit characters in cells with VBA and require action

    I have a column that I need to limit text to 40 characters in each cell. I can't use validation because data will be pasted in. I have the following code that almost works, but I need the message to keep coming up until the problem is fixed in any one cell. Right now, as soon as you enter, tab or click off the cell the message comes up, but you can click ok and then go on to other things without fixing the problem. I need them to fix the problem before moving on to anything else on the worksheet.

    Here is what I have so far:

    Private Sub Worksheet_Change(ByVal Target As Range)
        Dim cell    As Excel.Range
         
        If Not Intersect(Target, Columns("A")) Is Nothing Then
            For Each cell In Intersect(Target, Columns("A"))
                If Len(cell.Value) > 40 Then
                     cell.Select
                     MsgBox "Cell cannot contain over 40 character, please shorten description"
                End If
            Next cell
        End If
    End Sub
    Last edited by mfarus; 05-05-2010 at 02:48 PM. Reason: Solved

  2. #2
    Forum Contributor
    Join Date
    02-25-2010
    Location
    Boston
    MS-Off Ver
    Excel 2007
    Posts
    174

    Re: Limit characters in cells with VBA and require action

    Edit: Didn't see you couldnt use validation
    Last edited by DP978; 05-05-2010 at 09:35 AM.

  3. #3
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Limit characters in cells with VBA and require action

    This will prompt for the corrected description in a popup box, it will present the first 40 characters as a default so you could just press ENTER to truncate.
    Option Explicit
    
    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim cell As Range
    
        If Not Intersect(Target, Columns("A")) Is Nothing Then
            For Each cell In Intersect(Target, Columns("A"))
                If Len(cell.Value) > 40 Then
                     cell.Select
                     Do
                        cell.Value = Application.InputBox("This cell cannot contain over 40 character, please shorten description", _
                            "Shorten this description", Left(cell, 40), Type:=2)
                        If Len(cell) <= 40 Then Exit Do
                    Loop
                End If
            Next cell
        End If
    End Sub
    If you do not shorten it or enter another value still too long, it will loop until you do.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  4. #4
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Limit characters in cells with VBA and require action

    You might want to just have the cell value truncated and get a "beep" to indicate it happened.

    Option Explicit
    
    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim cell As Range
    
    If Not Intersect(Target, Columns("A")) Is Nothing Then
        For Each cell In Intersect(Target, Columns("A"))
            If Len(cell.Value) > 40 Then
                cell.Value = Left(cell, 40)
                Beep
            End If
        Next cell
    End If
    End Sub

  5. #5
    Registered User
    Join Date
    05-04-2010
    Location
    Atlanta
    MS-Off Ver
    Excel 2003
    Posts
    6

    Re: Limit characters in cells with VBA and require action

    I can't use your second example as the descriptions need to be modified, not just truncated. I had already tried this and ran into that problem.

    Your first example is close, but instead of truncating as explained above, could it just force them back to the cell to correct? Or show them the entire text in the dialoge box and let them modify?

    Also, if they just hit cancel if wiped out the text in the cell and put in "FALSE", which isn't good.

  6. #6
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Limit characters in cells with VBA and require action

    How about this:
    Option Explicit
    
    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim cell As Range, MyStr As String, Ans As String
    
    If Not Intersect(Target, Columns("A")) Is Nothing Then
        For Each cell In Intersect(Target, Columns("A"))
            If Len(cell.Value) > 40 Then
                 MyStr = cell
                 Do
                    Ans = Application.InputBox("This cell cannot contain over 40 character, please shorten description", _
                        "Shorten this description", MyStr, Type:=2)
                    If Ans = "False" Then
                        'Loop will restart
                    ElseIf Len(Ans) <= 40 Then
                        cell = Ans
                        Exit Do
                    Else
                        MyStr = Ans
                    End If
                Loop
            End If
        Next cell
    End If
    
    End Sub

  7. #7
    Registered User
    Join Date
    05-04-2010
    Location
    Atlanta
    MS-Off Ver
    Excel 2003
    Posts
    6

    Re: Limit characters in cells with VBA and require action

    That is perfect, you're Awesome. Thanks

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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