Combining Data from Multiple Rows

Dratchsky

New Member
Joined
Apr 24, 2021
Messages
11
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
  2. Web
Good Morning,

I am looking for a solution to quickly help fill rows based on their shared Value in Column D

I have several group id (Column D) that I need to have go from this

Book7
ABCDEFGHIJKLMNOPQRSTUVWX
1id_numlast_namefirst_nameGroupIDmobile_phoneemail_addressRoom 1 IDRoom 1 LRoom 1 FGroupIDRoom 1 PhoneRoom 1 EmailRoom 2 ID NumberRoom 2 LRoom 2 FGroupIDRoom 2 PhoneRoom 2 EmailRoom 3 IDRoom 3 LRoom 3 FGroupIDRoom 3 PhoneRoom 3 Email
2519254Last1First1GH106(1) 234-5678Last1.First1@test.com
3503820Last2First2GH106(1) 234-5678Last2.First2@test.com
4503854Last3First3GH106(1) 234-5678Last3.First3@test.com
5510131Last4First4GH108(1) 234-5678Last4.First4@test.com
6481242Last5First5GH118(1) 234-5678Last5.First5@test.com
7474673Last6First6GH118(1) 234-5678Last6.First6@test.com
8506636Last7First7GH118(1) 234-5678Last7.First7@test.com
9507597Last8First8GH118(1) 234-5678Last8.First8@test.com
Sheet1


To This:
Book6
ABCDEFGHIJKLMNOPQRSTUVWX
1id_numlast_namefirst_nameGroupIDmobile_phoneemail_addressRoom 1 IDRoom 1 LRoom 1 FGroupIDRoom 1 PhoneRoom 1 EmailRoom 2 ID NumberRoom 2 LRoom 2 FGroupIDRoom 2 PhoneRoom 2 EmailRoom 3 IDRoom 3 LRoom 3 FGroupIDRoom 3 PhoneRoom 3 Email
2519254Last1First1GH106(1) 234-5678Last1.First1@test.com503820Last2First2GH106(1) 234-5678Last2.First2@test.com503854Last3First3GH106(1) 234-5678Last3.First3@test.com
3503820Last2First2GH106(1) 234-5678Last2.First2@test.com519254Last1First1GH106(1) 234-5678Last1.First1@test.com503854Last3First3GH106(1) 234-5678Last3.First3@test.com
4503854Last3First3GH106(1) 234-5678Last3.First3@test.com519254Last1First1GH106(1) 234-5678Last1.First1@test.com503820Last2First2GH106(1) 234-5678Last2.First2@test.com
5510131Last4First4GH108(1) 234-5678Last4.First4@test.com
6481242Last5First5GH118(1) 234-5678Last5.First5@test.com474673Last6First6GH118(1) 234-5678Last6.First6@test.com506636Last7First7GH118(1) 234-5678Last7.First7@test.com507597Last8First8GH118(1) 234-5678Last8.First8@test.com
7474673Last6First6GH118(1) 234-5678Last6.First6@test.com481242Last5First5GH118(1) 234-5678Last5.First5@test.com506636Last7First7GH118(1) 234-5678Last7.First7@test.com507597Last8First8GH118(1) 234-5678Last8.First8@test.com
8506636Last7First7GH118(1) 234-5678Last7.First7@test.com481242Last5First5GH118(1) 234-5678Last5.First5@test.com474673Last6First6GH118(1) 234-5678Last6.First6@test.com507597Last8First8GH118(1) 234-5678Last8.First8@test.com
9507597Last8First8GH118(1) 234-5678Last8.First8@test.com481242Last5First5GH118(1) 234-5678Last5.First5@test.com474673Last6First6GH118(1) 234-5678Last6.First6@test.com506636Last7First7GH118(1) 234-5678Last7.First7@test.com
Sheet1
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Try this:

