+ Reply to Thread
Results 1 to 10 of 10

Macro to Validate existing data.

  1. #1
    Registered User
    Join Date
    04-25-2011
    Location
    Noida, India
    MS-Off Ver
    Excel 2010
    Posts
    16

    Macro to Validate existing data.

    Hi,
    I need a macro to check values in a sheet.

    The values need to be consistent with the following rules:
    1. Names consisting of the following characters: A-Z, a-z, 0-9, # _ - [ ] @ only.
    2. The character _ cannot be used as the first or the last character of the ID
    3. Consecutive use of the character _ that is __ is not allowed
    4. The maximum size allowed is 21

    The error values should be highlighted as shown in attachment, and There should also be a log in one of the sheets that outputs a statement that " the cluster value for Utrancell ABC is in error".


    Thanks
    Attached Files Attached Files
    Last edited by cbhawsar; 05-16-2011 at 10:55 AM.

  2. #2
    Forum Contributor
    Join Date
    04-11-2011
    Location
    Columbus, Ohio
    MS-Off Ver
    Excel 2007
    Posts
    325

    Re: Macro to Validate existing data.

    I'll just say it is unlikely anyone is going to make the entire macro for you. You have to show some effort and when you get stuck ask questions.

    Start by finding out how to traverse a list.

    Please Login or Register  to view this content.
    If someone does do the macro for you consider yourself lucky.

  3. #3
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Macro to Validate existing data.

    Hello cbhawsar,

    This macro has been tested on the workbook you posted. I believe it to correct but you should double check it. The macro below has been added to the attached workbook and can be run by clicking a button on the "Input" sheet. The errors are listed on the "Output" sheet.
    Please Login or Register  to view this content.
    Attached Files Attached Files
    Last edited by Leith Ross; 05-15-2011 at 05:25 PM. Reason: Corrected typo
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Macro to Validate existing data.

    @ 111StepsAhead - Not so difficult when you know how.

  5. #5
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,629

    Re: Macro to Validate existing data.

    I spent most of the evening on this so I thought I'd post it anyway, despite Leith Ross having provided a solution some hours ago ;-)

    My biggest problem was the Regular Expression coding ... hence I have plagiarised this from Leith. Thanks.

    I'd also worked out a different test for the multiple underscores and I've replaced that with Leith's second RegExp test.

    The code is a little different, reflecting styles more than anything. One key point is that I tag the log entries onto the end which you may or may not want.



    Please Login or Register  to view this content.


    I hope you will find the alternative approach gives you some ideas.

    Regards
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  6. #6
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Macro to Validate existing data.

    Hello TMShucks,

    You may not have realized that the expression below does not allow the string to begin or end with an underscore.
    Please Login or Register  to view this content.

    The "OR" condition in Regular Expressions is denoted by the pipe character "|". Easy to miss in all the characters. The caret "^" outside the parenthesis marks the beginning of the string and the "$" marks the end of the string.

    In between, the pattern looks for any characters that do not match: A-Z, a-z, 0-9, # _ - [ ] @
    If the tested string returns a true value then there is a error.

  7. #7
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,629

    Re: Macro to Validate existing data.

    @Leith Ross: thank you for the clarification. That had escaped me ;-)

    I tend to look at the use of Regular Expressions with some awe. It's useful to have a practical example.

    Thanks again

  8. #8
    Registered User
    Join Date
    04-25-2011
    Location
    Noida, India
    MS-Off Ver
    Excel 2010
    Posts
    16

    Re: Macro to Validate existing data.

    Thanks Leith and TMShucks

    I was stuck on this for hours as I am a beginner to VBA.

    This forum is amazing

    @111StepsAhead
    Thanks for the algorithm. I am actually making a hobby project and I was asking for only a small demo code. my actual project involves validation of a lot of values. this would certainly help.

  9. #9
    Registered User
    Join Date
    04-25-2011
    Location
    Noida, India
    MS-Off Ver
    Excel 2010
    Posts
    16

    [Solved] Macro to Validate existing data.

    How do I mark this as solved?

  10. #10
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,629

    Re: Macro to Validate existing data.

    See my signature or the FAQ.

    Regards

+ 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