+ Reply to Thread
Results 1 to 5 of 5

Macro: Remove Duplicate before Pasting Text (on selected cells)

Hybrid View

  1. #1
    Registered User
    Join Date
    06-05-2012
    Location
    Indonesia
    MS-Off Ver
    Excel 2007
    Posts
    18

    Macro: Remove Duplicate before Pasting Text (on selected cells)

    Hi all members,

    Using this macro, when you select several cells (using ctrl+click) , for example cell B12 + C13 + D16 (then run the macro), it will will copy values of cell I12, I13, I16. In otherword, no matter which cells you selected, it will always copy cell of column "I" (under that same row the cell was selected)

    (originally from a guy call Karan and modified by Alphafrog in this thread)
    Sub CopySelectedCells()
        Dim str As String, rangeRow As Range
        For Each rangeRow In Selection.Rows
            str = str & Cells(rangeRow.Row, "I").Value
            str = str & ","
        Next
        str = Left(str, Len(str) - 1) & vbCrLf
        
        With New MSForms.DataObject 'Early binding (requires reference to MSForms 2.0 Obect library)
        'Late binding (does not require reference)
        'With CreateObject("new:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")
            .SetText str
            .PutInClipboard
        End With
    End Sub

    Now the problem is, sometime the selected cells contain same values (duplicate)

    Can someone help modify this macro so that it will copy and remove duplicates value, if any. (the remove process should be done before pasting the data into other media, such as notepad)

    p.s: my knowledge in VB is way below basic

  2. #2
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,551

    Re: Macro: Remove Duplicate before Pasting Text (on selected cells)

    This should do it

    Sub CopySelectedCells()
     Dim str As String, rangeRow As Range
     With CreateObject("scripting.dictionary")
        For Each rangeRow In Selection.Rows
            If Trim$(Cells(rangeRow.Row, "I").Value) <> vbNullString Then
                If Not .exists(Trim$(Cells(rangeRow.Row, "I").Value)) Then
                    .Item(Trim$(Cells(rangeRow.Row, "I").Value)) = Trim$(Cells(rangeRow.Row, "I").Value)
                End If
            End If
        Next
        str = Join$(.items, ",")
     End With
        MsgBox str
    End Sub
    Thanks,
    Mike

    If you are satisfied with the solution(s) provided, please mark your thread as Solved.
    Select Thread Tools-> Mark thread as Solved.

  3. #3
    Registered User
    Join Date
    06-05-2012
    Location
    Indonesia
    MS-Off Ver
    Excel 2007
    Posts
    18

    Re: Macro: Remove Duplicate before Pasting Text (on selected cells)

    Dear Mike, tons and tons of thanks to you....it works just like i wish for.

    this is the final looks of the macro that i implement (see the blue comments)

    Sub CopySelectedCells()
     Dim str As String, rangeRow As Range
     With CreateObject("scripting.dictionary")
        For Each rangeRow In Selection.Rows
            If Trim$(Cells(rangeRow.Row, "I").Value) <> vbNullString Then
                If Not .Exists(Trim$(Cells(rangeRow.Row, "I").Value)) Then
                    .Item(Trim$(Cells(rangeRow.Row, "I").Value)) = Trim$(Cells(rangeRow.Row, "I").Value)
                End If
            End If
        Next
        str = Join$(.Items, ",")
     End With
    ''MsgBox str ( instead of poping up the msgbox, i prefer the data in the clipboard, so i changed back to the original )
        With New MSForms.DataObject
        .SetText str
        .PutInClipboard
    End Sub
    many thanks again mike

    regards,
    rampal

  4. #4
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,551

    Re: Macro: Remove Duplicate before Pasting Text (on selected cells)

    Why are you using?

        With New MSForms.DataObject
        .SetText str
        .PutInClipboard
    End Sub

  5. #5
    Registered User
    Join Date
    06-05-2012
    Location
    Indonesia
    MS-Off Ver
    Excel 2007
    Posts
    18

    Re: Macro: Remove Duplicate before Pasting Text (on selected cells)

    i prefer the data be putted in the clipboard (instead of in a pop-up msgbox) so it can be pasted into word or notepad
    that's why i changed back to the original

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1