I did find a most beautiful macro somewhere in the net making it possible for me to just make a selection and
it would make the unique values in the selection go to the clipboard sorted
It worked fine in Excel 2010 but stopped working in Excel 2014.
I get no sensible output , just
Could someone please help me adjust it to Excel 2014?
It´s a most useful macro, could be of value for many I believe
Sub Unika_till_clipboard()
Dim no_dupes_coll As New Collection
Dim i As Long
Dim MyArray()
Dim iRow As Integer
Dim szTmp As String
Dim MyDataObj As New DataObject
Dim Cell
Dim j
Dim Swap1
Dim Swap2
Dim iCol
'Gör alla celler till text
Selection.TextToColumns Destination:=ActiveCell, DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
Semicolon:=False, Comma:=False, Space:=False, Other:=False, FieldInfo _
:=Array(1, 2), TrailingMinusNumbers:=True
' Selection.SpecialCells(xlCellTypeVisible).Select
' For Each c In Selection.Cells
'Lägga till unika värden i collection
Selection.SpecialCells(xlCellTypeVisible).Select
For Each Cell In Selection.Cells
'For Each cell In Selection
'attempting to add a duplicate key causes an error, so ignore
'and the duplicate will not be added
On Error Resume Next
no_dupes_coll.Add Item:=Cell.Value, key:=Cell.Value
On Error GoTo 0
Next Cell
'Sort the collection (optional)
For i = 1 To no_dupes_coll.Count - 1
For j = i + 1 To no_dupes_coll.Count
If no_dupes_coll(i) > no_dupes_coll(j) Then
Swap1 = no_dupes_coll(i)
Swap2 = no_dupes_coll(j)
no_dupes_coll.Add Swap1, before:=j
no_dupes_coll.Add Swap2, before:=i
no_dupes_coll.Remove i + 1
no_dupes_coll.Remove j + 1
End If
Next j
Next i
'Collection to myArray
If no_dupes_coll.Count > 0 Then
ReDim MyArray(1 To no_dupes_coll.Count)
For i = 1 To no_dupes_coll.Count
MyArray(i) = no_dupes_coll(i)
'Debug.Print MyArray(i)
'delete above line and do your processing here
'Cells(2, 5).Value = myArray(i)
Next i
End If
'Sätta värden i clipboard
For iRow = 1 To no_dupes_coll.Count
For iCol = 1 To 1
szTmp = szTmp & CStr(iCol) & vbTab
Next iCol
szTmp = szTmp & CStr(MyArray(iRow)) & vbCrLf
Next iRow
MyDataObj.SetText szTmp
MyDataObj.PutInClipboard
'clear the collection
Do While no_dupes_coll.Count > 0
no_dupes_coll.Remove 1
Loop
End Sub
it would make the unique values in the selection go to the clipboard sorted
It worked fine in Excel 2010 but stopped working in Excel 2014.
I get no sensible output , just
Could someone please help me adjust it to Excel 2014?
It´s a most useful macro, could be of value for many I believe
Sub Unika_till_clipboard()
Dim no_dupes_coll As New Collection
Dim i As Long
Dim MyArray()
Dim iRow As Integer
Dim szTmp As String
Dim MyDataObj As New DataObject
Dim Cell
Dim j
Dim Swap1
Dim Swap2
Dim iCol
'Gör alla celler till text
Selection.TextToColumns Destination:=ActiveCell, DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
Semicolon:=False, Comma:=False, Space:=False, Other:=False, FieldInfo _
:=Array(1, 2), TrailingMinusNumbers:=True
' Selection.SpecialCells(xlCellTypeVisible).Select
' For Each c In Selection.Cells
'Lägga till unika värden i collection
Selection.SpecialCells(xlCellTypeVisible).Select
For Each Cell In Selection.Cells
'For Each cell In Selection
'attempting to add a duplicate key causes an error, so ignore
'and the duplicate will not be added
On Error Resume Next
no_dupes_coll.Add Item:=Cell.Value, key:=Cell.Value
On Error GoTo 0
Next Cell
'Sort the collection (optional)
For i = 1 To no_dupes_coll.Count - 1
For j = i + 1 To no_dupes_coll.Count
If no_dupes_coll(i) > no_dupes_coll(j) Then
Swap1 = no_dupes_coll(i)
Swap2 = no_dupes_coll(j)
no_dupes_coll.Add Swap1, before:=j
no_dupes_coll.Add Swap2, before:=i
no_dupes_coll.Remove i + 1
no_dupes_coll.Remove j + 1
End If
Next j
Next i
'Collection to myArray
If no_dupes_coll.Count > 0 Then
ReDim MyArray(1 To no_dupes_coll.Count)
For i = 1 To no_dupes_coll.Count
MyArray(i) = no_dupes_coll(i)
'Debug.Print MyArray(i)
'delete above line and do your processing here
'Cells(2, 5).Value = myArray(i)
Next i
End If
'Sätta värden i clipboard
For iRow = 1 To no_dupes_coll.Count
For iCol = 1 To 1
szTmp = szTmp & CStr(iCol) & vbTab
Next iCol
szTmp = szTmp & CStr(MyArray(iRow)) & vbCrLf
Next iRow
MyDataObj.SetText szTmp
MyDataObj.PutInClipboard
'clear the collection
Do While no_dupes_coll.Count > 0
no_dupes_coll.Remove 1
Loop
End Sub