The easy stuff first. The iCol/FirstCol only refer to the columns that should
be checked & merged. Since the first column is the key column, you wouldn't
want: bob,bob,bob as the result.
The difficult part.
Your second question:
Do you mean you want the values strung together:
x,x,1,x,1
Then this might work:
Option Explicit
Sub testme2()
Dim wks As Worksheet
Dim FirstRow As Long
Dim LastRow As Long
Dim iRow As Long
Dim FirstCol As Long
Dim iCol As Long
Set wks = Worksheets("sheet1")
With wks
FirstRow = 2 'header row?
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
FirstCol = 2 'names in column A
For iRow = LastRow To FirstRow + 1 Step -1
If .Cells(iRow, "A").Value = .Cells(iRow - 1, "A").Value Then
For iCol = FirstCol To _
.Cells(iRow, .Columns.Count).End(xlToLeft).Column
If IsEmpty(.Cells(iRow, iCol)) Then
'do nothing
Else
If IsEmpty(.Cells(iRow - 1, iCol)) Then
.Cells(iRow - 1, iCol).Value _
= .Cells(iRow, iCol).Value
Else
.Cells(iRow - 1, iCol).Value _
= .Cells(iRow - 1, iCol).Value _
& " ," & .Cells(iRow, iCol).Value
End If
End If
Next iCol
'delete this row with the same name
.Rows(iRow).Delete
End If
Next iRow
End With
End Sub
or if the row above is not empty, then keep it and don't change it?
Then this might work:
Option Explicit
Sub testme3()
Dim wks As Worksheet
Dim FirstRow As Long
Dim LastRow As Long
Dim iRow As Long
Dim FirstCol As Long
Dim iCol As Long
Set wks = Worksheets("sheet1")
With wks
FirstRow = 2 'header row?
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
FirstCol = 2 'names in column A
For iRow = LastRow To FirstRow + 1 Step -1
If .Cells(iRow, "A").Value = .Cells(iRow - 1, "A").Value Then
For iCol = FirstCol To _
.Cells(iRow, .Columns.Count).End(xlToLeft).Column
If IsEmpty(.Cells(iRow - 1, iCol)) = False Then
'do nothing
Else
'just move it up and overwrite anything there?
.Cells(iRow - 1, iCol).Value = .Cells(iRow, iCol).Value
End If
Next iCol
'delete this row with the same name
.Rows(iRow).Delete
End If
Next iRow
End With
End Sub
(Just check the cell above. If it's not empty, don't touch it.
Ricky said:
Hi Dave,
I appreciate your response. The data in the cells would be either "X"s
or "1"s. After running your macro, I've elected to go with the "1"s
instead.
On your post:
With wks
FirstRow = 2 'header row? [Yes]
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
FirstCol = 2 'names in column A
Question:
As the search criteria is based on Column A's names, should it have been
{First Col = 1}?
Another part:
'just move it up and overwrite anything there?
.Cells(iRow - 1, iCol).Value = .Cells(iRow, iCol).Value
Question:
No, not overwriting the X's that is already on the previous row. I
wanted to paste special, skip blanks, so that the data is not
overwritten. The X's will just keep adding up a row until all of the
X's (or 1's), all amalgamate onto the first row of the groupings.
So far, your code does delete the multiple names, leaving only 1
distinct name each. But, I am having trouble working around the
consolidating all of the X's from multiple lines into just one line for
each group.
Thanks Dave for helping me.
Ricky