Avatar of Slow Learner
Slow Learner
 asked on

Having trouble with Excel VBA Select Case Transpose

Hello

I am mildly experienced with Excel and have been trying to pick up some VBA knowledge.  I have a workbook with 2 sheets, on sheet 1 i have big data set table and on sheet 2 I have a list of ID's and Type (String).  The ID's from Sheet 2 correspond with a row in Sheet 1, the ID field is a primary key.  So on Sheet 1 there is only one instance of a particular ID, and on sheet 2 ID could be listed multiple times depending to the number of types associated with that ID.

I am trying to create a vba solution to go through list of ID's on sheet 2 and match with sheet1 ID and then add a column or columns that would list the TYPES from Sheet 2 list.  

The end result would be the unique ID on Sheet1 would then have all the TYPES associated with it listed horizontally after the other columns corresponding to the unique ID.

Hope I worded that right, any help would be appreciated.
Microsoft ExcelVisual Basic ClassicVBA

Avatar of undefined
Last Comment
Martin Liss

8/22/2022 - Mon
Saurabh Singh Teotia

It will be really helpful if you post your some sample data with before and after as it will be easy to design a solution for you post then...

Saurabh...
Slow Learner

ASKER
Thank you for the reply Saurabh.  I have put together simple sample cause of data privacy stuff.  But this sample file sums it up.  Actual file has 168 more columns and few hundred thousand rows but I think the idea is same with sample.  

Thanks again, I appreciate the help.
sample.xls
ASKER CERTIFIED SOLUTION
Saurabh Singh Teotia

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Slow Learner

ASKER
Thank you Saurabh, it works right on the sample data, but in my actual worksheet, if error listed for ID, it places all errors for that ID on same row, but the last column keeps going out.  I have reattached the sample and tried to depict what is happening.

So when I ran the code, after 5 minutes, I applied break, and at that point it was on column1313.
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
Saurabh Singh Teotia

Your workbook is missing..can you add so that i can look what's happening and fix it accordingly...
Saurabh Singh Teotia

I guess you meant to say when the code exists in sheet2 and didn't exist on sheet1 it used to give an error or didn't work properly..Their you go i fixed that for you..try this...

Sub getdata()
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False

    Dim ws As Worksheet, ws1 As Worksheet

    Set ws = Sheets("Sheet2")
    Set ws1 = Sheets("Sheet1")

    Dim rng As Range, cell As Range, lrow As Long, r1 As Range
    Dim lcol As Long, r As Range, lr As Long, str As String

    lrow = ws.Cells(Cells.Rows.Count, "a").End(xlUp).Row
    Set rng = ws.Range("A2:A" & lrow)

    lr = ws1.Cells(Cells.Rows.Count, "a").End(xlUp).Row
    Set r = ws1.Range("A2:A" & lr)

    For Each cell In rng
        If Trim(cell.Value) <> "" Then
            str = cell.Value
            If Application.WorksheetFunction.CountIf(r, cell.Value) > 0 Then
                Set r1 = r.Find(what:=str, after:=ws1.Cells(2, 1), LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
                lcol = Cells(r1.Row, Cells.Columns.Count).End(xlToLeft).Column + 1
                ws1.Cells(r1.Row, lcol).Value = cell.Offset(0, 1).Value

            End If

        Else
            cell.EntireRow.Interior.ColorIndex = 3
        End If

    Next cell
    ws1.Cells.EntireColumn.AutoFit

    Application.ScreenUpdating = True
    Application.DisplayAlerts = True


End Sub

Open in new window


If this still doesn't work then can you please upload your workbook where it's going messy..

Saurabh...
Slow Learner

ASKER
Here is the sample file with the results I am getting in my actual workbook.  Sorry I forgot to attach.
sample--1-.xls
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Saurabh Singh Teotia

Not sure what you are doing it works correctly for me..Enclosed is the code and workbook for your reference...

Saurabh...

Sub getdata()
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False

    Dim ws As Worksheet, ws1 As Worksheet

    Set ws = Sheets("Sheet2")
    Set ws1 = Sheets("Sheet1")

    Dim rng As Range, cell As Range, lrow As Long, r1 As Range
    Dim lcol As Long, r As Range, lr As Long, str As String

    lrow = ws.Cells(Cells.Rows.Count, "a").End(xlUp).Row
    Set rng = ws.Range("A2:A" & lrow)

    lr = ws1.Cells(Cells.Rows.Count, "a").End(xlUp).Row
    Set r = ws1.Range("A2:A" & lr)

    ws1.Select

    For Each cell In rng
        If Trim(cell.Value) <> "" Then
            str = cell.Value
            If Application.WorksheetFunction.CountIf(r, cell.Value) > 0 Then
                Set r1 = r.Find(what:=str, after:=ws1.Cells(2, 1), LookIn:=xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
                lcol = Cells(r1.Row, Cells.Columns.Count).End(xlToLeft).Column + 1
                ws1.Cells(r1.Row, lcol).Value = cell.Offset(0, 1).Value

            End If

        Else
            cell.EntireRow.Interior.ColorIndex = 3
        End If

    Next cell
    ws1.Cells.EntireColumn.AutoFit

    Application.ScreenUpdating = True
    Application.DisplayAlerts = True


End Sub

Open in new window


Saurabh...
sample--1-.xls
Slow Learner

ASKER
Thanks again.   Don't know what is going on with my file...but I didn't give up and When I applied your code to a copy of my actual workbook I decided to remove all formatting from both sheets and that cleared it up.

Thank you,
SL
Saurabh Singh Teotia

So I'm assuming this is working perfectly for you now..
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
Martin Liss

This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.