Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Removing duplicate rows and combining unique data

Hi,

I'm pretty new to Excel VBA programming. I'm trying to make a
subroutine that will iterate through the rows removing all duplicate
rows (using a column A for the unique cell values) and taking and
concatenating all the String values from a different column (F) in rows
with the same key value into one single cell - in the row not deleted
after the duplicate removal.

I'm using CPearson's code for removing duplicates with my own (messy)
additions to try and combine the cell values but it doesn't work
properly. The concatenation part seems to work, but it puts the
concatenated string into the wrong cell (usually beneath). Any
suggestions would be much appreciated.

Code:

Sub DelDuplicates()

Dim rowNumber As Long
Dim toCompany As String
Dim firstTime As Boolean
Dim currentRow As Integer

firstTime = True
currentRow = Selection(Selection.Cells.Count).Row

ColNum = Selection(1).Column
For RowNdx = Selection(Selection.Cells.Count).Row To _
Selection(1).Row + 1 Step -1

If Cells(RowNdx, ColNum).Value = Cells(RowNdx - 1, ColNum).Value
Then
toCompany = toCompany & ", " & Range("F" &
currentRow).Value
Cells(RowNdx, ColNum).EntireRow.Delete
Else
If firstTime = True Then
rowNumber = currentRow
toCompany = Range("F" & currentRow).Value
firstTime = False
Else
rowNumber = currentRow
Range("F" & rowNumber + 1).Value = toCompany
toCompany = Range("F" & currentRow).Value
End If
End If
currentRow = currentRow - 1

Next RowNdx
End Sub

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Removing duplicate rows and combining unique data

Sub DelDuplicates()

Dim rowNumber As Long
Dim toCompany As String
Dim firstTime As Boolean
Dim currentRow As Integer

firstTime = True
currentRow = Selection(Selection.Cells.Count).Row

ColNum = Selection(1).Column
For RowNdx = Selection(Selection.Cells.Count).Row To _
Selection(1).Row + 1 Step -1
If Cells(RowNdx, ColNum).Value = _
Cells(RowNdx - 1, ColNum).Value Then
toCompany = toCompany & ", " & Range("F" & RowNdx).Value
Cells(RowNdx, ColNum).EntireRow.Delete
Else
If Len(toCompany) 0 Then
Cells(RowNdx, "F") = Right(toCompany, _
Len(toCompany) - 1) & ", " & Cells(RowNdx, "F")
toCompany = ""
End If
End If
Next RowNdx
End Sub

--
Regards,
Tom Ogilvy

wrote in message
oups.com...
Hi,

I'm pretty new to Excel VBA programming. I'm trying to make a
subroutine that will iterate through the rows removing all duplicate
rows (using a column A for the unique cell values) and taking and
concatenating all the String values from a different column (F) in rows
with the same key value into one single cell - in the row not deleted
after the duplicate removal.

I'm using CPearson's code for removing duplicates with my own (messy)
additions to try and combine the cell values but it doesn't work
properly. The concatenation part seems to work, but it puts the
concatenated string into the wrong cell (usually beneath). Any
suggestions would be much appreciated.

Code:

Sub DelDuplicates()

Dim rowNumber As Long
Dim toCompany As String
Dim firstTime As Boolean
Dim currentRow As Integer

firstTime = True
currentRow = Selection(Selection.Cells.Count).Row

ColNum = Selection(1).Column
For RowNdx = Selection(Selection.Cells.Count).Row To _
Selection(1).Row + 1 Step -1

If Cells(RowNdx, ColNum).Value = Cells(RowNdx - 1, ColNum).Value
Then
toCompany = toCompany & ", " & Range("F" &
currentRow).Value
Cells(RowNdx, ColNum).EntireRow.Delete
Else
If firstTime = True Then
rowNumber = currentRow
toCompany = Range("F" & currentRow).Value
firstTime = False
Else
rowNumber = currentRow
Range("F" & rowNumber + 1).Value = toCompany
toCompany = Range("F" & currentRow).Value
End If
End If
currentRow = currentRow - 1

Next RowNdx
End Sub



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Removing duplicate rows and combining unique data

Thanks. It works great!

Regards,
Chris

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
Combining duplicate rows into one ceci Excel Discussion (Misc queries) 2 February 4th 09 02:42 AM
Removing duplicate rows Jase4now Excel Discussion (Misc queries) 3 October 24th 07 09:42 PM
combining duplicate rows jezzica85 Excel Discussion (Misc queries) 1 March 18th 06 02:30 PM
Removing Duplicate Rows bvinternet Excel Discussion (Misc queries) 1 July 23rd 05 09:26 PM
Removing Near-Duplicate Rows, Leaving Those w/Most Data in Specific Columns foofoo Excel Discussion (Misc queries) 1 April 2nd 05 12:02 AM


All times are GMT +1. The time now is 10:39 AM.

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"