I would really appreciate some help to find a correct approach to solve my issue.
I need to mach data from different sheets.
In the sheet 1, I have this list of data.
Key Reference COL B COL C COL D
ID123 YZA ... ...
ID123 BBA ... ...
ID123 XCP ... ...
ID123 ABC
ID123 empty cell
ID123 …
ID124 empty cell
ID124 XCP
… …
In the sheet2, I will only have the list of unique references
ID123 ID124 ID125 ...
By unique reference, I need to sort the data from the column B with the below conditions:
- empty cells
- string "XCP"
- all the rest (from ABC to YZA)
Then, count the number of rows by unique reference Insert this number of rows in the sheet2 and paste the data sorted.
I think the easiest way to do it is to use a loop with a If statement for each of my condition instead of the sorting option.
The expected result are : So it seems to be the same as sheet 1 but col b respect my sorting conditions
Key Reference COL B COL C COL D
ID123 empty cell ... ...
ID123 XCP ... ...
ID123 ABC
ID123 YZA
ID123 …
ID124 empty cell
ID124 XCP
. Please see below the code i try to create
Sub mapbreak5() Dim lr As Long, r As Long lr = Sheets("Sheet1").Cells(Rows.Count, "A").End(xlUp).Row Dim rngKey As Range
For r = 2 To lr
If Sheets("Sheet1").Range("B" & r).Value = "" Then
'...
End If
Next r
'Or =>
Do
If Range("B2") Is Empty Then
Copy.EntireRow
'find the respective key refence in the breaks sheet
ThisWorkbook.Worksheets("breaks").Cells.Find(rngKey.Value, searchorder:=xlByRows, searchdirection:=xlPrevious).Row
'check if the IDxx field is already populated
If Range("F2") Is Empty Then
Range("E2").Paste.Selection
Else: ActiveCell.Offset (1)
Rows.Select
Selection.Insert Shift:=xlDown
End If
Else: ActiveCell.Offset (1)
End If
Loop Until IsEmpty(ActiveCell.Offset(0, -1))
Do
If Range("B2") = "XCP" Then
Copy.EntireRow
'find the respective key refence in the breaks sheet
ThisWorkbook.Worksheets("breaks").Cells.Find(rngKey.Value, searchorder:=xlByRows, searchdirection:=xlPrevious).Row
'check if the IDxx field is already populated
If Range("F2") Is Empty Then
Range("E2").Paste.Selection
Else: ActiveCell.Offset (1)
Rows.Select
Selection.Insert Shift:=xlDown
End If
Else: ActiveCell.Offset (1)
End If
Loop Until IsEmpty(ActiveCell.Offset(0, -1))
Do
If Range("B2") Is Not Empty Or "XCP" Then
Copy.EntireRow
'find the respective key refence in the breaks sheet
ThisWorkbook.Worksheets("breaks").Cells.Find(rngKey.Value, searchorder:=xlByRows, searchdirection:=xlPrevious).Row
'check if the IDxx field is already populated
If Range("F2") Is Empty Then
Range("E2").Paste.Selection
Else: ActiveCell.Offset (1)
Rows.Select
Selection.Insert Shift:=xlDown
End If
Else: ActiveCell.Offset (1)
End If
Loop Until IsEmpty(ActiveCell.Offset(0, -1))
End Sub