VBA Combining Information and Totaling a Sum

bigmacfann

New Member
Joined
Aug 26, 2005
Messages
48
Hello,

Every month, I get the below table sent to me (Columns A-E) which varies in the number of records it contains. Sometimes there are only 3 or 4 rows of entries, and sometimes there can be as many as 250 rows of entries. After I do my work on the sheet, I am left with what is in Columns G-J. What I am doing is looking for check numbers in Column B which have the same number then combining the Job # in Column I separated with a comma and an ampersand between the last 2 job numbers (as you can see in cell I23. I am also taking the totals in Column E and adding them up into Column J for the check numbers which match. Can anyone help me with a VBA code to do this automatically?

Deposit Detail - October 2024.xlsx
ABCDEFGHIJ
1DateCheck NumNameJob #AmountDateNameProject #Amount Paid
210/09/20249905208109Contractor 124-755-$2,140.0010/09/2024Contractor 124-755 & 24-750$3,590.00
310/09/20249905208109Contractor 124-750-$1,450.0010/09/2024Contractor 1024-738$3,300.00
410/09/20249363Contractor 1024-738-$3,300.0010/09/2024Contractor 1123-441$45,584.10
510/09/20240070782Contractor 1123-441-$45,584.1010/09/2024Contractor 1424-753$1,200.00
610/09/20240000005069Contractor 1424-753-$1,200.0010/09/2024Contractor 1523-484$24,073.20
710/09/202425433Contractor 1523-484-$24,073.2010/09/2024Contractor 1624-737$1,380.00
810/09/20245500Contractor 1624-737-$1,380.0010/09/2024Contractor 1823-629$13,497.75
910/09/202418177Contractor 1823-629-$13,497.7510/09/2024Contractor 224-771$9,390.00
1010/09/202412127Contractor 224-771-$9,390.0010/09/2024Contractor 2024-794$2,060.00
1110/09/20247132Contractor 2024-794-$2,060.0010/09/2024Contractor 48-241, 14-578 & 18-549$32,305.00
1210/09/2024567316Contractor 48-241-$25,984.0010/09/2024Contractor 724-801$15,000.00
1310/09/2024567316Contractor 414-578-$6,085.0010/09/2024Contractor 823-624$11,227.00
1410/09/2024567316Contractor 418-549-$236.0010/09/2024Contractor 924-639$48,085.20
1510/09/202447954Contractor 724-801-$15,000.0010/25/2024Contractor 1224-727$2,540.00
1610/09/2024321107Contractor 823-624-$11,227.0010/25/2024Contractor 1324-760$7,800.00
1710/09/20240000009668Contractor 924-639-$48,085.2010/25/2024Contractor 1324-740$3,020.00
1810/25/202411497Contractor 1224-727-$2,540.0010/25/2024Contractor 1324-752$2,340.00
1910/25/20247210511133Contractor 1324-760-$7,800.0010/25/2024Contractor 1324-751$820.00
2010/25/20247210632222Contractor 1324-740-$3,020.0010/25/2024Contractor 1324-759$1,870.00
2110/25/20247210632223Contractor 1324-752-$2,340.0010/25/2024Contractor 1324-769$4,180.00
2210/25/20247210632224Contractor 1324-751-$820.0010/25/2024Contractor 1523-484$7,002.45
2310/25/20247210839665Contractor 1324-759-$1,870.0010/25/2024Contractor 1724-678, 24-693, 24-679 & 24-725$39,320.60
2410/25/20247210839670Contractor 1324-769-$4,180.0010/25/2024Contractor 1922-298$8,215.00
2510/25/202425598Contractor 1523-484-$7,002.4510/25/2024Contractor 48-241, 14-578 & 18-549$23,460.00
2610/25/202417338Contractor 1724-678-$11,050.0010/25/2024Contractor 524-707$340.00
2710/25/202417338Contractor 1724-693-$8,660.0010/25/2024Contractor 524-741$3,150.00
2810/25/202417338Contractor 1724-679-$11,550.6010/25/2024Contractor 624-690$15,871.00
2910/25/202417338Contractor 1724-679-$1,380.0010/31/2024Contractor 1424-747$5,580.00
3010/25/202417338Contractor 1724-725-$6,680.0010/31/2024Contractor 1424-722$35,046.00
3110/25/20242474Contractor 1922-298-$8,215.0010/31/2024Contractor 324-765$7,650.00
3210/25/2024570702Contractor 48-241-$18,375.00
3310/25/2024570702Contractor 414-578-$4,466.00
3410/25/2024570702Contractor 418-549-$619.00
3510/25/2024862408284Contractor 524-707-$340.00
3610/25/2024862408286Contractor 524-741-$3,150.00
3710/25/202407152200Contractor 624-690-$15,871.00
3810/31/20240000001412Contractor 1424-747-$5,580.00
3910/31/20240000002836Contractor 1424-722-$35,046.00
4010/31/20241111Contractor 324-765-$7,650.00
Sheet1
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
looking for check numbers in Column B which have the same number

