+ Reply to Thread
Results 1 to 11 of 11

Use VBA to Validate data entered or pasted into a range

  1. #1
    Registered User
    Join Date
    06-20-2013
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    26

    Use VBA to Validate data entered or pasted into a range

    Hi,

    I want to prevent users from entering data more than 20 characters in length into a named excel range.

    This can be achieved with normal Excel Data Valiadtion set-up but my problem is that when users paste data into the range, which will do often due to the high number of updates required, the validation is not applied. It looks like VBA code could be used but I am not an expert with that.

    Could somebody please provide me with a VBA code example?

    Thanks in advance.

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

    Re: Use VBA to Validate data entered or pasted into a range

    use an EVENTG CODE
    rightclick the sheet tab and click view code in the window that comes up copy this code


    Please Login or Register  to view this content.
    the relevant cell is A1 in the sheet. if some thling esle you have to change the line in the code
    If Target.Address <> "$A$1" Then Exit Sub

    now enter any entry or copy paste in THAT cell and see what happens.
    I am not an expert. better solutions may be available
    [email protected]

  3. #3
    Registered User
    Join Date
    06-20-2013
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    26

    Re: Use VBA to Validate data entered or pasted into a range

    Thanks this worked for cell A1 but how can I apply this to Column A? When I changed the code from If Target.Address <> "$A$1" Then Exit Sub to If Target.Address <> Range("$A:$A") Then Exit Sub and tested again it failed. Maybe I am selecting the range incorrectly.

  4. #4
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: Use VBA to Validate data entered or pasted into a range

    There are a few methods, you could do:
    Please Login or Register  to view this content.
    or if you have a particular range you want to apply it for you could use INTERSECT():
    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    06-20-2013
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    26

    Re: Use VBA to Validate data entered or pasted into a range

    Thanks again. However it is not exaclty what I need. The problem is that when I paste a number of cells into the range that contain some data with more than 20 characters no error message is displayed. Sorry to be so particular but this is exacly what is likely to happen. i.e. Many records pasted into a column with the possibility of only one cell data being too long (>20).

  6. #6
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: Use VBA to Validate data entered or pasted into a range

    You would need to loop through the different cells and test each one individually. To only adjust cells that have more than 20:
    Please Login or Register  to view this content.
    to clear everything you paste if one is more than 20:
    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    06-20-2013
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    26

    Re: Use VBA to Validate data entered or pasted into a range

    This is really excellent. Thank you so much.
    I would like two refinments if possible.
    1. To reference the Cell Address in the message if entry >20. example: You have entered more than 20 characters in cell A125
    2. To truncate that entry to 20 characters if entry >20

    Thanks again

  8. #8
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: Use VBA to Validate data entered or pasted into a range

    Please Login or Register  to view this content.

  9. #9
    Registered User
    Join Date
    06-20-2013
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    26

    Re: Use VBA to Validate data entered or pasted into a range

    Thanks, However this only work for me when I remove cL = Left(cL,20)

  10. #10
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: Use VBA to Validate data entered or pasted into a range

    Please Login or Register  to view this content.
    ?

  11. #11
    Registered User
    Join Date
    06-20-2013
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    26

    Re: Use VBA to Validate data entered or pasted into a range

    Awesome! Thanks

+ 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