Ask a questionAsk a question
 

AnswerData validation

  • Wednesday, November 04, 2009 8:28 AMHuaMin Chen Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi,
    Within Excel, we can have data validation for the cells' values using a named list that is from the same workbook/sheets. How to programmably check if one cell value is within the named list, using VBA?

    One thing more is, I remember that we can sort by several columns on a selected range (suppose that the range covers several columns) using Selection.Sort, in VBA. Am I right?
    Many Thanks & Best Regards, HuaMin Chen

Answers

  • Thursday, November 05, 2009 1:01 AMSJOO Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Hi

    If you want to know if a data is in the list or not, the best easy way ,I think, is WorksheetFunction.CountIf.

    WorksheetFunction.COUNTIF(range/list,criteria)

    HTH

    SJOO

    the best time to plant a tree was twenty years ago. the second best time, is today (Chinese proverb) sjoo.kwak at gmail.com
    • Marked As Answer byHuaMin Chen Friday, November 06, 2009 2:11 AM
    •  
  • Thursday, November 05, 2009 4:53 AMSJOO Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Hi

    I think you should write the code like this:

    If WorksheetFunction.CountIf(Range("Mainlist"), Trim(Cells(row, 1).Value)) = 0 Then

    HTH

    SJOO

    the best time to plant a tree was twenty years ago. the second best time, is today (Chinese proverb) sjoo.kwak at gmail.com
    • Marked As Answer byHuaMin Chen Friday, November 06, 2009 2:11 AM
    •  

All Replies

  • Thursday, November 05, 2009 1:01 AMSJOO Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Hi

    If you want to know if a data is in the list or not, the best easy way ,I think, is WorksheetFunction.CountIf.

    WorksheetFunction.COUNTIF(range/list,criteria)

    HTH

    SJOO

    the best time to plant a tree was twenty years ago. the second best time, is today (Chinese proverb) sjoo.kwak at gmail.com
    • Marked As Answer byHuaMin Chen Friday, November 06, 2009 2:11 AM
    •  
  • Thursday, November 05, 2009 2:02 AMHuaMin Chen Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Many thanks. I do have a problem with this line
            If WorksheetFunction.CountIf(Mainlist, Trim(Cells(row, 1).Value)) = 0 Then

    where the range name 'Mainlist' is created within the sheets


    Many Thanks & Best Regards, HuaMin Chen
  • Thursday, November 05, 2009 4:53 AMSJOO Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Hi

    I think you should write the code like this:

    If WorksheetFunction.CountIf(Range("Mainlist"), Trim(Cells(row, 1).Value)) = 0 Then

    HTH

    SJOO

    the best time to plant a tree was twenty years ago. the second best time, is today (Chinese proverb) sjoo.kwak at gmail.com
    • Marked As Answer byHuaMin Chen Friday, November 06, 2009 2:11 AM
    •