Merge data for each range based on two words & dates

abdo meghari

Well-known Member
Joined
Aug 3, 2021
Messages
651
Office Version
  1. 2019
Hello
I have about four ranges , every range could contain 5000 rows .
the ranges will be in columns A:D and I want merging ranges based on duplicates dates and the same words for duplicates date
so the duplicate date & NOT PAID should merge alone . also the duplicate date & PAID should merge alone . the merging ranges will be based on column B,C
the result should be in columns F:I and should delete ranges(F:I) when run the macro every time before merging .

before

MERGE RANGES.xlsm
ABCD
1SR
2ITEMDATETYPETOTAL
312023/06/10NOT PAID7,720.00
422023/06/10NOT PAID2,000.00
532023/06/10PAID6,810.00
642023/06/10PAID4,000.00
752023/06/11NOT PAID7,000.00
8SUM27,530.00
9
10SVR
11ITEMDATETYPETOTAL
1212023/06/10NOT PAID5,720.00
1322023/06/10PAID14,040.00
1432023/06/11NOT PAID1,230.00
1542023/06/11NOT PAID12,000.00
1652023/06/12PAID14,000.00
1762023/06/12PAID1,000.00
18SUM47,990.00
19
20
21SDE
22ITEMDATETYPETOTAL
2312023/06/10PAID4,000.00
2422023/06/10PAID1,000.00
2532023/06/11PAID2,000.00
2642023/06/11NOT PAID2,200.00
2752023/06/12NOT PAID3,000.00
28SUM12,200.00
29
30
31FGR
32ITEMDATETYPETOTAL
3312023/06/10PAID1,200.00
3422023/06/10PAID1,300.00
3532023/06/11NOT PAID1,100.00
3642023/06/12NOT PAID2,200.00
3752023/06/12NOT PAID7,000.00
38SUM12,800.00
REPORT
Cell Formulas
RangeFormula
D8,D38,D28D8=SUM(D3:D7)
D18D18=SUM(D12:D17)


after
MERGE RANGES.xlsm
ABCDEFGHI
1SRSR
2ITEMDATETYPETOTALITEMDATENOT PAIDPAID
312023/06/10NOT PAID7,720.0012023/06/109,720.0010,810.00
422023/06/10NOT PAID2,000.0022023/06/1170000.00
532023/06/10PAID6,810.00SUM16,720.0010,810.00
642023/06/10PAID4,000.00
752023/06/11NOT PAID7,000.00SVR
8SUM27,530.00ITEMDATENOT PAIDPAID
912023/06/105,720.0014,040.00
10SVR22023/06/1113,230.000.00
11ITEMDATETYPETOTAL32023/06/120.0015,000.00
1212023/06/10NOT PAID5,720.00SUM18,950.0029,040.00
1322023/06/10PAID14,040.00
1432023/06/11NOT PAID1,230.00SDE
1542023/06/11NOT PAID12,000.00ITEMDATENOT PAIDPAID
1652023/06/12PAID14,000.0012023/06/100.005,000.00
1762023/06/12PAID1,000.0022023/06/110.002,000.00
18SUM47,990.0032023/06/112,200.000.00
1942023/06/123,000.000.00
20SUM5,200.007,000.00
21SDE
22ITEMDATETYPETOTALFGR
2312023/06/10PAID4,000.00ITEMDATENOT PAIDPAID
2422023/06/10PAID1,000.0012023/06/100.002,500.00
2532023/06/11PAID2,000.0022023/06/111,100.000.00
2642023/06/11NOT PAID2,200.0032023/06/129,200.000.00
2752023/06/12NOT PAID3,000.00SUM10,300.002,500.00
28SUM12,200.00
29
30
31FGR
32ITEMDATETYPETOTAL
3312023/06/10PAID1,200.00
3422023/06/10PAID1,300.00
3532023/06/11NOT PAID1,100.00
3642023/06/12NOT PAID2,200.00
3752023/06/12NOT PAID7,000.00
38SUM12,800.00
REPORT
Cell Formulas
RangeFormula
H5:I5H5=SUM(H3:H4)
D8,D38,D28D8=SUM(D3:D7)
H27:I27,H12:I12H12=SUM(H9:H11)
D18D18=SUM(D12:D17)
H20:I20H20=SUM(H16:H19)
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Hi abdo,

You could use a mixture of PowerQuery and formulas to get your result.

let
Source = Excel.CurrentWorkbook(){[Name="SR"]}[Content],
#"Removed Other Columns" = Table.SelectColumns(Source,{"Date"}),
#"Changed Type" = Table.TransformColumnTypes(#"Removed Other Columns",{{"Date", type date}}),
#"Filtered Rows" = Table.SelectRows(#"Changed Type", each ([Date] <> null)),
#"Removed Duplicates" = Table.Distinct(#"Filtered Rows", {"Date"}),
#"Added Index" = Table.AddIndexColumn(#"Removed Duplicates", "Item", 1, 1, Int64.Type),
#"Reordered Columns" = Table.ReorderColumns(#"Added Index",{"Item", "Date"})
in
#"Reordered Columns"

