+ Reply to Thread
Results 1 to 15 of 15

VBA code to move data in columns by ID in a different column

Hybrid View

  1. #1
    Registered User
    Join Date
    03-29-2007
    MS-Off Ver
    365
    Posts
    70

    VBA code to move data in columns by ID in a different column

    Hi all,

    I have an Excel workbook with ID number in Column A. The same ID can repeat. There are other data in Column C, D and E. I am looking for a VBA code to move data in Column C, D E to the top row of each ID.

    like the photo attached.

    Excel.jpg

    appreciate any help and suggestion. Thank you so much.
    Last edited by tt388; 05-11-2024 at 08:36 PM.

  2. #2
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 2013
    Posts
    7,866

    Re: VBA code to move data in columns by ID in a different column

    Try:
    Sub MoveData()
        Application.ScreenUpdating = False
        Dim v As Variant, i As Long, dic As Object, x As Long: x = 2
        v = Range("A2", Range("A" & Rows.Count).End(xlUp)).Resize(, 4).Value
        Set dic = CreateObject("Scripting.Dictionary")
        For i = LBound(v) To UBound(v)
            If Not dic.exists(v(i, 1)) Then
                If v(i, 3) <> "" Or v(i, 4) <> "" Then
                    dic.Add v(i, 1), i + 2
                Else
                    dic.Add v(i, 1), i + 1
                End If
            Else
                If v(i, 3) <> "" Or v(i, 4) <> "" Then
                    Range("C" & dic(v(i, 1))).Resize(, 2) = Array(v(i, 3), v(i, 4))
                    Range("C" & i + 1).Resize(, 2).ClearContents
                    dic(v(i, 1)) = dic(v(i, 1)) + 1
                End If
            End If
        Next i
        Application.ScreenUpdating = True
    End Sub
    You can say "THANK YOU" for help received by clicking the Star symbol at the bottom left of the helper's post.
    Practice makes perfect. I'm very far from perfect so I'm still practising.

  3. #3
    Registered User
    Join Date
    03-29-2007
    MS-Off Ver
    365
    Posts
    70

    Re: VBA code to move data in columns by ID in a different column

    Thanks #2 and #3 for your help !

  4. #4
    Registered User
    Join Date
    03-29-2007
    MS-Off Ver
    365
    Posts
    70

    Re: VBA code to move data in columns by ID in a different column

    Quote Originally Posted by Mumps1 View Post
    Try:
    Sub MoveData()
        Application.ScreenUpdating = False
        Dim v As Variant, i As Long, dic As Object, x As Long: x = 2
        v = Range("A2", Range("A" & Rows.Count).End(xlUp)).Resize(, 4).Value
        Set dic = CreateObject("Scripting.Dictionary")
        For i = LBound(v) To UBound(v)
            If Not dic.exists(v(i, 1)) Then
                If v(i, 3) <> "" Or v(i, 4) <> "" Then
                    dic.Add v(i, 1), i + 2
                Else
                    dic.Add v(i, 1), i + 1
                End If
            Else
                If v(i, 3) <> "" Or v(i, 4) <> "" Then
                    Range("C" & dic(v(i, 1))).Resize(, 2) = Array(v(i, 3), v(i, 4))
                    Range("C" & i + 1).Resize(, 2).ClearContents
                    dic(v(i, 1)) = dic(v(i, 1)) + 1
                End If
            End If
        Next i
        Application.ScreenUpdating = True
    End Sub



    This code worked well for my spreadsheet.


    There are additional scenarios in my use case.

    1. Additional columns to move (ie. Column E, F, G....)
    2. If there are no data in column C, D but E

    Thank you so much for your help!
    Last edited by tt388; 05-10-2024 at 10:00 PM.

  5. #5
    Forum Expert
    Join Date
    11-28-2015
    Location
    indo
    MS-Off Ver
    2016 64 bitt
    Posts
    1,481

    Re: VBA code to move data in columns by ID in a different column

    Sub Test()
       Sub Test()
      Dim a, i As Long, ii As Long, n As Long, rw&, z As New Collection, b
      a = [a1].CurrentRegion
      Set v = CreateObject("System.collections.sortedlist")
      For i = 2 To UBound(a)
        On Error Resume Next
          z.Add Key:=CStr(a(i, 1)), Item:=CreateObject("System.collections.sortedlist")
           With z(CStr(a(i, 1)))
            .Item(IIf(a(i, 3) = "" And a(i, 4) = "", 100000, i)) = i
           End With
         On Error GoTo 0
      Next i
      n = 1
      ReDim b(1 To UBound(a), 1 To 4)
      b(1, 3) = a(1, 3): b(1, 4) = a(1, 4)
      For i = 2 To UBound(a, 1)
        b(i, 1) = a(i, 1)
        b(i, 2) = a(i, 2)
      Next i
      For i = 1 To z.Count
         For ii = 0 To z(i).Count - 1
             n = n + 1
             rw = z(i).getbyindex(ii)
             b(n, 3) = a(rw, 3): b(n, 4) = a(rw, 4)
         Next ii
         n = n + 1
      Next i
       [h1].Resize(UBound(a, 1), 4).Value = b
    End Sub
    Last edited by daboho; 05-10-2024 at 10:53 PM.
    "Presh Star Who has help you *For Add Reputation!! And mark case as Solve"

  6. #6
    Registered User
    Join Date
    03-29-2007
    MS-Off Ver
    365
    Posts
    70

    Re: VBA code to move data in columns by ID in a different column

    Quote Originally Posted by daboho View Post
    Sub Test()
      Sub Test()
      Dim a, i As Long, ii As Long, rw As Long, cl As Long, c As Long, z As New Collection
      a = [a1].CurrentRegion.Value
      For i = 2 To UBound(a)
        c = 0
        For ii = 3 To UBound(a, 2)
          If a(i, ii) = "" Then
            c = ii: Exit For
          End If
        Next ii
        
        On Error Resume Next
         If c > 0 Then z.Add Key:=a(i, 1), Item:=Array(i, c)
           If Err Then
             rw = z(a(i, 1))(0): cl = z(a(i, 1))(1)
             For ii = cl To UBound(a, 2)
               a(rw, ii) = a(i, ii): a(i, ii) = ""
             Next ii
               c = IIf(c < cl, c, cl)
               z.Remove a(i, 1)
               z.Add Key:=a(i, 1), Item:=Array(i, c)
             Err.Clear
           End If
        On Error GoTo 0
      Next i
      'Change [h1] to [a1] if want in same location
      [h1].Resize(UBound(a, 1), UBound(a, 2)).Value = a
    End Sub

    Thank you so much for your help, but the codes did not seem to work as the data did not move.

  7. #7
    Forum Expert
    Join Date
    11-28-2015
    Location
    indo
    MS-Off Ver
    2016 64 bitt
    Posts
    1,481

    Re: VBA code to move data in columns by ID in a different column

    try this only sort
     
    
      Option Explicit
      Sub Test()
      Dim a, i As Long, ii As Long, n As Long, rw&, z As New Collection, b
      a = [a1].CurrentRegion
      For i = 2 To UBound(a)
        On Error Resume Next
          z.Add Key:=CStr(a(i, 1)), Item:=CreateObject("System.collections.sortedlist")
           With z(CStr(a(i, 1)))
            .Item(IIf(a(i, 3) = "" And a(i, 4) = "", UBound(a, 1) + 1000, i)) = i
           End With
         On Error GoTo 0
      Next i
      n = 1
      ReDim b(1 To UBound(a), 1 To 4)
      b(1, 1) = a(1, 1): b(1, 2) = a(1, 2)
      b(1, 3) = a(1, 3): b(1, 4) = a(1, 4)
      For i = 2 To UBound(a, 1)
        b(i, 1) = a(i, 1)
        b(i, 2) = a(i, 2)
      Next i
      For i = 1 To z.Count
         For ii = 0 To z(i).Count - 1
             n = n + 1
             rw = z(i).getbyindex(ii)
             b(n, 3) = a(rw, 3): b(n, 4) = a(rw, 4)
         Next ii
         n = n + 1
      Next i
       [h1].Resize(UBound(b, 1), UBound(b, 2)).Value = b
    End Sub
    Attached Files Attached Files
    Last edited by daboho; 05-10-2024 at 11:11 PM.

  8. #8
    Registered User
    Join Date
    03-29-2007
    MS-Off Ver
    365
    Posts
    70

    Re: VBA code to move data in columns by ID in a different column

    Quote Originally Posted by daboho View Post
    try this only sort
    Sub Test()
       Sub Test()
      Dim a, i As Long, ii As Long, n As Long, rw&, z As New Collection, b
      a = [a1].CurrentRegion
      Set v = CreateObject("System.collections.sortedlist")
      For i = 2 To UBound(a)
        On Error Resume Next
          z.Add Key:=CStr(a(i, 1)), Item:=CreateObject("System.collections.sortedlist")
           With z(CStr(a(i, 1)))
            .Item(IIf(a(i, 3) = "" And a(i, 4) = "", 100000, i)) = i
           End With
         On Error GoTo 0
      Next i
      n = 1
      ReDim b(1 To UBound(a), 1 To 4)
      b(1, 3) = a(1, 3): b(1, 4) = a(1, 4)
      For i = 2 To UBound(a, 1)
        b(i, 1) = a(i, 1)
        b(i, 2) = a(i, 2)
      Next i
      For i = 1 To z.Count
         For ii = 0 To z(i).Count - 1
             n = n + 1
             rw = z(i).getbyindex(ii)
             b(n, 3) = a(rw, 3): b(n, 4) = a(rw, 4)
         Next ii
         n = n + 1
      Next i
       [h1].Resize(UBound(a, 1), 4).Value = b
    End Sub
    Thanks again, but a Run time error / Automation error was returned on
    Set v = CreateObject("System.collections.sortedlist")

  9. #9
    Forum Expert
    Join Date
    11-28-2015
    Location
    indo
    MS-Off Ver
    2016 64 bitt
    Posts
    1,481

    Re: VBA code to move data in columns by ID in a different column

    maybe you using option explicit,try again i has update code and workbook in post #7
    Last edited by daboho; 05-10-2024 at 11:12 PM.

  10. #10
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,636

    Re: VBA code to move data in columns by ID in a different column

    Sub test()
        Dim a, b, x, i As Long
        Application.ScreenUpdating = False
        With [a1].CurrentRegion
            With .Resize(.Rows.Count + 1).Columns(1)
                x = Filter(Evaluate("transpose(if(" & .Address & "<>" & _
                .Offset(1).Address & ",row(" & .Offset(1).Address & ")))"), False, 0)
            End With
            With .Columns("c").Resize(, .Columns.Count - 2)
                For i = 0 To UBound(x) - 1
                    If x(i + 1) - x(i) > 1 Then
                        With .Rows(x(i)).Resize(x(i + 1) - x(i))
                            b = Filter(Evaluate("transpose(if(countblank(offset(" & .Address & _
                            ",row(1:" & .Rows.Count & ")-1,,1," & .Columns.Count & "))<" & _
                            .Columns.Count & ",row(1:" & .Rows.Count & ")))"), False, 0)
                            If UBound(b) > -1 Then
                                a = Application.Index(.Value, Application.Transpose(b), _
                                    Application.Sequence(1, .Columns.Count, 1))
                                .ClearContents
                                .Resize(UBound(b) + 1) = a
                            End If
                        End With
                    End If
                Next
            End With
        End With
        Application.ScreenUpdating = True
    End Sub
    Last edited by jindon; 05-11-2024 at 07:50 AM.

  11. #11
    Registered User
    Join Date
    03-29-2007
    MS-Off Ver
    365
    Posts
    70

    Re: VBA code to move data in columns by ID in a different column

    Quote Originally Posted by jindon View Post
    Sub test()
        Dim a, b, x, i As Long
        Application.ScreenUpdating = False
        With [a1].CurrentRegion
            With .Resize(.Rows.Count + 1).Columns(1)
                x = Filter(Evaluate("transpose(if(" & .Address & "<>" & _
                .Offset(1).Address & ",row(" & .Offset(1).Address & ")))"), False, 0)
            End With
            With .Columns("c").Resize(, .Columns.Count - 2)
                For i = 0 To UBound(x) - 1
                    If x(i + 1) - x(i) > 1 Then
                        With .Rows(x(i)).Resize(x(i + 1) - x(i))
                            b = Filter(Evaluate("transpose(if(countblank(offset(" & .Address & _
                            ",row(1:" & .Rows.Count & ")-1,,1," & .Columns.Count & "))<" & _
                            .Columns.Count & ",row(1:" & .Rows.Count & ")))"), False, 0)
                            If UBound(b) > -1 Then
                                a = Application.Index(.Value, Application.Transpose(b), _
                                    Application.Sequence(1, .Columns.Count, 1))
                                .ClearContents
                                .Resize(UBound(b) + 1) = a
                            End If
                        End With
                    End If
                Next
            End With
        End With
        Application.ScreenUpdating = True
    End Sub
    This works perfectly. Thank you so much for your assistance !!

  12. #12
    Forum Expert
    Join Date
    11-28-2015
    Location
    indo
    MS-Off Ver
    2016 64 bitt
    Posts
    1,481

    Re: VBA code to move data in columns by ID in a different column

    for dinamic moving area, only change starCol from star col number to moving change 3 to your number col
      
    Option Explicit
    Const starCol As Long = 3 '>change this to number start moving column
    Sub Test()
    Dim a, i As Long, ii As Long, r As Range, n As Long, iii As Long, rw&, z As New Collection, b
      Set r = [a1].CurrentRegion
      a = r.Value
      For i = 1 To UBound(a)
        On Error Resume Next
           z.Add Key:=CStr(a(i, 1)), Item:=CreateObject("System.collections.sortedlist")
           With z(CStr(a(i, 1)))
               n = IIf(WorksheetFunction.CountA(r.Offset(, starCol - 1).Resize(, UBound(a, 2) - starCol + 1).Rows(i)) > 0, i, UBound(a, 1) + i)
              .Item(n) = i
            End With
         On Error GoTo 0
      Next i
       ReDim b(1 To UBound(a, 1), 1 To UBound(a, 2))
       n = 0
       For i = 1 To z.Count
          For ii = 0 To z(i).Count - 1
             n = n + 1: rw = z(i).getbyindex(ii)
             For iii = 1 To starCol - 1
                 b(n, iii) = a(n, iii)
             Next iii
             For iii = starCol To UBound(a, 2)
                 b(n, iii) = a(rw, iii)
             Next iii
          Next ii
       Next i
       [H1].Resize(UBound(b, 1), UBound(b, 2)).Value = b
    End Sub
    Attached Files Attached Files
    Last edited by daboho; 05-11-2024 at 05:45 AM.

  13. #13
    Valued Forum Contributor
    Join Date
    02-02-2016
    Location
    Indonesia
    MS-Off Ver
    Office 365
    Posts
    1,015

    Re: VBA code to move data in columns by ID in a different column

    There are additional scenarios in my use case.

    1. Additional columns to move (ie. Column E, F, G....)
    2. If there are no data in column C, D but E
    Another option to try:
    Sub tt388_1()
    Dim i As Long, j As Long, h As Long, n As Long, k As Long, p As Long, q As Long
    Dim va, vb, vc
    n = Range("A" & Rows.Count).End(xlUp).Row
    q = Cells(1, Columns.Count).End(xlToLeft).Column - 2
    va = Range("A1:A" & n + 1)
    vb = Range("C1:C" & n).Resize(, q)
    ReDim vc(1 To UBound(vb, 1), 1 To q)
    For i = 1 To n
        j = i
        Do While va(i, 1) = va(i + 1, 1)
            i = i + 1
        Loop
        h = j
        For k = j To i
            If Len(Join(Application.Index(vb, k), "")) > 0 Then
                For p = 1 To q
                    vc(h, p) = vb(k, p)
                Next
                h = h + 1
            End If
        Next
    Next
    Range("C1").Resize(n, q) = vc
    End Sub

  14. #14
    Registered User
    Join Date
    03-29-2007
    MS-Off Ver
    365
    Posts
    70

    Re: VBA code to move data in columns by ID in a different column

    Quote Originally Posted by Akuini View Post
    Another option to try:
    Sub tt388_1()
    Dim i As Long, j As Long, h As Long, n As Long, k As Long, p As Long, q As Long
    Dim va, vb, vc
    n = Range("A" & Rows.Count).End(xlUp).Row
    q = Cells(1, Columns.Count).End(xlToLeft).Column - 2
    va = Range("A1:A" & n + 1)
    vb = Range("C1:C" & n).Resize(, q)
    ReDim vc(1 To UBound(vb, 1), 1 To q)
    For i = 1 To n
        j = i
        Do While va(i, 1) = va(i + 1, 1)
            i = i + 1
        Loop
        h = j
        For k = j To i
            If Len(Join(Application.Index(vb, k), "")) > 0 Then
                For p = 1 To q
                    vc(h, p) = vb(k, p)
                Next
                h = h + 1
            End If
        Next
    Next
    Range("C1").Resize(n, q) = vc
    End Sub

    This works the best and it can be customized easily ! Thank you so much for your input !

  15. #15
    Valued Forum Contributor
    Join Date
    02-02-2016
    Location
    Indonesia
    MS-Off Ver
    Office 365
    Posts
    1,015

    Re: VBA code to move data in columns by ID in a different column

    You're welcome, glad we could help

+ 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: 0
    Last Post: 01-12-2024, 11:01 AM
  2. Move Data from Columns on Different Sheets to one column
    By irajeev in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 10-24-2016, 08:14 PM
  3. Replies: 12
    Last Post: 03-14-2016, 08:42 AM
  4. [SOLVED] Convert column B as multiple column titles and move data in column C into new columns?
    By princesscathryn in forum Excel - New Users/Basics
    Replies: 8
    Last Post: 06-30-2014, 07:31 PM
  5. [SOLVED] Move data from Column A-E or B-F to columns G-K
    By netnewbie in forum Excel Programming / VBA / Macros
    Replies: 18
    Last Post: 10-01-2013, 10:05 AM
  6. [SOLVED] How to move one Column's data to other Columns...
    By LeoThe Lion in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-25-2012, 05:11 AM
  7. Move Data from one column to many columns
    By Barb in forum Excel General
    Replies: 3
    Last Post: 01-16-2006, 01:00 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