Consolidate Data onto one row

Ivan Howard

Active Member
Joined
Nov 10, 2004
Messages
333
Hi All,

I have a small issue and have tried to write a macro for it, but I am struggling:

I have a list of staff numbers in column A... this holds duplicates. In column B, corresponding to each staff number is Apples, Bananas, Grapes, Oranges or Pears... Sometimes each person can have up to 5 entries, i.e. 5 rows...

What I need is a macro that will place each persons 'fruit preferences' onto one row, corresponding to their staff number, so that I can do a vlookup using the resulting list.

Preferrably, the staff number should remain in column A, the 1st choice should be in column B, 2nd choice in column C, 3rd choice in column D... etc.

I would really appreciate your help with this.
Thanks
Ivan
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Hi Ivan, if each staff member had the same number of fruit "options", even if some were left blank, it would be then straightforward to write the macro. Your current difficulty as I see it is the variable "fruits" per staff member, hence the macro not doing what you want.

Mel
 
Upvote 0
Thanks Mel.

I agree, I wrote the macro and it was fine dealing with the same number of rows, but I thought that it might be possible to do a count fo the number of entries per person and then get the macro to loop that certain number of times.

I suppose there might also be a way to do an 'inventive' vlookup where I concatenate the count of each entry to the staff number and then somehow try and pull in back to the final result I am looking for.

Thanks very much for your help.

Take care.
Ivan
 
Upvote 0
Ivan - don't know if you're still working on this, but try this code:

Code:
Sub MultipleRowsToOne()
 
Dim iCounter As Integer
Dim iOriginalRow As Integer
Dim iDuplicates As Integer
 
For iCounter = Range("a1").End(xlDown).Row To 1 Step -1
    iDuplicates = Evaluate("countif(a1:a" & iCounter & "," & Range("a" & iCounter).Value & ")")
    If iDuplicates > 1 Then
        iOriginalRow = Evaluate("match(" & Range("a" & iCounter).Value & ",a1:a" & iCounter & ",0)")
        Range("a" & iOriginalRow).Offset(0, iDuplicates).Value = Range("b" & iCounter).Value
        Range("a" & iCounter).EntireRow.Delete
    End If
Next iCounter
 
End Sub
 
Upvote 0
Thanks very much Emma, but I cannot get this to work.

the active cell does not seem to move off the selected cell.
 
Upvote 0
Ivan - the activecell doesn't change - it doesn't need to.
I tested the macro on some sample data, based on the information you gave. So my guess is that there are references that need to change to work on your actual data? Are your data actually in columns A & B, starting at cell A1?
 
Upvote 0
Thanks Emma.

Yes the data started in A1 - with no titles. I have tried the staff numbers as numerics and also as text, but I cant get it to work.

However, I have used Access to query the data and it is all fine now.

Thanks very much for your help.
 
Upvote 0

Forum statistics

Threads
1,214,785
Messages
6,121,543
Members
449,038
Latest member
Guest1337

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top