Consolidate Rows with VBA

RobWolf

New Member
Joined
Jun 19, 2024
Messages
4
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
I have a sheet that a user inputs rows of information using a user form. Some rows will contain the same data except a quantity, How can I consolidate this information with VBA?
I pasted a very simplified example of what I need below.

Thank you for any help.

1718815368482.png
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
How about (Windows)

VBA Code:
Sub jec()
 Dim ar, j As Long
 ar = Range("A2").CurrentRegion
 With CreateObject("scripting.dictionary")
   For j = 2 To UBound(ar)
     If Not .exists(ar(j, 1)) Then
       .Item(ar(j, 1)) = Array(ar(j, 1), ar(j, 2), ar(j, 3))
     Else
       .Item(ar(j, 1)) = Array(ar(j, 1), ar(j, 2), .Item(ar(j, 1))(2) + ar(j, 3))
     End If
   Next
   Range("E2").Resize(.Count, 3) = Application.Index(.items, 0, 0)
 End With
End Sub
 
Upvote 0
How about (Windows)

VBA Code:
Sub jec()
 Dim ar, j As Long
 ar = Range("A2").CurrentRegion
 With CreateObject("scripting.dictionary")
   For j = 2 To UBound(ar)
     If Not .exists(ar(j, 1)) Then
       .Item(ar(j, 1)) = Array(ar(j, 1), ar(j, 2), ar(j, 3))
     Else
       .Item(ar(j, 1)) = Array(ar(j, 1), ar(j, 2), .Item(ar(j, 1))(2) + ar(j, 3))
     End If
   Next
   Range("E2").Resize(.Count, 3) = Application.Index(.items, 0, 0)
 End With
End Sub
thank you. this works perfect for the example I posted. but I might of overly simplified it. below is an actual example of what we might get. I need to compare "thickness" and "desc" if they are the same consolidate the rows and add the board feet together. Also it would be great if it deleted the original information and replaced it with the new consolidated information
1718825036727.png
 
Upvote 0
assuming your data starts in A4

VBA Code:
Sub jec()
 Dim ar, dic, k, j As Long
 Set dic = CreateObject("scripting.dictionary")
 With Range("A4").CurrentRegion
   ar = .Value
   For j = 1 To UBound(ar)
     k = ar(j, 1) & ar(j, 4)
     If Not dic.exists(k) Then
       dic(k) = Array(ar(j, 1), ar(j, 2), ar(j, 3), ar(j, 4), ar(j, 5), ar(j, 6))
     Else
       dic(k) = Array(ar(j, 1), ar(j, 2), ar(j, 3), ar(j, 4), dic(ar(j, 1))(4) + ar(j, 5), ar(j, 6))
     End If
   Next
   .ClearContents
   .Resize(dic.Count, 3) = Application.Index(dic.items, 0, 0)
 End With
End Sub
 
Upvote 0
assuming your data starts in A4

VBA Code:
Sub jec()
 Dim ar, dic, k, j As Long
 Set dic = CreateObject("scripting.dictionary")
 With Range("A4").CurrentRegion
   ar = .Value
   For j = 1 To UBound(ar)
     k = ar(j, 1) & ar(j, 4)
     If Not dic.exists(k) Then
       dic(k) = Array(ar(j, 1), ar(j, 2), ar(j, 3), ar(j, 4), ar(j, 5), ar(j, 6))
     Else
       dic(k) = Array(ar(j, 1), ar(j, 2), ar(j, 3), ar(j, 4), dic(ar(j, 1))(4) + ar(j, 5), ar(j, 6))
     End If
   Next
   .ClearContents
   .Resize(dic.Count, 3) = Application.Index(dic.items, 0, 0)
 End With
End Sub
When I try to run this I get this error message
1718902487108.png


1718902523039.png
 
Upvote 0
Ah yes a typo, change

dic(ar(j,1))(4)

To

dic(k)(4)
 
Upvote 0
And also: (sry did not test)

.Resize(dic.Count, 3)

To

.Resize(dic.Count, 6)
 
Upvote 0

Forum statistics

Threads
1,221,600
Messages
6,160,733
Members
451,668
Latest member
alansbanans

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