Based on your first Source Table called 'SR', find all unique dates and number them. Then place them in their own table to the right.

Then use a SUMIFS(SR[Total],SR[Date],[@Date],SR[Type],SR_1[[#Headers],[Not Paid]]) to fill in the Not Paid and,
SUMIFS(SR[Total],SR[Date],[@Date],SR[Type],SR_1[[#Headers],[Paid]]) to fill in the Paid values.

As its a Table, add a Total Row.

Rinse and repeat for other Source Tables..

Hope this helps

nkruncher
 
Upvote 0
Would it be easier just to use Pivot Table?
 
Upvote 0
it gives error about SR name by PQ !
If you want to use Power Query, you can use Ranges (which can be flexible) or Cell Addresses, which may need manually updating to suit.

Change
Source = Excel.CurrentWorkbook(){[Name="SR"]}[Content],

To
Source = Excel.CurrentWorkbook(){[Name="A2:D8"]}[Content],

For you example.

Hope this helps

nk
 
Upvote 0
Hi Abdo:

Some details to consider.

1. In your example, for "SDE" you repeated the date.
1710293479179.png

2. The data must start in row 1.
3. The result formats will be taken from cells A to D formats.
4. The last cell with data in column "A" should say "SUM."

Try this:

VBA Code:
Sub Merge_Data()
  Dim ar As Range
  Dim i As Long, j As Long, n As Long, lr As Long
  Dim itm As String
  Dim a As Variant, b As Variant
  Dim dic As Object
  Dim sum_notp As Double, sum_paid As Double
 
  Application.ScreenUpdating = False
 
  Set dic = CreateObject("Scripting.Dictionary")
  Range("F:I").Clear
  lr = Range("A" & Rows.Count).End(3).Row
  a = Range("A1:D" & lr).Value
  ReDim b(1 To UBound(a, 1), 1 To 4)
 
  For i = 2 To UBound(a, 1)
    If a(i, 1) <> "" Then
      If a(i, 2) = "DATE" Then
        itm = a(i - 1, 2)
        j = j + 1
        b(j, 2) = itm
        j = j + 1
        b(j, 1) = "ITEM"
        b(j, 2) = "DATE"
        b(j, 3) = "NOT PAID"
        b(j, 4) = "PAID"
        dic.RemoveAll
        n = 0
        sum_notp = 0
        sum_paid = 0
      Else
        If a(i, 1) = "SUM" Then
          j = j + 1
          b(j, 1) = "SUM"
          b(j, 3) = sum_notp
          b(j, 4) = sum_paid
          j = j + 1
        ElseIf a(i, 2) <> "" Then
          If Not dic.exists(a(i, 2)) Then
            j = j + 1
            dic(a(i, 2)) = j
            n = n + 1
            b(j, 1) = n
            b(j, 2) = a(i, 2)
            b(j, 3) = 0
            b(j, 4) = 0
          End If
          If a(i, 3) = "NOT PAID" Then
            b(j, 3) = b(j, 3) + a(i, 4)
            sum_notp = sum_notp + a(i, 4)
          Else
            b(j, 4) = b(j, 4) + a(i, 4)
            sum_paid = sum_paid + a(i, 4)
          End If
        End If
      End If
    End If
  Next
 
  Range("F1").Resize(UBound(b, 1), UBound(b, 2)).Value = b

  'Format cells
  With Range("F:I")
    .Font.Name = Range("B1").Font.Name
    .Font.Size = Range("B1").Font.Size
    .EntireColumn.AutoFit
    .HorizontalAlignment = xlCenter
  End With
  Range("H:I").NumberFormat = Range("D3").NumberFormat
  Range("G:G").NumberFormat = Range("B3").NumberFormat
 
  For Each ar In Range("G1:G" & lr).SpecialCells(xlCellTypeConstants).Areas
   
    With ar.Cells(1)
      With .Borders
        .LineStyle = xlContinuous
        .ThemeColor = 1
        .TintAndShade = -0.499984740745262
      End With
      .Font.Color = Range("B1").Font.Color
      .Interior.Color = Range("B1").Interior.Color
    End With
   
    With ar.Offset(1, -1).Resize(1, 4)
      .Font.Color = Range("B1").Font.Color
      .Interior.Color = Range("B1").Interior.Color
    End With
   
    With ar.Offset(1, -1).Resize(ar.Rows.Count, 4).Borders
      .LineStyle = xlContinuous
      .ThemeColor = 1
      .TintAndShade = -0.499984740745262
    End With
    ar.Offset(ar.Rows.Count, -1).Resize(1).Interior.Color = Range("A" & lr).Interior.Color
  Next
 
  Application.ScreenUpdating = True
End Sub


Regards
Dante Amor
😇
 

Attachments

  • 1710293025236.png
    1710293025236.png
    13.9 KB · Views: 11
Upvote 1
Solution

Forum statistics

Threads
1,224,817
Messages
6,181,149
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