I am trying to populate multiple sheets based on the given data (attached Samplesheet SampleSheet.xlsx) as per the below rules:
- Customer Code is the primary key, there should be each sheet for each unique customer code.
- The new sheets should be named as "CustomerCode_Leads"
- Every worksheet should have same headers.
I have started up with a logic and build a code behind but am lacking the knowledge on how to read the customer code data line by line, copy the rows with the same customer code and paste it in the sheet based on unique customer code.
Code written so far:
Sub Test()
Dim ws1 As Worksheet
Set ws1 = ThisWorkbook.Worksheets("Data")
ws1.Copy ThisWorkbook.Sheets(Sheets.Count)
Selection.AutoFilter
Selection.End(xlDown).Select
ActiveCell.Offset(1, 0).Select
ActiveCell.EntireRow.Select
Selection.Delete Shift:=xlUp
Application.Goto Reference:="R2C2"
ActiveCell.EntireColumn.Select
Selection.Copy
Sheets.Add After:=ActiveSheet
ActiveSheet.Paste
Application.CutCopyMode = False
ActiveSheet.Range("$A$1:$A$5000").RemoveDuplicates Columns:=1, Header:=xlYes
Dim CurSheet As Worksheet
Dim Source As Range
Dim c As Range
Set CurSheet = ActiveSheet
Set Source = Selection.Cells
Application.ScreenUpdating = False
For Each c In Source
sName = Trim(c.Text)
If Len(sName) > 0 Then
Worksheets.Add After:=Worksheets(Worksheets.Count)
ActiveSheet.Name = sName + "_Leads"
End If
Next c
End Sub
Can someone advise how to read the data row by row and paste it in a new worksheet named "CustomerCode_Lead" where "CustomerCode" is a variable with some values in the Data sheet.
The algorithm that I am following is:
Copy the datasheet and paste it into a new worksheet
Sort the data in ascending order based on Customer Code (it'll bring all the similar customer code together and ease the row by row reading)
Read the data row by row and copy the entire row and paste into a new sheet until the customer code stays the same, once different code arrives in the next row, it creates a new sheet named "CustomerCode_Leads"
Do the reading of data until the end of the data in the "Data" sheet.
I would absolutely thank you in advance for the help I'll get here from the community. :)