Macro Required or a Shortcut

Abbas999

Board Regular
Joined
Jan 4, 2015
Messages
102
Hi All members,

I have around 2K records in a excel sheet with fields, Company, City, First name, Last name, Email, Then First name, Last Name, Email, Then First name Last name, Email, and so on....

Let me explain this. Each row contain a Company details and their workers first name, last name, email.
Each company have at least one worker and up to 8 workers.


Example Picture - It shows One Company one city but 6 workers working for them but data is in a single row.



<tbody>
</tbody>

Unknown
ABCDEFGHIJK
255Company NameCity/ProvFirst 1Last 1Email 1First 2Last 2Email 2First 3Last 3Email 3
256ISTITUTO CLINICO HUMANITASRozzanoSimonaCamardasimona.camarda@humanitas.itMonicaFlorianelloufficio.stampa@humanitas.itMartaGalbiatimarta.galbiati@materdomini.it
Filtered


Here is what i want to organize them in excel sheet. In above picture it shows 1 Company with six workers in a single row. I want six rows for one company but only difference is worker name and email.


Unknown
ABCDE
255Company NameCity/ProvFirst NameLast nameEmail
256ISTITUTO CLINICO HUMANITASRozzanoSimonaCamardasimona.camarda@humanitas.it
257ISTITUTO CLINICO HUMANITASRozzanoMonicaFlorianelloufficio.stampa@humanitas.it
258ISTITUTO CLINICO HUMANITASRozzanoMartaGalbiatimarta.galbiati@materdomini.it
259ISTITUTO CLINICO HUMANITASRozzanoTatianaDebellitatiana.debelli@gavazzeni.it
260ISTITUTO CLINICO HUMANITASRozzanoLauraCapardonilaura.capardoni@humanitas.it
261ISTITUTO CLINICO HUMANITASRozzanoWalterBruno walter.bruno@humanitas.it
Filtered


I want to know if there is a way to do this in a better way? Other than first inserting rows then copying and pasting name emails.

Thank you in advance
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
This is untested - try it on a copy of your sheet.
Code:
Sub RearrangeData()
Dim R As Range, lC As Long, i As Long, Rws As Long, j As Long
Set R = Range("A255").CurrentRegion.Offset(1, 0).Resize(Range("A255").CurrentRegion.Rows.Count - 1)
lC = R.Columns.Count
Application.ScreenUpdating = False
For i = R.Rows.Count To 1 Step -1
       Rws = WorksheetFunction.CountA(Range(R.Rows(i).Cells(i, 3), R.Rows(i).Cells(i, lC))) / 3
       If Rws > 1 Then
              R.Rows(i).Offset(1, 0).Resize(Rws).EntireRow.Insert
              R.Rows(i).Cells(i, 1).Resize(Rws, 2).FillDown
              For j = 2 To Rws
                     Range(R.Rows(i).Offset(j - 1, 0).Cells(i, 3), _
                         R.Rows(i).Offset(j - 1, 0).Cells(i, 5)).Value = Range(R.Rows(i).Cells(i, j * Rws), _
                         R.Rows(i).Cells(i, j * Rws + 2)).Value
                         Range(R.Rows(i).Cells(i, j * Rws), R.Rows(i).Cells(i, j * Rws + 2)).ClearContents
              Next j
       End If
Next i
Application.ScreenUpdating = True
End Sub
 