It may be that the same number exists with, for example, 2 different dates, if so what should the macro do?
a) Create 2 records?
b) Create 1 record, but which date to take?
 
Upvote 0
If the date is not relevant, try the following macro:

VBA Code:
Sub CombiningInformation()
  Dim dic As Object
  Dim a As Variant, b As Variant
  Dim i As Long, y As Long, n As Long
  
  Set dic = CreateObject("Scripting.Dictionary")
  
  a = Range("A2", Range("E" & Rows.Count).End(3)).Value
  ReDim b(1 To UBound(a, 1), 1 To 4)
  
  Range("G2:J" & Rows.Count).ClearContents
  
  For i = 1 To UBound(a, 1)
    If Not dic.exists(a(i, 2)) Then
      dic(a(i, 2)) = dic.Count + 1
    End If
    y = dic(a(i, 2))
    b(y, 1) = a(i, 1)
    b(y, 2) = a(i, 3)
    b(y, 3) = b(y, 3) & ", " & a(i, 4)
    b(y, 4) = b(y, 4) + -a(i, 5)
  Next
  
  For i = 1 To dic.Count
    b(i, 3) = Mid(b(i, 3), 3)
    n = InStrRev(b(i, 3), ",")
    If n > 0 Then b(i, 3) = Left(b(i, 3), n - 1) & Replace(b(i, 3), ",", " &", n)
  Next
  
  Range("G2").Resize(UBound(b, 1), UBound(b, 2)).Value = b
End Sub

🤗
 
Upvote 0
Dim dic As Object Dim a As Variant, b As Variant Dim i As Long, y As Long, n As Long Set dic = CreateObject("Scripting.Dictionary") a = Range("A2", Range("E" & Rows.Count).End(3)).Value ReDim b(1 To UBound(a, 1), 1 To 4) Range("G2:J" & Rows.Count).ClearContents For i = 1 To UBound(a, 1) If Not dic.exists(a(i, 2)) Then dic(a(i, 2)) = dic.Count + 1 End If y = dic(a(i, 2)) b(y, 1) = a(i, 1) b(y, 2) = a(i, 3) b(y, 3) = b(y, 3) & ", " & a(i, 4) b(y, 4) = b(y, 4) + -a(i, 5) Next For i = 1 To dic.Count b(i, 3) = Mid(b(i, 3), 3) n = InStrRev(b(i, 3), ",") If n > 0 Then b(i, 3) = Left(b(i, 3), n - 1) & Replace(b(i, 3), ",", " &", n) Next Range("G2").Resize(UBound(b, 1), UBound(b, 2)).Value = b
I get a type mismatch on line b(y, 4) = b(y, 4) + -a(i, 5)
 
