excelvbaexcel-2010vba7vba6

want to split data depends on multiple columns


I want to split data on basis of A & B-column, if next row is same from A-column then look into B-column. if next row is same from B then copy F, G H-column till value from B is unchanged. Its very complicated to explain in words. that's why I have pasted snap of raw data & expected result. It would be very grateful if someone help me into this. Column A to H is raw data, From column-J is expected data. I have selected range of raw & result data in snap. We just have to paste values from F-column only once for unique value A.

Sub demo()
Dim lastrow As Integer, r As Integer, c As Integer, cot4 As Integer, r1 As Integer
r1 = 2

lastrow = Worksheets("sheet1").Cells(Rows.Count, 1).End(xlUp).Row

Dim r3 As Integer

c = 15

For r = 3 To lastrow + 1

If Worksheets("Sheet1").Cells(r, 2).Offset(0, 0) = Worksheets("Sheet1").Cells(r, 2).Offset(-1, 0) Then
        
    Worksheets("Sheet1").Cells(1, c) = Worksheets("Sheet1").Cells(r, 1)
    Worksheets("Sheet1").Cells(2, c) = Worksheets("Sheet1").Cells(r, 2)
    Worksheets("Sheet1").Cells(r1, c).Offset(1, 0) = Worksheets("Sheet1").Cells(r - 1, 7)
Else
    Worksheets("Sheet1").Cells(r, c) = Worksheets("Sheet1").Cells(r, 7).Offset(-1, 0)
    c = c + 1
   Worksheets("Sheet1").Cells(r, c) = Worksheets("Sheet1").Cells(r, 7)
    Worksheets("Sheet1").Cells(r1, c) = Worksheets("Sheet1").Cells(r - 1, 7)
   ' c = c + 1
    r1 = 1
  End If
  r1 = r1 + 1
  Next r
  Call mycode_to_merge
End Sub

Sub mycode_to_merge()

Range("O1").Select
Range(Selection, Selection.End(xlToRight)).Select

Application.DisplayAlerts = False

Dim rng As Range

MergeCells:

For Each rng In Selection

    If rng.Value = rng.Offset(0, 1).Value And rng.Value <> "" Then
        Range(rng, rng.Offset(0, 1)).merge
        Range(rng, rng.Offset(0, 1)).HorizontalAlignment = xlCenter
        Range(rng, rng.Offset(0, 1)).VerticalAlignment = xlCenter
        GoTo MergeCells
    End If
Next
End Sub

This code generates this result, enter image description here

Don't have problem in above mycode_to_merge function.

Following is the Raw Data, need to split by space:

SVC_RA_CHT_NR   DEL_ZN_NR   FIX_VAR_MUL_CD  CTM_RA_CHT_MIN_QY   WGT_MS_UNT_TYP_CD   CTM_RA_CHT_MAX_QY   NCV_PKG_RA  RA_TYP_CD
1J1K2J  001             1   26.36   R
1J1K2J  001             2   26.91   R
1J1K2J  001             3   27.47   R
1J1K2J  001             10000000    0   P
1J1K2J  002             1   29.5    R
1J1K2J  002             2   30.93   R
1J1K2J  002             3   32.35   R
1J1K2J  002             10000000    0   P
1J1K2J  505             1   13.88   R
1J1K2J  505             2   0.65    R
1J1K2J  505             3   0.5 R
1J1K2J  505             10000000    0   P
1J1K2K  004             0.5 25.8    R
1J1K2K  004             1   28.63   R
1J1K2K  004             1.5 31.51   R
1J1K2K  004             65  150.51  R
1J1K2K  004             70  158.52  R
1J1K2K  004             10000000    2.26    M
1J1K2K  006             0.5 42.07   R
1J1K2K  006             1   46.63   R
1J1K2K  006             1.5 51.18   R
1J1K2K  006             65  244.06  R
1J1K2K  006             70  257.24  R
1J1K2K  006             10000000    3.67    M
1J1K2K  041             0.5 29.83   R
1J1K2K  041             1   32.04   R
1J1K2K  041             1.5 34.25   R
1J1K2K  041             65  156.3   R
1J1K2K  041             70  164.58  R
1J1K2K  041             10000000    2.35    M
1J1K2K  042             0.5 29.84   R
1J1K2K  042             1   32.93   R
1J1K2K  042             1.5 35.98   R
1J1K2K  042             65  177.15  R
1J1K2K  042             70  186.76  R
1J1K2K  042             10000000    2.66    M
1J1K2K  505             0.5 25.21   R
1J1K2K  505             1   28.13   R
1J1K2K  505             1.5 31.04   R
1J1K2K  505             65  144.24  R
1J1K2K  505             70  151.27  R
1J1K2K  505             10000000    2.15    M

