+ Reply to Thread
Results 1 to 7 of 7

Transpose a single column to a single row with duplicates removed

Hybrid View

  1. #1
    Registered User
    Join Date
    03-03-2011
    Location
    Beaumont, TX
    MS-Off Ver
    Excel 2011 Mac
    Posts
    25

    Transpose a single column to a single row with duplicates removed

    I am new to VBA coding and am trying to write a VBA code to transpose a single column to a single row with duplicates removed. This length of the list can vary.

    In column B contains
    1
    1
    2
    3
    4
    3

    I would like to transpose starting at "F1" and the row would look like this
    F1 G1 H1
    1 2 3

    What VBA code can accomplish this?

  2. #2
    Forum Expert
    Join Date
    11-23-2005
    Location
    Rome
    MS-Off Ver
    Ms Office 2016
    Posts
    1,628

    Re: Transpose a single column to a single row with duplicates removed

    If you have your data in 'A' column (data has to start from second row) you could use this macro:
    Sub Macro1()
       Application.ScreenUpdating = False
       With ThisWorkbook.ActiveSheet
          .Columns("A:A").AdvancedFilter Action:=xlFilterCopy, _
             CopyToRange:=.Range("F1"), Unique:=True
          lastrow = .Cells(Rows.Count, "f").End(xlUp).Row
          .Range("f2:f" & lastrow).Copy
          .Range("g1").PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, _
             SkipBlanks:=False, Transpose:=True
          .Columns("f").Delete
       End With
       Application.ScreenUpdating = True
    End Sub
    Regards,
    Antonio

  3. #3
    Registered User
    Join Date
    03-03-2011
    Location
    Beaumont, TX
    MS-Off Ver
    Excel 2011 Mac
    Posts
    25

    Re: Transpose a single column to a single row with duplicates removed

    Works good. Only 1 slight problem for some reason it is not copying A1 cell. If the column A has values as such
    1
    2
    1
    3
    4

    It will transpose to 2 3 4 and leaves out 1.
    Last edited by ggilzow; 06-07-2013 at 10:41 AM. Reason: Remove extra value

  4. #4
    Forum Expert
    Join Date
    11-23-2005
    Location
    Rome
    MS-Off Ver
    Ms Office 2016
    Posts
    1,628

    Re: Transpose a single column to a single row with duplicates removed

    May be you need to delete previous data that are in f1,g1, etc.:
    Sub Macro1()
       Application.ScreenUpdating = False
       With ThisWorkbook.ActiveSheet
          .Range("a1").Offset(, 5).Resize(, Columns.Count - 5).ClearContents
          .Columns("A:A").AdvancedFilter Action:=xlFilterCopy, _
             CopyToRange:=.Range("F1"), Unique:=True
          lastrow = .Cells(Rows.Count, "f").End(xlUp).Row
          .Range("f2:f" & lastrow).Copy
          .Range("g1").PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, _
             SkipBlanks:=False, Transpose:=True
          .Columns("f").Delete
       End With
       Application.ScreenUpdating = True
    End Sub
    Regards,
    Antonio

  5. #5
    Forum Contributor Obsessed's Avatar
    Join Date
    05-22-2013
    Location
    Cincinnati, Ohio
    MS-Off Ver
    Excel 365
    Posts
    215

    Re: Transpose a single column to a single row with duplicates removed

    Try, Assuming column B:

    Sub TransposeUniqueValues()
    
    Dim LR As Long
    
    Columns("B").RemoveDuplicates Columns:=1, Header:=xlNo
    
    LR = Range("B" & Columns("B").Rows.Count).End(xlUp).Row
    
    Range("B1:B" & LR).Copy
    Range("F1").PasteSpecial Paste:=xlPasteValues, Transpose:=True
    
    End Sub

  6. #6
    Registered User
    Join Date
    03-03-2011
    Location
    Beaumont, TX
    MS-Off Ver
    Excel 2011 Mac
    Posts
    25

    Re: Transpose a single column to a single row with duplicates removed

    Thank you Obsessed this will work.

    Thank you Antonio for your help.


  7. #7
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Transpose a single column to a single row with duplicates removed

    Another one

    Sub mitranspose()
    Dim x, i&
    
    x = Range("B2").CurrentRegion
    
    With CreateObject("Scripting.Dictionary")
        .comparemode = 1
        For i = 1 To UBound(x)
            If Not .exists(x(i, 1)) Then .Item(x(i, 1)) = Empty
        Next i
        
        Range("F1").Resize(, .Count).Value = .Keys
    
    End With
    
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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