Create a new column for each unique item in a list with VBA?

teamswank

New Member
Joined
Dec 3, 2020
Messages
1
Office Version
  1. 365
Platform
  1. Windows
Hi -

I have a list of partnerships like this on Sheet2:
1607042392114.png


For each uniquely named partnership, I would like it to take that name and copy a "template" column I will provide but with the partnership name as the header in a table on Sheet1. My table looks like this:
1607042586674.png


So that in the end, each partnership would represent a column on my worksheet.

This seems like it would be a common request, but I have looked online, but cannot seem to find anything that mirrors it. Please point me in that direction if you do.

Thank you.
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Hi teamswank,

Welcome to MrExcel!!

Try this:

VBA Code:
Option Explicit
Sub Macro1()

    Dim wsSource As Worksheet, wsOutput As Worksheet
    Dim lngMyRow As Long, lngLastRow As Long, lngMyCounter As Long
    Dim lngOutputCol As Long
    Dim clnUniquePshp As New Collection
   
    Application.ScreenUpdating = False
   
    Set wsSource = ThisWorkbook.Sheets("Sheet2")
    Set wsOutput = ThisWorkbook.Sheets("Sheet1")
    lngLastRow = wsSource.Cells(Rows.Count, "A").End(xlUp).Row
    lngOutputCol = 3 'Initial output column (C in this case). Change to suit.
   
    For lngMyRow = 2 To lngLastRow
        On Error Resume Next
            clnUniquePshp.Add wsSource.Range("A" & lngMyRow), CStr(wsSource.Range("A" & lngMyRow)) 'Assumes the partnership names are in Col. A of 'wsSource'. Change to suit.
            If Err.Number = 0 Then
                wsOutput.Cells(1, lngOutputCol).Value = wsSource.Range("A" & lngMyRow)
                lngOutputCol = lngOutputCol + 1
                lngMyCounter = lngMyCounter + 1
            End If
        On Error GoTo 0
    Next lngMyRow

    Application.ScreenUpdating = True
   
    If lngMyCounter = 0 Then
        MsgBox "There were no unique partnerships found in """ & wsSource.Name & """ to be copied to """ & wsOutput.Name & ".", vbExclamation
    Else
        MsgBox Format(lngMyCounter, "#,##0") & " unique partnership(s) have now been copied from """ & wsSource.Name & """ to""" & wsOutput.Name & ".", vbInformation
    End If

End Sub

Regards,

Robert
 
Upvote 0
So that in the end, each partnership would represent a column on my worksheet.
So, do you mean the first name would go in that blue cell you have shown, second name in the cell to the right of the blue one etc?
If so, do you really need a macro? Could you just put this formula (check table name and column header name) into the blue cell of your template sheet?

BTW, I suggest that you investigate XL2BB for providing sample data to make it easier for helpers by not having to manually type out sample data to test with. Then you won't get simplified data like this?

teamswank.xlsm
C
1PshpName
2Name 1
3Name 1
4Name 1
5Name 1
6Name 1
7Name 2
8Name 2
9Name 3
10Name 3
11Name 3
12Name 3
13Name 3
14Name 3
Sheet2



teamswank.xlsm
BCDE
1Name 1Name 2Name 3
21
32
43
54a
64b
Sheet1
Cell Formulas
RangeFormula
C1:E1C1=TRANSPOSE(UNIQUE(Table1[PshpName]))
Dynamic array formulas.
 
Upvote 0

Forum statistics

Threads
1,214,643
Messages
6,120,707
Members
448,981
Latest member
recon11bucks

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top