Markdown format

SVC_RA_CHT_NR DEL_ZN_NR FIX_VAR_MUL_CD CTM_RA_CHT_MIN_QY WGT_MS_UNT_TYP_CD CTM_RA_CHT_MAX_QY NCV_PKG_RA RA_TYP_CD
1J1K2J 001 1 26.36 R
1J1K2J 001 2 26.91 R
1J1K2J 001 3 27.47 R
1J1K2J 001 10000000 0 P
1J1K2J 002 1 29.5 R
1J1K2J 002 2 30.93 R
1J1K2J 002 3 32.35 R
1J1K2J 002 10000000 0 P
1J1K2J 505 1 13.88 R
1J1K2J 505 2 0.65 R
1J1K2J 505 3 0.5 R
1J1K2J 505 10000000 0 P
1J1K2K 004 0.5 25.8 R
1J1K2K 004 1 28.63 R
1J1K2K 004 1.5 31.51 R
1J1K2K 004 65 150.51 R
1J1K2K 004 70 158.52 R
1J1K2K 004 10000000 2.26 M
1J1K2K 006 0.5 42.07 R
1J1K2K 006 1 46.63 R
1J1K2K 006 1.5 51.18 R
1J1K2K 006 65 244.06 R
1J1K2K 006 70 257.24 R
1J1K2K 006 10000000 3.67 M
1J1K2K 041 0.5 29.83 R
1J1K2K 041 1 32.04 R
1J1K2K 041 1.5 34.25 R
1J1K2K 041 65 156.3 R
1J1K2K 041 70 164.58 R
1J1K2K 041 10000000 2.35 M
1J1K2K 042 0.5 29.84 R
1J1K2K 042 1 32.93 R
1J1K2K 042 1.5 35.98 R
1J1K2K 042 65 177.15 R
1J1K2K 042 70 186.76 R
1J1K2K 042 10000000 2.66 M
1J1K2K 505 0.5 25.21 R
1J1K2K 505 1 28.13 R
1J1K2K 505 1.5 31.04 R
1J1K2K 505 65 144.24 R
1J1K2K 505 70 151.27 R
1J1K2K 505 10000000 2.15 M

Solution

  • Using a dictionary and collections

    edit1 - check on F and H values

    Option Explicit
    
    Sub processX()
    
        Dim ws As Worksheet, dict, k1, k2, ar, v
        Dim lastrow As Long, i As Long, c As Long, r As Long, n As Long
        Dim a As String, b As String
        
        Set ws = ThisWorkbook.Sheets("Sheet1")
        Set dict = CreateObject("Scripting.Dictionary")
        
        With ws
            .Columns("M:XFD").Delete
            lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row
            ar = .Range("A2:H" & lastrow)
        End With
        
         ' input
        For i = 1 To UBound(ar)
            a = Trim(ar(i, 1))
            b = Trim(ar(i, 2))
            
            If Not dict.exists(a) Then
                dict.Add a, CreateObject("Scripting.Dictionary")
            End If
            
            If Not dict(a).exists(b) Then
                dict(a).Add b, New Collection
            End If
            
            dict(a)(b).Add i
        Next
        
        ' output
        c = 13 ' m
        For Each k1 In dict.keys
            ws.Cells(1, c) = k1
            n = 1
            For Each k2 In dict(k1).keys
                n = n + 1
                ws.Cells(2, c + n).NumberFormat = "@"
                ws.Cells(2, c + n) = k2
              
               ' values
                r = 3
                For Each v In dict(k1)(k2)
                    If n = 2 Then
                        ws.Cells(r, c) = ar(v, 6) ' col F
                        ws.Cells(r, c + 1) = ar(v, 8) ' col H
                    Else
                        ' check all rows in collection have same F H values
                        If ws.Cells(r, c) <> ar(v, 6) _
                          Or ws.Cells(r, c + 1) <> ar(v, 8) Then
                          
                            MsgBox "Mismatch col F or H row " & v + 1, vbExclamation
                            Exit Sub                        
                        End If                    
                    End If
                    ws.Cells(r, c + n) = ar(v, 7) ' col G
                    r = r + 1
                Next
            Next
           
            ' format header
            With ws.Cells(1, c).Resize(, n + 1)
                .Merge
                .HorizontalAlignment = xlCenter
                .VerticalAlignment = xlCenter
                .Interior.Color = RGB(220, 220, 255)
                .Font.Bold = True
            End With
            c = c + n + 2
        Next
                
        MsgBox lastrow - 1 & " rows processed", vbInformation
    End Sub