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.
Slow LearnerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Saurabh Singh TeotiaCommented:
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...
0
Slow LearnerAuthor Commented:
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
0
Saurabh Singh TeotiaCommented:
Use this code which will do what you are looking for..

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

            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

    Next cell
    ws1.Cells.EntireColumn.AutoFit

    Application.ScreenUpdating = True
    Application.DisplayAlerts = True


End Sub

Open in new window


Saurabh...
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

Slow LearnerAuthor Commented:
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.
0
Saurabh Singh TeotiaCommented:
Your workbook is missing..can you add so that i can look what's happening and fix it accordingly...
0
Saurabh Singh TeotiaCommented:
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...
0
Slow LearnerAuthor Commented:
Here is the sample file with the results I am getting in my actual workbook.  Sorry I forgot to attach.
sample--1-.xls
0
Saurabh Singh TeotiaCommented:
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
0
Slow LearnerAuthor Commented:
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
0
Saurabh Singh TeotiaCommented:
So I'm assuming this is working perfectly for you now..
0
Martin LissOlder than dirtCommented:
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.