Upvote 0
This is untested - try it on a copy of your sheet.
Code:
Sub RearrangeData()
Dim R As Range, lC As Long, i As Long, Rws As Long, j As Long
Set R = Range("A255").CurrentRegion.Offset(1, 0).Resize(Range("A255").CurrentRegion.Rows.Count - 1)
lC = R.Columns.Count
Application.ScreenUpdating = False
For i = R.Rows.Count To 1 Step -1
       Rws = WorksheetFunction.CountA(Range(R.Rows(i).Cells(i, 3), R.Rows(i).Cells(i, lC))) / 3
       If Rws > 1 Then
              R.Rows(i).Offset(1, 0).Resize(Rws).EntireRow.Insert
              R.Rows(i).Cells(i, 1).Resize(Rws, 2).FillDown
              For j = 2 To Rws
                     Range(R.Rows(i).Offset(j - 1, 0).Cells(i, 3), _
                         R.Rows(i).Offset(j - 1, 0).Cells(i, 5)).Value = Range(R.Rows(i).Cells(i, j * Rws), _
                         R.Rows(i).Cells(i, j * Rws + 2)).Value
                         Range(R.Rows(i).Cells(i, j * Rws), R.Rows(i).Cells(i, j * Rws + 2)).ClearContents
              Next j
       End If
Next i
Application.ScreenUpdating = True
End Sub

@JoeMo Actually I used VBA few months ago and now I did'nt remeber how to execute this code in VBA. Could you please explain this?
 
Upvote 0
I just did the following:

Open a internet browsing window.
Went to Google.com
Typed into the search bar "Excel run a macro"
The top item is a box with full instructions on running a macro.

Did you try to search for the answer?
 
Upvote 0
I just did the following:

Open a internet browsing window.
Went to Google.com
Typed into the search bar "Excel run a macro"
The top item is a box with full instructions on running a macro.

Did you try to search for the answer?

Yes Did it already but this code is not giving the required results.
It just doubles the Company name and City. Still looking for a solution
 
Upvote 0
This is untested - try it on a copy of your sheet.
Code:
Sub RearrangeData()
Dim R As Range, lC As Long, i As Long, Rws As Long, j As Long
Set R = Range("A255").CurrentRegion.Offset(1, 0).Resize(Range("A255").CurrentRegion.Rows.Count - 1)
lC = R.Columns.Count
Application.ScreenUpdating = False
For i = R.Rows.Count To 1 Step -1
       Rws = WorksheetFunction.CountA(Range(R.Rows(i).Cells(i, 3), R.Rows(i).Cells(i, lC))) / 3
       If Rws > 1 Then
              R.Rows(i).Offset(1, 0).Resize(Rws).EntireRow.Insert
              R.Rows(i).Cells(i, 1).Resize(Rws, 2).FillDown
              For j = 2 To Rws
                     Range(R.Rows(i).Offset(j - 1, 0).Cells(i, 3), _
                         R.Rows(i).Offset(j - 1, 0).Cells(i, 5)).Value = Range(R.Rows(i).Cells(i, j * Rws), _
                         R.Rows(i).Cells(i, j * Rws + 2)).Value
                         Range(R.Rows(i).Cells(i, j * Rws), R.Rows(i).Cells(i, j * Rws + 2)).ClearContents
              Next j
       End If
Next i
Application.ScreenUpdating = True
End Sub

Not Giving the required results. Just doubles the first two coulmns.
 
Upvote 0
Is your data layout the same as you showed in your post?

In the code i see it was starting Range "A255" but i deleted some rows in excel and the data i mentioned above was "A255" but now "A260" so i edit the code line
Before - Set R = Range("A255")
After - Set R = Range("A260")

and i got few things corrected but not 100%. When i scroll down in sheet i can see some names are still in a single row.

Maybe code needed more modification. Let me know if i did something wrong?
 
Upvote 0
In the code i see it was starting Range "A255" but i deleted some rows in excel and the data i mentioned above was "A255" but now "A260" so i edit the code line
Before - Set R = Range("A255")
After - Set R = Range("A260")

and i got few things corrected but not 100%. When i scroll down in sheet i can see some names are still in a single row.

Maybe code needed more modification. Let me know if i did something wrong?
Are there any empty rows or columns between A260 and the end of your starting data?
 
Upvote 0

Forum statistics

Threads
1,215,960
Messages
6,127,943
Members
449,412
Latest member
montand

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