Results 1 to 10 of 10

Thread: Excel - Loop Through a Column and Compare?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 2002
    Location
    Thunder Bay, ON
    Posts
    89

    Excel - Loop Through a Column and Compare?

    I have to create a macro and I'm stuck on this step.

    I have to take the first Employee Number in Column A and first see if there are any other entries in column A with the same Employee Number.
    Perform a task based on that.
    Then go to the next employee number, and the next, etc.

    Anyone know how to perform this task?
    "That is just it: you must have the devil in you to succeed in any art."

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

    Re: Excel - Loop Through a Column and Compare?

    hopefully this will help.

    VB Code:
    1. Sub Duplicates()
    2. Dim rngCompareArea As Range
    3. Dim rngCheckCell As Range
    4. Dim sCellText As String
    5. Dim lOccurance As Long
    6.  
    7.     'First establish the range that contains
    8.     'the cells to be checked
    9.    
    10.     'This is just an example, you weill need to reference the correct range
    11.     Set rngCompareArea = ThisWorkbook.Worksheets(1).Columns(1)
    12.    
    13.     'Loop through each cell in the range
    14.     For Each rngCheckCell In rngCompareArea.Cells
    15.        
    16.         'Get the value in the cell
    17.         sCellText = rngCheckCell.Text
    18.        
    19.         'Ignore blanks
    20.         If sCellText <> "" Then
    21.            
    22.             'How many times does that value
    23.             'occur in the range?
    24.             lOccurance = Application.WorksheetFunction.CountIf(rngCompareArea, sCellText)
    25.            
    26.             'If its more than once....
    27.             If lOccurance > 1 Then
    28.                
    29.                 '...Perform your action here
    30.                
    31.             End If
    32.         End If
    33.     Next rngCheckCell
    34.    
    35.     'Clear object variables
    36.     Set rngCompareArea = Nothing
    37. End Sub
    Declan

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

  3. #3

    Thread Starter
    Lively Member
    Join Date
    May 2002
    Location
    Thunder Bay, ON
    Posts
    89

    Re: Excel - Loop Through a Column and Compare?

    Good news, your code works. It runs through and counts the number of re-occuring numbers.

    The issue now, is if the same number appears in Column A I need it to perform a check on it's matching column and then perform some math.

    So if Employee Number 53 is found twice then add their number in another column up.
    "That is just it: you must have the devil in you to succeed in any art."

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

    Re: Excel - Loop Through a Column and Compare?

    Have you considered the SUMIF function within excel? It sounds like this is what you need.
    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
    Lively Member
    Join Date
    May 2002
    Location
    Thunder Bay, ON
    Posts
    89

    Re: Excel - Loop Through a Column and Compare?

    Perhaps a visual refrence may help me a little.

    I have a spreadsheet with the following data. I need to go through it line by line and first find all matching "Pers.no." and then add their corresponsing "Days" column.

    And then throw all this data on another sheet with it all grouped together. There is one more IF stmt to be included but that is for later on!


    VB Code:
    1. Pers.no.    Name                    Org Key             Type            Days
    2. 53          K#thl##n W#lk#nsh#w TWKS - CENT SU      (No absence)    3
    3. 63          R#ch#rd W#dd#####n  CITY COUNCIL        (No absence)    7
    4. 68          L#wr#nc# T####          CITY COUNCIL        (No absence)    12
    "That is just it: you must have the devil in you to succeed in any art."

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

    Re: Excel - Loop Through a Column and Compare?

    you don't need code to do this, SUMIF will give you exactly what you described here.


    Here's an example, have a look at the formulas in cells I2:I4
    Attached Files Attached Files
    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
    Lively Member
    Join Date
    May 2002
    Location
    Thunder Bay, ON
    Posts
    89

    Re: Excel - Loop Through a Column and Compare?

    I understand what your saying, unfortunately this has to be done in a MACRO because there are some other calculations and actions to be performed after it has calculated the days.
    "That is just it: you must have the devil in you to succeed in any art."

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

    Re: Excel - Loop Through a Column and Compare?

    OK
    Can you explain what "other calculation and actions" need to be performed?
    Declan

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

  9. #9

    Thread Starter
    Lively Member
    Join Date
    May 2002
    Location
    Thunder Bay, ON
    Posts
    89

    Re: Excel - Loop Through a Column and Compare?

    Long story short, I have to check the "TYPE" category, if the word "Authorized Unpaid Absences" appears in any of that persons rows then I have to subtract 3 from the total of the "Days" column.

    Then if the Days total exceeds "3" I have to delete them from the list, otherwise keep it.

    I know its confusing.
    "That is just it: you must have the devil in you to succeed in any art."

  10. #10
    Fanatic Member Dnereb's Avatar
    Join Date
    Aug 2005
    Location
    Netherlands
    Posts
    863

    Re: Excel - Loop Through a Column and Compare?

    If there's no counter argument it's best to let Excel Sort your range
    for the specific column first.
    This way all double occurence's are follow each other up.
    and your data will be easier to manage.
    why can't programmers keep and 31 Oct and 25 dec apart. Why Rating is Useful
    for every question you ask provide an answer on another thread.

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