+ Reply to Thread
Results 1 to 21 of 21

Data Validation and Conditional Formatting using VBA

  1. #1
    Forum Contributor
    Join Date
    09-07-2009
    Location
    Hemet, CA
    MS-Off Ver
    Office 2007 Professional
    Posts
    111

    Cool Data Validation and Conditional Formatting using VBA

    I am using Data Validation for my drop down list, using “List” and “source” =$A$132:$A$159 along with Conditional Formatting that changes different fill colors, depending on selection. Is there a way to use VBA with a command button that when selected would give the user a dialog box so that they could enter additional comment that would be added to existing list and would also be able to select a fill color for that new selection.

    Below I have a recorded Marco to give an idea of what I would like to achieve, but of course using a input box of some sort to make selections of text and color
    Also I'm using Excel 2007
    Please Login or Register  to view this content.
    Last edited by ABabeNChrist; 01-21-2010 at 08:28 PM.

  2. #2
    Forum Contributor
    Join Date
    09-07-2009
    Location
    Hemet, CA
    MS-Off Ver
    Office 2007 Professional
    Posts
    111

    Re: Data Validation and Conditional Formatting using VBA

    Does anyone have any suggestion
    it doesnt have to use conditional forrmatting, but I do need it to enter new text by added to existing Data Validation list
    Last edited by ABabeNChrist; 01-18-2010 at 11:05 AM.

  3. #3
    Forum Expert Palmetto's Avatar
    Join Date
    04-04-2007
    Location
    South Eastern, USA
    MS-Off Ver
    XP, 2007, 2010
    Posts
    3,978

    Re: Data Validation and Conditional Formatting using VBA

    Does anyone have any suggestion
    It doesn't have to use conditional formatting, but I do need it to enter new text by added to existing Data Validation list
    One possible approach:
    Use either the Find Method or Countif function to determine if the new entry is in the current list of items. If the new entry does not exist, write it to the list. This action needs to occur the worksheet_Change event with the code targeted to the specific data validation cell(s).

    Your Data validation list should be based on a dynamic named range so that it can automatically accommodate new entries.
    Palmetto

    Do you know . . . ?

    You can leave feedback and add to the reputation of all who contributed a helpful response to your solution by clicking the star icon located at the left in one of their post in this thread.

  4. #4
    Forum Contributor
    Join Date
    09-07-2009
    Location
    Hemet, CA
    MS-Off Ver
    Office 2007 Professional
    Posts
    111

    Re: Data Validation and Conditional Formatting using VBA

    Hi Palmetto
    Thank you for your reply, I looked over your suggestions, I'm not sure how that will work.
    I am using a name range list and I have the name range selected up to 100 cells, but only use 25 at the present moment. When you view the drop down it will only show the 25 from the name list and a couple empty spots below last selection, not the full 100. Is it possible to just open a dialog that could add new entry that would enter at first empty cell on name range list? It’s just a thought

  5. #5
    Forum Expert Palmetto's Avatar
    Join Date
    04-04-2007
    Location
    South Eastern, USA
    MS-Off Ver
    XP, 2007, 2010
    Posts
    3,978

    Re: Data Validation and Conditional Formatting using VBA

    The approach I suggested will work. See the attached example.

    In the sample workbook . . .
    A dynamic named range was created for the validation list.
    Data Validation, list option, Error Alert set to Inform

    This code will check if the new entry is already in the list, if not then it will add it.
    Please Login or Register  to view this content.

  6. #6
    Forum Contributor
    Join Date
    09-07-2009
    Location
    Hemet, CA
    MS-Off Ver
    Office 2007 Professional
    Posts
    111

    Re: Data Validation and Conditional Formatting using VBA

    Hi Palmetto
    That is really cool, I like the way that it populates.
    is it possible that and entry could be removed if no longer needed, using somewhat the same method

  7. #7
    Forum Contributor
    Join Date
    09-07-2009
    Location
    Hemet, CA
    MS-Off Ver
    Office 2007 Professional
    Posts
    111

    Re: Data Validation and Conditional Formatting using VBA

    Hi Palmetto
    Or is it possible to enter new text using a right click event this way I could still enter additional text within the cell using the left double click event that will not save to name list, because this is sometimes needed to add a simple comment to existing populated text. And when I want text to be added to name list I would just right click and then enter new text. What do you think is this possible?

  8. #8
    Forum Expert Palmetto's Avatar
    Join Date
    04-04-2007
    Location
    South Eastern, USA
    MS-Off Ver
    XP, 2007, 2010
    Posts
    3,978

    Re: Data Validation and Conditional Formatting using VBA

    You cannot use the same approach for deleting items from the list. See attached workbook for example.

    There is a button to show a form with listbox of validation list items.
    Multiple selections in the lixt box may be made so as to delete several items at once.

    Note: a slight change was made to the dynamic named range for the validation list. It was given a header and this revised formula (below). Purpose: to prevent Excel from corrupting the named range formula when the top list item (anchor cell) is deleted. This new formula retains one cell in the dynamic named range, but the header cell is excluded from the list of items.
    =OFFSET(Sheet1!$L$1,1,0,COUNTA(Sheet1!$L:$L)-1,1)

    Here is the code assigned to the button:
    Please Login or Register  to view this content.
    Last edited by Palmetto; 01-19-2010 at 11:53 AM.

  9. #9
    Forum Contributor
    Join Date
    09-07-2009
    Location
    Hemet, CA
    MS-Off Ver
    Office 2007 Professional
    Posts
    111

    Re: Data Validation and Conditional Formatting using VBA

    Hi Palmetto
    See attached workbook for example.
    I believe this is the same workbook as before

  10. #10
    Forum Expert Palmetto's Avatar
    Join Date
    04-04-2007
    Location
    South Eastern, USA
    MS-Off Ver
    XP, 2007, 2010
    Posts
    3,978

    Re: Data Validation and Conditional Formatting using VBA

    Or is it possible to enter new text using a right click event this way I could still enter additional text within the cell using the left double click event that will not save to name list, because this is sometimes needed to add a simple comment to existing populated text. And when I want text to be added to name list I would just right click and then enter new text. What do you think is this possible?
    It is possible, though not recommended as it is, IMO, inefficient. Consider . . .
    In order to use the BeforeRightClick event, after making an entry into the cell you with have to
    press the tab or enter key to exit edit mode, which takes you out of the cell, or
    Click the accept button in the formula bar, requiring an extra mouse move/click

    That being said, if you want to proceed, then delete the code from the worksheet change event and use this code, which, as before, goes into the sheet module.
    Please Login or Register  to view this content.

    I believe this is the same workbook as before
    It contains amended code, which I showed in my last post. However, just to make certain, I removed the attachment and uploaded it again.

  11. #11
    Forum Contributor
    Join Date
    09-07-2009
    Location
    Hemet, CA
    MS-Off Ver
    Office 2007 Professional
    Posts
    111

    Re: Data Validation and Conditional Formatting using VBA

    Hi Palmetto
    I thank you so very much for your time and wisdom. I shall play around with what you have given me and see how it works in my applacation, I will update my results or possible questions. Also I can not see your attached workbook sample? maybe from my end.
    Again thank you
    Last edited by ABabeNChrist; 01-19-2010 at 12:28 PM.

  12. #12
    Forum Expert Palmetto's Avatar
    Join Date
    04-04-2007
    Location
    South Eastern, USA
    MS-Off Ver
    XP, 2007, 2010
    Posts
    3,978

    Re: Data Validation and Conditional Formatting using VBA

    Also I can not see your attached workbook sample? maybe from my end.
    I did not upload a workbook for the last post regarding the use of the BeforeRigthClick event. Simply replace the code as I explained.

  13. #13
    Forum Contributor
    Join Date
    09-07-2009
    Location
    Hemet, CA
    MS-Off Ver
    Office 2007 Professional
    Posts
    111

    Re: Data Validation and Conditional Formatting using VBA

    Hi Palmetto
    Upon further review I noticed there is room for accidental right click input error. I search around trying to get some new ideas on how to approach a method that may work. I was wondering if an input box could be used. Using a double click to open input box,

    Please Login or Register  to view this content.
    On the input box a couple buttons one to save to list and other button to only insert comment into cell, what are your thoughts on this approach.

  14. #14
    Forum Contributor
    Join Date
    09-07-2009
    Location
    Hemet, CA
    MS-Off Ver
    Office 2007 Professional
    Posts
    111

    Re: Data Validation and Conditional Formatting using VBA

    As to my last comment is this possible or am I reaching for straws, figure of speech

  15. #15
    Forum Expert Palmetto's Avatar
    Join Date
    04-04-2007
    Location
    South Eastern, USA
    MS-Off Ver
    XP, 2007, 2010
    Posts
    3,978

    Re: Data Validation and Conditional Formatting using VBA

    I see no reason not stick with the original data validation option/code I gave at the beginning. I wouldn't say you are "reaching for straws", but the goal should be, IMO, to use the simplest method possible that gets the job done.

    The input box approach, I think, is not the best, but it is possible
    Rather than chasing the wind and attempting to code for changing requirements, give some thorough thought to what you really want to achieve.
    Last edited by Palmetto; 01-21-2010 at 05:26 PM.

  16. #16
    Forum Contributor
    Join Date
    09-07-2009
    Location
    Hemet, CA
    MS-Off Ver
    Office 2007 Professional
    Posts
    111

    Re: Data Validation and Conditional Formatting using VBA

    Quote Originally Posted by Palmetto View Post
    I see no reason not stick with the original data validation option/code I gave at the beginning. I wouldn't say you are "reaching for straws", but the goal should be, IMO, to use the simplest method possible that gets the job done.

    The input box approach, I think, is not the best, but is it possible
    Rather than chasing the wind and attempting to code for changing requirements, give some thorough thought to what you really want to achieve.
    Hi Palmetto
    First off I thank you for your assistance.
    I was looking over Post #8 and I noticed that your attached file appears to be the same a post #5.
    I could be wrong. I was wanting to review your sample of this feature

  17. #17
    Forum Expert Palmetto's Avatar
    Join Date
    04-04-2007
    Location
    South Eastern, USA
    MS-Off Ver
    XP, 2007, 2010
    Posts
    3,978

    Re: Data Validation and Conditional Formatting using VBA

    The workbook in post #5 does not contain the additional code given in post #8 (which was a response to your additional request to delete items). The workbook in both of those posts includes the code given in post #5.

  18. #18
    Forum Contributor
    Join Date
    09-07-2009
    Location
    Hemet, CA
    MS-Off Ver
    Office 2007 Professional
    Posts
    111

    Re: Data Validation and Conditional Formatting using VBA

    Hi Palmetto
    I thank you for all your assistance and apologize for any mess confusion on my part.
    I'm new with this, and every day is a new learning experience.

  19. #19
    Forum Expert Palmetto's Avatar
    Join Date
    04-04-2007
    Location
    South Eastern, USA
    MS-Off Ver
    XP, 2007, 2010
    Posts
    3,978

    Re: Data Validation and Conditional Formatting using VBA

    Glad to help.

    Don't forget to leave feedback / add to the reputation of those who contributed a response you found helpful. See my signature for how to go about it.

  20. #20
    Registered User
    Join Date
    01-29-2010
    Location
    San Diego, California
    MS-Off Ver
    Excel 2003
    Posts
    3

    Re: Data Validation and Conditional Formatting using VBA

    I have used the following in my Leave and Payroll tracker found at http://bit.ly/2010FedLeave to accomplish conditional formatting for many conditions. (the link contains a download location of the working spreadsheet. I would appriciate any suggestions on improvements.)
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Set MyPage = Range("d5:q38")
    For Each Cell In MyPage
    If UCase(Cell.Value) Like "A*" Then
    Cell.Interior.ColorIndex = 35
    End If
    If UCase(Cell.Value) Like "P*" Then
    Cell.Interior.ColorIndex = 34
    End If
    If UCase(Cell.Value) Like "C*" Then
    Cell.Interior.ColorIndex = 6
    End If
    If UCase(Cell.Value) Like "U*" Then
    Cell.Interior.ColorIndex = 26
    End If
    If UCase(Cell.Value) Like "O*" Then
    Cell.Interior.ColorIndex = 36
    End If
    If UCase(Cell.Value) Like "H*" Then
    Cell.Interior.ColorIndex = 39
    End If
    If UCase(Cell.Value) Like "S*" Then
    Cell.Interior.ColorIndex = 22
    End If
    If Not (UCase(Cell.Value) Like "A*") And Not (UCase(Cell.Value) Like "P*") And Not (UCase(Cell.Value) Like "C*") And Not (UCase(Cell.Value) Like "U*") And Not (UCase(Cell.Value) Like "O*") And Not (UCase(Cell.Value) Like "H*") And Not (UCase(Cell.Value) Like "S*") Then
    Cell.Interior.ColorIndex = xlNone
    End If

    Next
    End Sub

  21. #21
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Data Validation and Conditional Formatting using VBA

    seakitnruth,

    a) unless you're offering some profound insight otherwise not covered in a SOLVED thread please do not post into it

    b) do not post questions into other people's threads (this has been mentioned to you in your other posts) - if your post is not directly related to the specifics of the question in the OP you should not be posting to it.

    c) use CODE tags for VBA not QUOTE tags


    On a final note if you have a file you wish to be critiqued...

    i) post in the Development Forum

    ii) attach the file to your posting such that it is located on the Board directly


    Please endeavour to first familiarise yourself with, and subsequently adhere to, the Forum Rules - all of which you agreed to by means of initial registration.

+ 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