I am using Excel 2003. I am working with a list of names of various people,
in one column. I need to have this list reproduced on a blank worksheetwith
the repeated names removed.
for instance, the list I'm working with would be something like:
Pat
Pat
Dan
Marie
Marie
Sharron
Sharron
Sharron
Sharron
Daniel
Mark
Mark
Mark
Mark
...and I would need this list to be reproduced on another tab as:
Pat
Dan
Marie
Sharron
Daniel
Mark
Is there a way I can do this?
Yes, there is a way!
Paste this into a new module and hit F5:
'Code
Option Explicit
Public Sub CountDuplicates()
'Declarations
Dim strCellText() As String
Dim strCellUnique() As String
Dim Cell As Range
Dim iCounter As Integer
Dim jCounter As Integer
Dim iNumCells As Integer
Dim iNumDups As Integer
Dim MSG As String
Dim bnDup As Boolean
Dim strSheetName As String
Dim strNewName As String
'Get array of all unique values
iCounter = 1
For Each Cell In Selection
bnDup = False
ReDim Preserve strCellText(iCounter)
strCellText(iCounter) = Cell
For jCounter = 1 To iNumCells
If strCellText(iCounter) = strCellText(jCounter) Then
bnDup = True
End If
Next jCounter
If bnDup = False Then
iNumCells = iNumCells + 1
ReDim Preserve strCellUnique(iNumCells)
strCellUnique(iNumCells) = Cell
End If
iCounter = iCounter + 1
Next Cell
'Get sheet names
strSheetName = ActiveWorkbook.ActiveSheet.Name
strNewName = "NewSheet" & CStr(ActiveWorkbook.Worksheets.Count)
'See if sheet exists, create if it doesn't
If WorksheetExists(strNewName, ActiveWorkbook) Then
Call MsgBox("Rename sheet " & strNewName & ".", vbOKOnly,
"Error")
Exit Sub
Else
ActiveWorkbook.Worksheets.Add.Name = strNewName
Sheets(strNewName).Move
After:=Sheets(ActiveWorkbook.Worksheets.Count)
End If
'Copy and paste
Sheets(strNewName).Activate
For iCounter = 1 To iNumCells
Cells(iCounter, 1) = strCellUnique(iCounter)
Next iCounter
End Sub
Function WorksheetExists(SheetName As String, Optional WhichBook As
Workbook) As Boolean
'from Chip Pearson
Dim WB As Workbook
Set WB = IIf(WhichBook Is Nothing, ThisWorkbook, WhichBook)
On Error Resume Next
WorksheetExists = CBool(Len(WB.Worksheets(SheetName).Name) > 0)
End Function
'End of code
HTH
Chris