+ Reply to Thread
Results 1 to 6 of 6

Setting validation with VBA

  1. #1
    Registered User
    Join Date
    03-20-2013
    Location
    United Kingdom
    MS-Off Ver
    Excel 2010
    Posts
    16

    Setting validation with VBA

    Hi All
    I have a routine that generates a complex excel file (the routine runs from an access application) using vba. A lot of validation is set - e.g.
    Please Login or Register  to view this content.
    I would like to amend the particular validation above so that only the characters Y or N are allowed - while still retaining the required text length of 7. Can this be done? I realise I could do this with some 'change event' code, but would much prefer to prevent invalid characters rather than just give a warning about them.
    Hope someone can help.
    Thanks in advance.
    Les

  2. #2
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Setting validation with VBA

    you would need to use a custom validation with a formula such as
    =and(len(C1)=7,LEN(SUBSTITUTE(SUBSTITUTE(UPPER(C1),"Y",""),"N",""))=0)

    because this requires relative addressing you have to select each cell as you apply the validation, or apply the validation to an entire column range at a time and select the first cell before applying the validation
    Josie

    if at first you don't succeed try doing it the way your wife told you to

  3. #3
    Registered User
    Join Date
    03-20-2013
    Location
    United Kingdom
    MS-Off Ver
    Excel 2010
    Posts
    16

    Re: Setting validation with VBA

    Hi JosephP
    Many thanks for your reply.
    I tried your formula in a blank workbook and it works exactly as required (as you knew it would ), but I'm having problems trying to use it in the vba. I have:
    Please Login or Register  to view this content.
    but this generates an error 1004 - Application defined or object defined error.
    The above works fine for lots of simpler validation that I have set, so I'm guessing that the problem is to do with the relative addressing
    Any ideas?
    Thanks again for your help.
    Les

  4. #4
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Setting validation with VBA

    I reckon that oughta be
    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    03-20-2013
    Location
    United Kingdom
    MS-Off Ver
    Excel 2010
    Posts
    16

    Re: Setting validation with VBA

    Hi JosephP
    Genius
    Getting those quotes right has always been a nightmare for me, so very many thanks.
    Les

  6. #6
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Setting validation with VBA

    you're welcome :-)

    please don't forget to mark the thread solved (click the 'thread tools' link at the top, then 'mark solved')

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Setting up a dependent data validation
    By dougdrex in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 06-10-2013, 12:10 PM
  2. Trouble setting validation reference to cells on another worksheet
    By daylward in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-21-2012, 11:53 AM
  3. Replies: 2
    Last Post: 02-01-2008, 04:30 PM
  4. Setting validation rule using code
    By stoney1977 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-14-2007, 07:47 PM
  5. Replies: 2
    Last Post: 05-04-2006, 02:40 PM

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