+ Reply to Thread
Results 1 to 5 of 5

Validation with Macro to count characters

  1. #1
    Registered User
    Join Date
    06-05-2006
    Location
    Philippines
    Posts
    22

    Validation with Macro to count characters

    Hi Guys,

    Can you help me out.

    I want to come up with a validation on Cell A1. The total number of characters that will be entered should be exactly 11. If the number of the characters that I entered is not exactly 11 it will be great if there is a macro program that will show a message box that will prompt me how many characters am I off.

    any help is greatly appreciated.

  2. #2
    Forum Contributor colofnature's Avatar
    Join Date
    05-11-2006
    Location
    -
    MS-Off Ver
    -
    Posts
    301
    Try this in the Worksheet's code module:

    Private Sub Worksheet_Change(ByVal Target As Excel.Range)
    If Target.Address(True, True) = "$A$1" Then
    If Len(Target.Value) <> 11 And Target.Value <> Empty Then _
    MsgBox "You have entered " & Abs(11 - Len(Target.Value)) & " too " _
    & IIf(Len(Target.Value) > 11, "many", "few") & " characters"
    [a1].Select
    End If
    End Sub


    Col

  3. #3
    NickHK
    Guest

    Re: Validation with Macro to count characters

    Eladamri,
    You can use Data>Validation for this. No code necessary.
    Although it will not tell you how many characters you are off, only that it
    is NOT 11.

    If you really need the number chars off then:
    Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Range("D1")) Is Nothing Then
    If Len(Target.Text) <> 11 Then
    MsgBox "Not 11 chars: Length= " & Len(Target.Text) - 11
    Target.Select
    End If
    End If
    End Sub

    Obviously changing "D1" to the range in question.

    NickHK

    "Eladamri" <[email protected]> wrote in
    message news:[email protected]...
    >
    > Hi Guys,
    >
    > Can you help me out.
    >
    > I want to come up with a validation on Cell A1. The total number of
    > characters that will be entered should be exactly 11. If the number of
    > the characters that I entered is not exactly 11 it will be great if
    > there is a macro program that will show a message box that will prompt
    > me how many characters am I off.
    >
    > any help is greatly appreciated.
    >
    >
    > --
    > Eladamri
    > ------------------------------------------------------------------------
    > Eladamri's Profile:

    http://www.excelforum.com/member.php...o&userid=35099
    > View this thread: http://www.excelforum.com/showthread...hreadid=562783
    >




  4. #4
    Registered User
    Join Date
    06-05-2006
    Location
    Philippines
    Posts
    22
    Quote Originally Posted by colofnature
    Try this in the Worksheet's code module:

    Private Sub Worksheet_Change(ByVal Target As Excel.Range)
    If Target.Address(True, True) = "$A$1" Then
    If Len(Target.Value) <> 11 And Target.Value <> Empty Then _
    MsgBox "You have entered " & Abs(11 - Len(Target.Value)) & " too " _
    & IIf(Len(Target.Value) > 11, "many", "few") & " characters"
    [a1].Select
    End If
    End Sub


    Col
    Hi Colofnature,

    I received an error message saying that Object Required in
    If Target.Address(True, True) = "$A$1" Then
    would you know how to correct this?
    anyway im just using the visual basic editor in MS Excel.
    Last edited by Eladamri; 07-19-2006 at 07:50 AM.

  5. #5
    Forum Contributor colofnature's Avatar
    Join Date
    05-11-2006
    Location
    -
    MS-Off Ver
    -
    Posts
    301
    Er, no... I can't replicate the error so I'm not sure what's causing it. I'll try to work out what I'm doing wrong (or not doing wrong, if you see what I mean...) and get back to you.

    C

+ 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