Upvote 0
It may be that the same number exists with, for example, 2 different dates, if so what should the macro do?
a) Create 2 records?
b) Create 1 record, but which date to take?
If multiple jobs were paid by the same check, the date will always be the same (because it's 1 check). One thing to note is that 1 check could have paid the same job multiple times, but I do not need the same job number listed more than once in Column I; I do, however, still need the total to get added to the sum in Column J.
 
Upvote 0
I get a type mismatch on line b(y, 4) = b(y, 4) + -a(i, 5)

That's because in column E you have text instead of numerical values.

Run the macro again, when the error occurs, press the Debug button, on the line highlighted in yellow, bring the mouse pointer closer to the letter "y". A small window will appear with text like the one in the following image:

1738267964141.png


After the "y" a number appears, add 1 to that number, in that line, in column "E" of your Excel sheet you have a text.

Check that in column E all are numerical values.


------------------------------------------
One thing to note is that 1 check could have paid the same job multiple times, but I do not need the same job number listed more than once in Column I; I do, however, still need the total to get added to the sum in Column J.

For that, try: this:

VBA Code:
Sub CombiningInformation()
  Dim dic As Object
  Dim a As Variant, b As Variant
  Dim i As Long, y As Long, n As Long
 
  Set dic = CreateObject("Scripting.Dictionary")
  a = Range("A2", Range("E" & Rows.Count).End(3)).Value
  ReDim b(1 To UBound(a, 1), 1 To 4)
  Range("G2:J" & Rows.Count).ClearContents
 
  For i = 1 To UBound(a, 1)
    If Not dic.exists(a(i, 2)) Then
      dic(a(i, 2)) = dic.Count + 1
    End If
    y = dic(a(i, 2))
    b(y, 1) = a(i, 1)
    b(y, 2) = a(i, 3)
    If InStr(1, b(y, 3), a(i, 4), vbTextCompare) = 0 Then
      b(y, 3) = b(y, 3) & ", " & a(i, 4)
    End If
    b(y, 4) = b(y, 4) + -a(i, 5)
  Next
 
  For i = 1 To dic.Count
    b(i, 3) = Mid(b(i, 3), 3)
    n = InStrRev(b(i, 3), ",")
    If n > 0 Then b(i, 3) = Left(b(i, 3), n - 1) & Replace(b(i, 3), ",", " &", n)
  Next
 
  Range("G2").Resize(UBound(b, 1), UBound(b, 2)).Value = b
End Sub


🫡
 
Upvote 0
After the "y" a number appears, add 1 to that number, in that line, in column "E" of your Excel sheet you have a text.
Everything in Column E is formatted as Currency. When I hit debug, it had an issue with y=31 so I went to E32 and it was formatted as Currency (-18375)
 
Upvote 0
E32 and it was formatted as Currency (-18375)
Maybe that number is stored as text, or maybe it has whitespace on the right or left.

Try the following macro to try to solve the problem with your data.

VBA Code:
Sub CombiningInformation()
  Dim dic As Object
  Dim a As Variant, b As Variant
  Dim i As Long, y As Long, n As Long
  Dim aux As Variant, ch As String
  
  Set dic = CreateObject("Scripting.Dictionary")
  
  a = Range("A2", Range("E" & Rows.Count).End(3)).Value
  ReDim b(1 To UBound(a, 1), 1 To 4)
  
  Range("G2:J" & Rows.Count).ClearContents
  
  For i = 1 To UBound(a, 1)
    If Not dic.exists(a(i, 2)) Then
      dic(a(i, 2)) = dic.Count + 1
    End If
    y = dic(a(i, 2))
    b(y, 1) = a(i, 1)
    b(y, 2) = a(i, 3)
    If InStr(1, b(y, 3), a(i, 4), vbTextCompare) = 0 Then
      b(y, 3) = b(y, 3) & ", " & a(i, 4)
    End If
    
    aux = Replace(Replace(Replace(a(i, 5), "$", ""), " ", ""), Chr(160), "")
    If IsNumeric(aux) Then
      b(y, 4) = b(y, 4) + -aux
    Else
      ch = ch & "E" & i + 1 & vbCr
    End If
  Next
  
  For i = 1 To dic.Count
    b(i, 3) = Mid(b(i, 3), 3)
    n = InStrRev(b(i, 3), ",")
    If n > 0 Then b(i, 3) = Left(b(i, 3), n - 1) & Replace(b(i, 3), ",", " &", n)
  Next
  
  Range("G2").Resize(UBound(b, 1), UBound(b, 2)).Value = b
  If ch <> "" Then MsgBox "Cells with error: " & vbCr & ch
End Sub


If the problem with your data continues. To continue with the test. Delete the rows from the cell with error downwards. Try the macro again to see the results.

Analyze the cell with error, check that it really is a number, edit the cell and then press enter. Or delete the content and manually type the number.

All of the above is simply to verify your data.

🫡
 
Upvote 0

Forum statistics

Threads
1,226,116
Messages
6,189,055
Members
453,523
Latest member
Don Quixote

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