+ Reply to Thread
Results 1 to 6 of 6

Convert data (a bit like a FOREACH?)

Hybrid View

  1. #1
    Registered User
    Join Date
    03-03-2016
    Location
    London
    MS-Off Ver
    15
    Posts
    8

    Red face Convert data (a bit like a FOREACH?)

    Hello,

    Keen Excel user here trying to do something a little bit out of his skill set!

    I have a spreadsheet with the info below. Every person will sign up for swimming (23442) but they can then optionally sign up for UP TO 7 other sports. If they have signed up for something else then there will be another entry in the spreadsheet.

    event	userid	username	firstname	lastname
    23442	651	philly	Phil	Smith
    23442	673	wibblewo	David	James
    23442	876	slimtony	Tony	Wilson
    23442	1617	fatboy	Dave	Dee
    29033	1617	fatboy	Dave	Dee
    28985	1617	fatboy	Dave	Dee
    23442	1877	alanantic	Alan	Bob
    28973	1877	alanantic	Alan	Bob
    28838	1877	alanantic	Alan	Bob
    23442	2074	jamesbob	Jayme	Mayer
    23442	9944	Chester	Will	Black
    29201	9944	Chester	Will	Black
    28976	9944	Chester	Will	Black
    29273	9944	Chester	Will	Black
    I need to change the above so that each person is only on one line - something like:

    userid	username	firstname	lastname	event codes
    651	philly	Phil	Smith	23442
    673	wibblewo	David	James	23442
    876	slimtony	Tony	Wilson	23442
    1617	fatboy	Dave	Dee	23442, 29033, 28985
    1877	alanantic	Alan	Bob	23442, 28973, 28838
    2074	jamesbob	Jayme	Mayer	23442
    9944	Chester	Will	Black	23442, 29201, 28976, 29273
    Any help gratefully appreciated! I'm certain I can do it but it's driving me bonkers!

    Thanks
    Attached Files Attached Files

  2. #2
    Forum Expert
    Join Date
    08-28-2014
    Location
    Texas, USA
    MS-Off Ver
    2016
    Posts
    1,796

    Re: Convert data (a bit like a FOREACH?)

    If you want the event names added, you're going to need to provide a complete event id / event name lookup table. This code does not add event names for that reason.

    Sub simonp820()
    
    Set ws1 = Sheets("query_result")
    Set ws2 = Sheets.Add(After:=Sheets(Sheets.Count))
    ws2.Name = "output " & Sheets.Count
    
    ws1.[B1:E1].Copy ws2.[A1]
    ws2.[E1].Value = "event codes"
    uid = ""
    r = 1
    
    For Each c In ws1.Columns(2).SpecialCells(2, 1)
        If c.Value <> uid Then
            r = r + 1
            Set d = c.Resize(, 4)
            d.Copy ws2.Cells(r, 1)
            ws2.Cells(r, 5) = c.Offset(0, -1).Value
            uid = c.Value
        Else
            ws2.Cells(r, 5).Value = ws2.Cells(r, 5).Value & ", " & c.Offset(0, -1).Value
        End If
    Next
    
    ws2.Columns.AutoFit
    
    End Sub
    I'm interested in starting a career working with VBA, if anyone knows of any opportunities!

  3. #3
    Registered User
    Join Date
    03-03-2016
    Location
    London
    MS-Off Ver
    15
    Posts
    8

    Re: Convert data (a bit like a FOREACH?)

    Oh walruseggman that's just awesome! I remember doing VB stuff years ago and that all makes sense - I've just never applied it to Excel and look forward to learning more.

    The code works great - thank you so much!

  4. #4
    Registered User
    Join Date
    03-03-2016
    Location
    London
    MS-Off Ver
    15
    Posts
    8

    Re: Convert data (a bit like a FOREACH?)

    OK I tried to do the event name lookup but failed miserably

    If I had another sheet ("events")with the event IDs/event names in, how would I alter the above code to replace the event IDs with the event name?

    So the "events" sheet would be simply:

    23442	swimming
    29033	football
    28985	snooker
    28973	netball
    28838	hockey
    29201	basketball
    28976	skating
    29273	tennis
    I've been reading about VLOOKUP but can't work out how to integrate it with the above code.

    Any help welcome!

  5. #5
    Forum Expert
    Join Date
    08-28-2014
    Location
    Texas, USA
    MS-Off Ver
    2016
    Posts
    1,796

    Re: Convert data (a bit like a FOREACH?)

    I'm more inclined to use Range.Find then Vlookup, if only because I'm more familiar with it. If you have a massive data set to search (like 100,000+ rows), then I've seen that Vlookup can be faster. Otherwise it doesn't really matter.

    Please note I've changed the sub as well as added the Function. Also assumes event id's are in column A on "events" sheet. If not, adjust Columns(1) in the function.


    Sub simonp820_2()
    
    Set ws1 = Sheets("query_result")
    Set ws2 = Sheets.Add(After:=Sheets(Sheets.Count))
    ws2.Name = "output " & Sheets.Count
    
    ws1.[B1:E1].Copy ws2.[A1]
    ws2.[E1].Value = "event codes"
    ws2.[F1].Value = "event names"
    uid = ""
    r = 1
    
    For Each c In ws1.Columns(2).SpecialCells(2, 1)
        If c.Value <> uid Then
            r = r + 1
            Set d = c.Resize(, 4)
            d.Copy ws2.Cells(r, 1)
            ws2.Cells(r, 5) = c.Offset(0, -1).Value
            uid = c.Value
            ws2.Cells(r, 6).Value = event_name(c.Offset(0, -1).Value, "")
        Else
            ws2.Cells(r, 5).Value = ws2.Cells(r, 5).Value & ", " & c.Offset(0, -1).Value
            ws2.Cells(r, 6).Value = ws2.Cells(r, 6).Value & event_name(c.Offset(0, -1).Value, ", ")
        End If
        
    Next
    
    ws2.Columns.AutoFit
    
    End Sub
    
    Function event_name(eid As String, spacer As String) As String
        Set c = Sheets("events").Columns(1).Find(eid)
        If Not c Is Nothing Then event_name = spacer & c.Offset(0, 1).Value
    End Function
    Last edited by walruseggman; 03-04-2016 at 01:40 PM.

  6. #6
    Registered User
    Join Date
    03-03-2016
    Location
    London
    MS-Off Ver
    15
    Posts
    8

    Re: Convert data (a bit like a FOREACH?)

    Thanks again walruseggman - just perfect

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Replies: 3
    Last Post: 09-15-2015, 06:07 AM
  2. Excel 2007 Sumproduct foreach day in column?
    By jrider64 in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 07-29-2015, 02:27 PM
  3. VBA Code convert data to tab delimited and write data to text file | Not Working
    By lbl159 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-07-2015, 06:40 AM
  4. Foreach Name in a List Paste a Template of Cells
    By Codeman553 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-03-2013, 10:25 AM
  5. [SOLVED] Foreach loop lookup between two worksheets
    By asparak in forum Excel General
    Replies: 7
    Last Post: 07-14-2012, 05:03 AM
  6. Replies: 2
    Last Post: 06-25-2012, 12:24 PM
  7. [SOLVED] ForEach statement problem
    By excelnut1954 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-31-2006, 05:40 PM

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