Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 553
Default Searching for Duplicates across columns and print to new column

I have multiple columns say 5 of them (1 every two columns starting in column
B and ending in column J). Each column has varying rows of string data. I
want to find the duplicate data across the columns and summarize in another
column 2 away from column J (column L). Hence column L will only show the
duplicate data found in the the 5 other columns. Note that there will not be
duplicate data within any one column. The duplicate data will only exist
across columns.

I am assuming that the best way to do this is to create a VBA 2D array,
define it by the colunn with the most rows of data and then load each column
into the array and search for duplicates from there. I am assuming that I
would then store the duplicates in another array and then dump the results to
column L.

Any insight on how to do this efficiently?

Thanks
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,119
Default Searching for Duplicates across columns and print to new column

Here is some code that I use. Place this in a standard module and reference
the "Micorsoft Scripting Runtime". Highlight the 5 columns to be searched for
duplicates and run the procedure. It will create a new sheet containing all
of the duplicates. With a little modification you should be able to make it
work the way you need.

Private Sub GetDuplicateItems()
Dim cell As Range 'Current cell in range to check
Dim rngToSearch As Range 'Cells to be searched
Dim dic As Scripting.Dictionary 'Dictionary Object
Dim dicItem As Variant 'Items within dictionary object
Dim wks As Worksheet 'Worksheet to populate with
unique items
Dim rngPaste As Range 'Cells where unique items are
placed
Dim aryDuplicates() As String
Dim lngCounter As Long

'Create range to be searched
Set rngToSearch = Intersect(ActiveSheet.UsedRange, Selection)
lngCounter = 0

'Confirm there is a relevant range selected
If Not rngToSearch Is Nothing Then
'Create dictionay object
Set dic = New Scripting.Dictionary

'Populate dictionary object with unique items (use key to define
unique)
For Each cell In rngToSearch 'Traverse selected range
If Not dic.Exists(cell.Value) And cell.Value < Empty Then
'Check the key
dic.Add cell.Value, cell.Value 'Add the item if unique
Else
ReDim Preserve aryDuplicates(lngCounter)
aryDuplicates(lngCounter) = cell
lngCounter = lngCounter + 1
End If
Next

If lngCounter 0 Then 'Check for values
Set wks = Worksheets.Add 'Create worksheet to populate
Set rngPaste = wks.Range("A1") 'Create range to populate
For lngCounter = LBound(aryDuplicates) To UBound(aryDuplicates)
'Loop duplicates
rngPaste.NumberFormat = "@" 'Format cell as text
rngPaste.Value = aryDuplicates(lngCounter) 'Add items to
new sheet
Set rngPaste = rngPaste.Offset(1, 0) 'Increment paste range
Next lngCounter
'Clean up objects
Set wks = Nothing
Set rngPaste = Nothing
Else
MsgBox "There are no duplicate items in the selected cells.",
vbInformation, "No Duplicates"
End If
Set dic = Nothing
End If
End Sub
--
HTH...

Jim Thomlinson


"ExcelMonkey" wrote:

I have multiple columns say 5 of them (1 every two columns starting in column
B and ending in column J). Each column has varying rows of string data. I
want to find the duplicate data across the columns and summarize in another
column 2 away from column J (column L). Hence column L will only show the
duplicate data found in the the 5 other columns. Note that there will not be
duplicate data within any one column. The duplicate data will only exist
across columns.

I am assuming that the best way to do this is to create a VBA 2D array,
define it by the colunn with the most rows of data and then load each column
into the array and search for duplicates from there. I am assuming that I
would then store the duplicates in another array and then dump the results to
column L.

Any insight on how to do this efficiently?

Thanks

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
Searching on 2 columns and putting result in third column. RobFJ[_3_] Excel Worksheet Functions 2 April 14th 09 04:32 PM
Print multiple columns from single column data? Bobinalong Excel Discussion (Misc queries) 1 August 27th 08 02:40 AM
Instructions on searching workbook for duplicates in Excel TJ@HB Excel Discussion (Misc queries) 0 October 30th 06 04:55 PM
How do I print a single column of data in two columns on one page Neal Excel Discussion (Misc queries) 1 January 8th 06 11:31 PM
print 3 column range in six columns dawgpilot Excel Discussion (Misc queries) 3 April 28th 05 10:53 PM


All times are GMT +1. The time now is 09:28 AM.

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

About Us

"It's about Microsoft Excel"