View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Greg Wilson Greg Wilson is offline
external usenet poster
 
Posts: 747
Default combine rows of data to one row...tough one

Assumed is that the names are all first name / last name with no exceptions
such as single names or triple names. The code gets its reference from the
space preceeding the first name. An extra or missing space will screw it up.

Also assumed is that the data starts in cell A2 and continues to the last
datum in column A. The code tolerates gaps. So ensure there is nothing in
below the intended data.

The results will be pasted to column C starting at C2. Minimal testing.
Seems OK. I'm tired and off to bed. Good luck.

Sub CombineData()
Dim r As Range, c As Range
Dim i As Integer, x As Integer
Dim nm As String, currnm As String
Dim info As String, txt As String

Set r = Range(Cells(2, 1), Cells(Rows.Count, 1).End(xlUp))
i = 2
For Each c In r.Cells
txt = Trim(c.Value)
If Len(txt) 0 Then
x = InStrRev(txt, " ")
x = InStrRev(txt, " ", x - 1)
nm = Right$(txt, Len(txt) - x)
If currnm < nm Then
If Len(currnm) 0 Then
Cells(i, 3) = info & " " & currnm
i = i + 1
End If
currnm = nm
info = Left$(txt, x - 1)
Else
info = info & " " & Left$(txt, x - 1)
End If
End If
Next
Cells(i, 3) = info & " " & currnm
Set c = Nothing: Set r = Nothing
End Sub