+ Reply to Thread
Results 1 to 3 of 3

Change data layout

  1. #1
    Registered User
    Join Date
    11-23-2017
    Location
    Calgary, Canada
    MS-Off Ver
    2016
    Posts
    2

    Change data layout

    I currently have a three columns of data which I need to convert to a different layout using VBA.

    I need to go from this:

    Tag Drawing Drawing Type
    1 I1 IFC
    1 I2 IFC
    1 R1 Redline
    1 B1 Bootleg
    2 I3 IFC
    2 R2 Redline
    2 B2 Bootleg
    3 I4 IFC
    4 R3 Redline
    5 I5 IFC
    5 B3 Bootleg
    5 D1 Detail
    6 I6 IFC
    6 I7 IFC
    6 R4 Redline
    6 R5 Redline
    6 B4 Bootleg


    To this:

    Tag IFC IFC Redline Redline Bootleg Detail
    1 I1 I2 R1 B1
    2 I3 R2 B2
    3 I4
    4 R3
    5 I5 B3 D1
    6 I6 I7 R4 R5 B4


    The duplicates are removed from the "Tag" column, but if there are duplicates in the "Drawing Type" column for that tag, new columns need to be added to accommodate the different drawing numbers. Any help would be appreciated. Thanks

  2. #2
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: Change data layout

    You do not need a macro.

    Two formulas will do it.

    Enter these titles from E1 to K1: ag IFC IFC Redline Redline Bootleg Detail

    Enter this Formula into E2 and fill down until you get blank rows

    Formula: copy to clipboard
    Please Login or Register  to view this content.


    Enter this formula into F2 using ctrl shift enter. Then fill to Column K last row

    Formula: copy to clipboard
    Please Login or Register  to view this content.
    My General Rules if you want my help. Not aimed at any person in particular:

    1. Please Make Requests not demands, none of us get paid here.

    2. Check back on your post regularly. I will not return to a post after 4 days.
    If it is not important to you then it definitely is not important to me.

  3. #3
    Registered User
    Join Date
    11-23-2017
    Location
    Calgary, Canada
    MS-Off Ver
    2016
    Posts
    2

    Re: Change data layout

    All of the credit for this goes to MickG at Mr. Excel

    Sub MG24Nov23()
    Dim Dn As Range
    Dim Rng As Range
    Dim Dic As Object
    Dim Q As Variant
    Dim n As Long
    Dim Rw As Long
    Dim Ac As Long
    Dim k As Variant
    Dim p As Variant
    Dim c As Long
    Dim R As Range
    Dim col As Long

    Set Rng = Range(Range("A2"), Range("A" & Rows.Count).End(xlUp))
    Set Dic = CreateObject("Scripting.Dictionary")
    Dic.CompareMode = 1
    For Each Dn In Rng
    If Not Dic.Exists(Dn.Value) Then
    Set Dic(Dn.Value) = CreateObject("Scripting.Dictionary")
    End If

    If Not Dic(Dn.Value).Exists(Dn.Offset(, 2).Value) Then
    Dic(Dn.Value).Add (Dn.Offset(, 2).Value), Array(1, Dn)
    Else
    Q = Dic(Dn.Value).Item(Dn.Offset(, 2).Value)
    Q(0) = Q(0) + 1
    Set Q(1) = Union(Q(1), Dn)
    Dic(Dn.Value).Item(Dn.Offset(, 2).Value) = Q
    End If
    Next Dn

    With CreateObject("scripting.dictionary")
    .CompareMode = vbTextCompare
    For Each k In Dic.keys
    c = c + 1
    For Each p In Dic(k)
    If Not .Exists(p) Then
    .Add p, Array(Dic(k).Item(p)(0), 0)
    Else
    Q = .Item(p)
    Q(0) = Application.Max(Q(0), Dic(k).Item(p)(0))
    .Item(p) = Q
    End If
    Next p
    Next k
    c = 1
    For Each k In .keys
    For n = 1 To .Item(k)(0)
    c = c + 1
    Q = .Item(k)
    If Q(1) = 0 Then Q(1) = c
    .Item(k) = Q
    Next n
    Next k
    ReDim Ray(1 To Dic.Count + 1, 1 To c)
    Ray(1, 1) = "Tag"
    For Each k In Dic.keys
    Rw = Rw + 1
    For Each p In Dic(k)
    Ac = .Item(p)(1): col = 0
    For Each R In Dic(k).Item(p)(1)
    Ray(1, Ac + col) = p
    Ray(Rw + 1, 1) = k
    Ray(Rw + 1, Ac + col) = R.Offset(, 1).Value
    col = col + 1
    Next R
    Next p
    Next k
    End With
    With Sheets("Sheet2").Range("A1").Resize(UBound(Ray, 1), UBound(Ray, 2))
    .Value = Ray
    .Borders.Weight = 2
    .Columns.AutoFit
    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)

Similar Threads

  1. auto change the data layout
    By ids_73 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-22-2017, 03:28 PM
  2. Change the layout of the data set
    By h1981 in forum Excel General
    Replies: 1
    Last Post: 09-04-2014, 01:53 AM
  3. Data table layout change for mail merge
    By justinallsop in forum Excel General
    Replies: 7
    Last Post: 06-18-2013, 02:09 AM
  4. [SOLVED] Copy Data from one sheet to other to change data layout
    By rizmomin in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 11-29-2012, 07:38 PM
  5. Change the layout of data
    By ryanvaz in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-13-2012, 05:27 AM
  6. Script to change layout of data
    By maxoverrun in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-08-2011, 05:28 PM
  7. Change the layout of non-calculated data...
    By mhagele in forum Excel General
    Replies: 6
    Last Post: 01-18-2007, 10:36 AM

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