Consolidate Data onto one row

Ivan Howard

Active Member
Joined
Nov 10, 2004
Messages
332
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

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type

Mel Smith

Well-known Member
Joined
Dec 13, 2005
Messages
979
Office Version
  1. 365
Platform
  1. Windows
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

Ivan Howard

Active Member
Joined
Nov 10, 2004
Messages
332
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

cornflakegirl

Well-known Member
Joined
Nov 4, 2004
Messages
2,023
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

Ivan Howard

Active Member
Joined
Nov 10, 2004
Messages
332
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

cornflakegirl

Well-known Member
Joined
Nov 4, 2004
Messages
2,023
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

Ivan Howard

Active Member
Joined
Nov 10, 2004
Messages
332
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,191,707
Messages
5,988,229
Members
440,139
Latest member
ngaicuong2017

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
Top