Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 23
Default Merging Rows

I am trying to merge data in several columns to one row, for example.

Col 'A' Col 'B' Col 'C' Col 'D' Col 'E'
Joe 10/1/08
Joe 10/2/08
Joe 10/3/08
Kim 10/2/08
Kim 10/1/08
John 10/1/08
John 10/2/08
John 10/3/08
John 10/4/08

The Output should look like this:

Col 'A' Col 'B' Col 'C' Col 'D' Col 'E'
Joe 10/1/08 10/2/08 10/3/08

Kim 10/1/08 10/2/08
John 10/1/08 10/2/08 10/3/08 10/4/08


I have the following macro but it does not seem to work completely right. It
does some merging but leaves some duplicate names - wondered if anyone could
help?

Sub Remove_Duplicate()

Dim LASTROW As Long
Dim I As Long
Dim J As Long
Dim K As Long
Dim MyVALUE As Variant

Application.ScreenUpdating = False
LASTROW = Range("A" & Rows.Count).End(xlUp).Row
For I = 1 To LASTROW - 1
MyVALUE = Cells(I, "C") & Cells(I, "D")
For J = I + 1 To LASTROW
If (MyVALUE = Cells(J, "D") & Cells(J, "E")) Then
For K = 1 To 13
If (Cells(I, K) = "") Then Cells(I, K) = Cells(J, K)
Next K
Cells(J, "A").EntireRow.Delete
End If
Next J
Next I
Application.ScreenUpdating = True
'
End Sub
--
Jake
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9,101
Default Merging Rows


Sub Remove_Duplicate()

Dim LASTROW As Long
Dim I As Long
Dim J As Long
Dim K As Long
Dim MyVALUE As Variant

Application.ScreenUpdating = False
LASTROW = Range("A" & Rows.Count).End(xlUp).Row
RowCount = 1
Do While Range("A" & RowCount) < ""
Do While Range("A" & RowCount) = Range("A" & (RowCount + 1))
For ColCount = 2 To 5
If Cells(RowCount, ColCount) = "" And Cells(RowCount + 1,
ColCount) < "" Then
Cells(RowCount, ColCount) = Cells(RowCount + 1, ColCount)
End If
Next ColCount
Rows(RowCount + 1).Delete
Loop
RowCount = RowCount + 1
Loop
Application.ScreenUpdating = True
'
End Sub
"JakeShipley2008" wrote:

I am trying to merge data in several columns to one row, for example.

Col 'A' Col 'B' Col 'C' Col 'D' Col 'E'
Joe 10/1/08
Joe 10/2/08
Joe 10/3/08
Kim 10/2/08
Kim 10/1/08
John 10/1/08
John 10/2/08
John 10/3/08
John 10/4/08

The Output should look like this:

Col 'A' Col 'B' Col 'C' Col 'D' Col 'E'
Joe 10/1/08 10/2/08 10/3/08

Kim 10/1/08 10/2/08
John 10/1/08 10/2/08 10/3/08 10/4/08


I have the following macro but it does not seem to work completely right. It
does some merging but leaves some duplicate names - wondered if anyone could
help?

Sub Remove_Duplicate()

Dim LASTROW As Long
Dim I As Long
Dim J As Long
Dim K As Long
Dim MyVALUE As Variant

Application.ScreenUpdating = False
LASTROW = Range("A" & Rows.Count).End(xlUp).Row
For I = 1 To LASTROW - 1
MyVALUE = Cells(I, "C") & Cells(I, "D")
For J = I + 1 To LASTROW
If (MyVALUE = Cells(J, "D") & Cells(J, "E")) Then
For K = 1 To 13
If (Cells(I, K) = "") Then Cells(I, K) = Cells(J, K)
Next K
Cells(J, "A").EntireRow.Delete
End If
Next J
Next I
Application.ScreenUpdating = True
'
End Sub
--
Jake

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Merging Rows

When you're deleting rows like this, it really makes it easier to start at the
bottom and work your way to the top. Then you don't have to worry about what
row you're code is processing.

Option Explicit
Sub Remove_Duplicate()

Dim LastRow As Long
Dim TopRow As Long
Dim iCol As Long
Dim iRow As Long
Dim wks As Worksheet

Set wks = Worksheets("Sheet1")

Application.ScreenUpdating = False

With wks
TopRow = 2 'headers in row 1
LastRow = .Range("A" & .Rows.Count).End(xlUp).Row

For iRow = LastRow To TopRow + 1 Step -1
If .Cells(iRow, "A").Value = .Cells(iRow - 1, "A").Value Then
'same name
For iCol = .Cells(iRow, .Columns.Count).End(xlToLeft).Column _
To 2 Step -1
If IsEmpty(.Cells(iRow, iCol).Value) Then
'do nothing
Else
.Cells(iRow - 1, iCol).Value = .Cells(iRow, iCol).Value
End If
Next iCol
.Rows(iRow).Delete
End If
Next iRow
End With

Application.ScreenUpdating = True

End Sub

This does expect that there is no more than one date per person per column.

JakeShipley2008 wrote:

I am trying to merge data in several columns to one row, for example.

Col 'A' Col 'B' Col 'C' Col 'D' Col 'E'
Joe 10/1/08
Joe 10/2/08
Joe 10/3/08
Kim 10/2/08
Kim 10/1/08
John 10/1/08
John 10/2/08
John 10/3/08
John 10/4/08

The Output should look like this:

Col 'A' Col 'B' Col 'C' Col 'D' Col 'E'
Joe 10/1/08 10/2/08 10/3/08

Kim 10/1/08 10/2/08
John 10/1/08 10/2/08 10/3/08 10/4/08

I have the following macro but it does not seem to work completely right. It
does some merging but leaves some duplicate names - wondered if anyone could
help?

Sub Remove_Duplicate()

Dim LASTROW As Long
Dim I As Long
Dim J As Long
Dim K As Long
Dim MyVALUE As Variant

Application.ScreenUpdating = False
LASTROW = Range("A" & Rows.Count).End(xlUp).Row
For I = 1 To LASTROW - 1
MyVALUE = Cells(I, "C") & Cells(I, "D")
For J = I + 1 To LASTROW
If (MyVALUE = Cells(J, "D") & Cells(J, "E")) Then
For K = 1 To 13
If (Cells(I, K) = "") Then Cells(I, K) = Cells(J, K)
Next K
Cells(J, "A").EntireRow.Delete
End If
Next J
Next I
Application.ScreenUpdating = True
'
End Sub
--
Jake


--

Dave Peterson
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Merging rows JakeShipley2008 Excel Programming 2 October 17th 08 05:44 PM
Is merging rows possible? Mr BT[_2_] Excel Worksheet Functions 1 May 16th 08 04:28 PM
Merging rows Peter Horrocks New Users to Excel 1 November 15th 05 12:01 PM
Merging Two Rows Into One Ourania Excel Worksheet Functions 1 March 18th 05 10:07 AM
Merging a number of rows Smeesh Excel Programming 1 May 4th 04 12:56 PM


All times are GMT +1. The time now is 09:57 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"