Results 1 to 5 of 5

Thread: [RESOLVED] More Excel Macro Fun!

  1. #1

    Thread Starter
    Fanatic Member The_Grudge's Avatar
    Join Date
    Jan 2005
    Location
    Canada
    Posts
    836

    Resolved [RESOLVED] More Excel Macro Fun!

    Firstly, let me apologize for all of my "macro" posts the last few days. I could have merged them all into one but I had no idea that I was going to need more than one! If your out there DKenny you may be able to help since your solution yesterday is VERY similar to today's issue.

    Okay, hopefully this is it.
    I'm using Excel so I need a macro to do the following.

    Lets' say I have the following data (Name, Patient ID, and Date)...

    DAVID, 01251, 20051214
    DAVID, 01251, 20051214
    JOE, 05564, 20051215
    ERIC, 06498, 20051215
    ERIC, 06498, 20051216

    Well, wherever the Patient and Date are the same I need to create an index.
    So it would end up looking like...

    DAVID, 01251, 20051214 ----> 1
    DAVID, 01251, 20051214 ----> 2
    JOE, 05564, 20051215 ----> 1
    ERIC, 06498, 20051215 -----> 1
    ERIC, 06498, 20051216 ------> 1

    The index only increments if both the PATIENTID and NAME are the same.

    Got me?

    Thanks in advance for the help guys & gals.

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

    Re: More Excel Macro Fun!

    Quote Originally Posted by The_Grudge
    Well, wherever the Patient and Date are the same I need to create an index.
    So it would end up looking like...

    ERIC, 06498, 20051215 -----> 1
    ERIC, 06498, 20051216 ------> 1

    The index only increments if both the PATIENTID and NAME are the same.
    Which is it? Patient and Date (per the first statement) or PATIENTID and NAME (per the second statement).
    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
    Fanatic Member The_Grudge's Avatar
    Join Date
    Jan 2005
    Location
    Canada
    Posts
    836

    Re: More Excel Macro Fun!



    Wow.....I need to SLOOOOOW DOWNNNNNNN.

    It's patient ID and Date.

    NOTE:
    My file actually contains 15 columns or so but I figured it would be easier to cut out the 2 columns I need onto their own sheet, get the indexes with a macro, and paste the indexes back into the original sheet. That way I don't have to worry about messing with the other columns.

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

    Re: More Excel Macro Fun!

    OK
    Assuming that you have moved the ID and Date columns to a new sheet, the following will index that sheet for you. You will need to change the reference for the rngDataTable variable.

    VB Code:
    1. Sub GrudgeIndex()
    2. Dim rngDataTable As Range
    3. Dim rngCell As Range
    4.    
    5.     'This is the 2 column range that contains the ID in
    6.     'the first column and the date in the second column
    7.     Set rngDataTable = ThisWorkbook.Worksheets(1).UsedRange
    8.    
    9.     'Note: I'm assuming that the range has already been sorted
    10.     'by ID then Date - If not you should insert a sort here
    11.    
    12.     'Loop through each PatientID cell
    13.     For Each rngCell In rngDataTable.Columns(1).Cells
    14.        
    15.         'I'm assuming that the 1st row contains headers
    16.         If rngCell.Row <> 1 Then
    17.            
    18.             'If the ID is different that the ID on the
    19.             'previous row, or the date is different from
    20.             'the date on the previous row
    21.             'we restart the index at 1
    22.             If rngCell.Value <> rngCell.Offset(-1, 0) _
    23.             Or rngCell.Offset(0, 1) <> rngCell.Offset(-1, 1) Then
    24.                 rngCell.Offset(0, 2).Value = 1
    25.            
    26.             'Otherwise we increment the index by 1
    27.             Else
    28.                 rngCell.Offset(0, 2).Value = rngCell.Offset(-1, 2).Value + 1
    29.             End If
    30.         End If
    31.     Next rngCell
    32.    
    33.    
    34.     Set rngDataTable = Nothing
    35. 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
    Fanatic Member The_Grudge's Avatar
    Join Date
    Jan 2005
    Location
    Canada
    Posts
    836

    Re: More Excel Macro Fun!

    NICE.

    How much easier is life now!!

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