VBA Code:
Sub Combining_Data()
  Dim a As Variant, b As Variant
  Dim dic As Object
  Dim i As Long, j As Long, k As Long, m As Long, n As Long
  Dim rws As Variant, ky As Variant
  
  a = Range("A2", Range("F" & Rows.Count).End(3)).Value
  ReDim b(1 To UBound(a), 1 To UBound(a) * UBound(a))
  Set dic = CreateObject("Scripting.Dictionary")
  
  For i = 1 To UBound(a)
    If Not dic.exists(a(i, 4)) Then
      dic(a(i, 4)) = i
    Else
      dic(a(i, 4)) = dic(a(i, 4)) & "|" & i
    End If
  Next
  
  For Each ky In dic.keys
    rws = Split(dic(ky), "|")
    For i = 0 To UBound(rws)
      n = 1
      m = rws(i)
      For j = 0 To UBound(rws)
        If j <> i Then
          For k = 1 To 6
            b(m, n) = a(rws(j), k)
            n = n + 1
          Next k
        End If
      Next j
    Next i
  Next
  Range("G2").Resize(UBound(b, 1), UBound(b, 2)).Value = b
End Sub
 
Upvote 0
Use this version with the code a bit simplified.

VBA Code:
Sub Combining_Data()
  Dim dic As Object
  Dim i As Long, j As Long, k As Long, n As Long
  Dim a As Variant, b As Variant, rws As Variant, it As Variant
  
  a = Range("A2", Range("F" & Rows.Count).End(3)).Value
  ReDim b(1 To UBound(a), 1 To UBound(a) * UBound(a))
  Set dic = CreateObject("Scripting.Dictionary")
  
  For i = 1 To UBound(a)
    dic(a(i, 4)) = dic(a(i, 4)) & "|" & i
  Next
  
  For Each it In dic.items
    rws = Split(it, "|")
    For i = 1 To UBound(rws)
      n = 1
      For j = 1 To UBound(rws)
        If j <> i Then
          For k = 1 To 6
            b(rws(i), n) = a(rws(j), k)
            n = n + 1
          Next k
        End If
      Next j
    Next i
  Next it
  Range("G2").Resize(UBound(b, 1), UBound(b, 2)).Value = b
End Sub
 
Upvote 0
Use this version with the code a bit simplified.

VBA Code:
Sub Combining_Data()
  Dim dic As Object
  Dim i As Long, j As Long, k As Long, n As Long
  Dim a As Variant, b As Variant, rws As Variant, it As Variant
 
  a = Range("A2", Range("F" & Rows.Count).End(3)).Value
  ReDim b(1 To UBound(a), 1 To UBound(a) * UBound(a))
  Set dic = CreateObject("Scripting.Dictionary")
 
  For i = 1 To UBound(a)
    dic(a(i, 4)) = dic(a(i, 4)) & "|" & i
  Next
 
  For Each it In dic.items
    rws = Split(it, "|")
    For i = 1 To UBound(rws)
      n = 1
      For j = 1 To UBound(rws)
        If j <> i Then
          For k = 1 To 6
            b(rws(i), n) = a(rws(j), k)
            n = n + 1
          Next k
        End If
      Next j
    Next i
  Next it
  Range("G2").Resize(UBound(b, 1), UBound(b, 2)).Value = b
End Sub
Good Morning unfortunately this hasn't worked I am receiving a run time error.
 
Upvote 0
It would help if you put the full error message.
Press the debug button and a line of the macro is highlighted in yellow, put here what that line is.
Also put here the data you are testing with. Since with the sample data you put it works fine.
 
Upvote 0
It would help if you put the full error message.
Press the debug button and a line of the macro is highlighted in yellow, put here what that line is.
Also put here the data you are testing with. Since with the sample data you put it works fine.
1626187691933.png
 
Upvote 0
Press the OK button and check if any line in the macro is highlighted.
Did you copy the whole macro?
Did you modify something in the macro?
 
Upvote 0
Press the OK button and check if any line in the macro is highlighted.
Did you copy the whole macro?
Did you modify something in the macro?
I did not modify anything and unfortunately nothing is highlighted after hitting ok
 
Upvote 0
You could upload a copy of your file with the macro to a free site such google drive. Once you do that, mark it for 'Sharing' and you will be given a link to the file that you can post here. If the workbook contains confidential information, you could replace it with generic data.
 
Upvote 0
You could upload a copy of your file with the macro to a free site such google drive. Once you do that, mark it for 'Sharing' and you will be given a link to the file that you can post here. If the workbook contains confidential information, you could replace it with generic data.
 
Upvote 0

Forum statistics

Threads
1,224,814
Messages
6,181,120
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top