Results 1 to 7 of 7

Thread: MS Excel Macro for validating unique entries in the column

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2005
    Posts
    14

    Question MS Excel Macro for validating unique entries in the column

    Hi,
    I need to validate a column in Excel for its uniqueness. This column should accept only unique values. The function should iterate through the entire column to compare the string with all the previous and next cells in the column. How can i do this using a function n not any formula?
    Also i'd appreciate if somebody can help me with an Excel sheet with 5 columns such that only 1 of the columns has a '1' n rest all can have only '0's.
    What i mean is suppose u have columns A-E. If u put a '1' in cell A2, rest all ie B2,C2,D2,E2 must have only '0's n no other value. So that only 1 of 5 cells has a '1'. Next time u may choose C5 to put '1'.Then A5,B5,D5,E5 must have only '0's.
    Thanks in advance.

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: MS Excel Macro for validating unique entries in the column

    Moved to Office Development.

  3. #3
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: MS Excel Macro for validating unique entries in the column

    OK, lets do this one question at a time.

    Quote Originally Posted by technowerk
    I need to validate a column in Excel for its uniqueness. This column should accept only unique values.

    The following function will check for uniqueness in a sinngle column. The Worksheeet_change event will fire whenever a value is entered into a cell. In my example I am only checking for unique vales in column 5, but you can check in one, many or all columns.

    I will answer your secong question in a seperate post.

    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Worksheet_Change(ByVal Target As Range)
    4.        
    5.     'Assuming you only want to test for uniqueness in Col 5 ("E")
    6.     If Target.Column = 5 Then
    7.     'If the value being entered is a duplicate in the column then remove it
    8.         If Not Unique(Target.Column) Then
    9.             Application.EnableEvents = False
    10.             Target = ""
    11.             Application.EnableEvents = True
    12.         End If
    13.     End If
    14.    
    15. End Sub
    16.  
    17.  
    18. Private Function Unique(ColNum As Integer) As Boolean
    19. Dim RowCount As Integer
    20. Dim RowNum As Integer
    21. Dim ChkVal As String
    22. Dim ChkCount As Integer
    23.    
    24.     'Assume there are no duplicates
    25.     Unique = True
    26.    
    27.     'Determine how many rows to test
    28.     RowCount = ActiveSheet.UsedRange.Rows.Count
    29.    
    30.     'loop through each cell in the target column
    31.     For RowNum = 1 To RowCount
    32.         'Get the value in the cell being tested
    33.         ChkVal = ActiveSheet.Cells(RowNum, ColNum)
    34.         'Count the occurances of that value in the column
    35.         If ChkVal <> "" Then
    36.             ChkCount = Application.WorksheetFunction.CountIf(ActiveSheet.Columns(ColNum), ChkVal)
    37.         Else
    38.             ChkCount = 1
    39.         End If
    40.         'If the value occurs more that once set unique to false and exit the loop
    41.         If ChkCount > 1 Then
    42.             Unique = False
    43.             Exit For
    44.         End If
    45.     Next RowNum
    46. End Function
    Last edited by DKenny; Dec 9th, 2005 at 10:37 AM.
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  4. #4
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: MS Excel Macro for validating unique entries in the column

    Quote Originally Posted by technowerk
    i'd appreciate if somebody can help me with an Excel sheet with 5 columns such that only 1 of the columns has a '1' n rest all can have only '0's.
    Again we will use the Worksheet_Change event to trigger this code. The code only runs when the user edits a cell in the first 5 columns. It also checks to ensure that only one cell is being edited.

    If the cell value is zero or is an empty cell it does nothing.
    If the cell value is other that 1, 0 or empty it clears the cell.
    If the cell value is 1 it loops through the first 5 cells in the row of the target cell and sets their value to 0, except for the target cell.

    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Worksheet_Change(ByVal Target As Range)
    4. Dim ColNum As Integer
    5.    
    6.     'only run this check if editing the first 5 columns and only editing a single cell
    7.     If Target.Column <= 5 And Target.Cells.Count = 1 Then
    8.         Select Case Target.Value
    9.             Case 0
    10.                 'do nothing
    11.             Case 1
    12.                 'Loop throught the 5 columns
    13.                 For ColNum = 1 To 5
    14.                     'You don't need to change the current cell
    15.                     If ColNum <> Target.Column Then
    16.                         'Change the other 4 cells to 0
    17.                         Cells(Target.Row, ColNum) = 0
    18.                     End If
    19.                 Next ColNum
    20.             Case Else
    21.                 ' If a value other than 1 or 0 is entered, remove it. (This is optional - I don't know if this is a reqirement)
    22.                 Application.EnableEvents = False
    23.                 Target.Value = ""
    24.                 Application.EnableEvents = True
    25.         End Select
    26.     End If
    27. End Sub
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  5. #5

    Thread Starter
    New Member
    Join Date
    Dec 2005
    Posts
    14

    Re: MS Excel Macro for validating unique entries in the column

    hey DKenny,
    thanks a lot for ur quick reply. I really appreciate that. The codes u sent me work fantastic n produce the desired functionality. No doubt about it. However I once again need ur help to modify the codes so as to run them in separate macros n not as a "worksheet_change" event b'coz i want the users to fire the event to chech the column's validity. I don't want it to happen as n when the users enter the data.
    Hope u will help me with this problem also as u did earlier. Looking forward to ur guidance.

    Thank you once again.

  6. #6
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: MS Excel Macro for validating unique entries in the column

    You could add a couple of buttons to the worksheet and move the code into the "_click" events.
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  7. #7

    Thread Starter
    New Member
    Join Date
    Dec 2005
    Posts
    14

    Re: MS Excel Macro for validating unique entries in the column

    hey,
    It works fantastic. Thank you so much for sparing ur time for me. Hope to receive more guidance from you in the future as well.

    Thanks once again.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width