Need help consolidating data from 3 columns. I have a VBA code for QTY but the last column will now be data instead, how do I change code?

kholden1

New Member
Joined
Jun 8, 2023
Messages
16
Office Version
  1. 365
  2. 2007
Platform
  1. Windows
Good morning!

You all helped me come up with a VBA code that takes a part number, manufacturer, and qty and consolidates duplicates. I have been messing around with that original VBA code but I cannot seem to figure out how to replace with a new 3rd column and consolidate it the same. It keeps trying to count a qty that isn't there since my change with column 3 is words. I've added a picture of what I'm currently working on. I want each column to consolidate, as long as it's the same as columns B and C. If it's different than either column, then I'd want it to start as a new line.

Capture.PNG



Here is my original VBA code:

Sub Consolidate()

Dim ws1 As Worksheet

Dim ws2 As Worksheet

Dim lrow As Long

Dim i As Long



Set ws1 = ActiveSheet

Set ws2 = ActiveWorkbook.Worksheets.Add

ws2.Name = "ONLINEINV3"


lrow = ws1.Cells(Rows.Count, 1).End(xlUp).Row



ws1.Range("A1:B" & lrow).Copy ws2.Range("A1")



ws2.Range("A:B").RemoveDuplicates Columns:=Array(1, 2), Header:=xlYes

ws2.Range("C1") = "FULL PARTNO"

ws2.Range("D1") = "QTY"


lrow = ws2.Cells(Rows.Count, 1).End(xlUp).Row


For i = 2 To lrow

ws2.Cells(i, 3) = ws2.Cells(i, 2) & " " & ws2.Cells(i, 1)

ws2.Cells(i, 4) = Application.SumIfs(ws1.Range("C:C"), ws1.Range("A:A"), ws2.Cells(i, 1), ws1.Range("B:B"), ws2.Cells(i, 2))



Next i


End Sub
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
It helps if you use code tags when posting your code. Here is your original code using code tags
VBA Code:
'Here is my original VBA code:
Sub Consolidate()
    Dim ws1 As Worksheet
    Dim ws2 As Worksheet
    Dim lrow As Long
    Dim i As Long

    Set ws1 = ActiveSheet
    Set ws2 = ActiveWorkbook.Worksheets.Add

    ws2.Name = "ONLINEINV3"
    lrow = ws1.Cells(Rows.Count, 1).End(xlUp).Row

    ws1.Range("A1:B" & lrow).Copy ws2.Range("A1")
    ws2.Range("A:B").RemoveDuplicates Columns:=Array(1, 2), Header:=xlYes

    ws2.Range("C1") = "FULL PARTNO"
    ws2.Range("D1") = "QTY"

    lrow = ws2.Cells(Rows.Count, 1).End(xlUp).Row

    For i = 2 To lrow
        ws2.Cells(i, 3) = ws2.Cells(i, 2) & " " & ws2.Cells(i, 1)
        ws2.Cells(i, 4) = Application.SumIfs(ws1.Range("C:C"), ws1.Range("A:A"), ws2.Cells(i, 1), ws1.Range("B:B"), ws2.Cells(i, 2))
    Next i
End Sub

Code tag instructions:
 
Upvote 0
1696964873237.png


Instead of posting your data as an image, post it in a way that others can easily copy if they need it to analyze or experiment with.
Book1
ABC
1PartMFGInternal Part
2anamea
3anamea
4anamea1
5anamea1
6bname1b
7bname1b1
8cname2c
9cname2c
10cname2c
Sheet1


You can use this free tool

 
Upvote 0
I want each column to consolidate, as long as it's the same as columns B and C. If it's different than either column, then I'd want it to start as a new line.

This part is not clear at all. Instead, post an example of what the new sheet should look like for a given set of data on the input sheet.

1696965002993.png
 
Upvote 0
I apologize, I am having trouble downloading the add-in right now. I will keep trying. I think I am in over my head.

Right now my table works like this:

Part NumberManufacterqty
abc123Micro500,000
abc123Micro25,600
abc123Conn300,000
bcd234Micro40,000
bcd234Conn100,000

The VBA code consolidates all of my duplicates from column 1 and then adds the qtys. If column b or c is different than column 1 then it creates a new line. It ends up looking like the picture attached on the top half.

I am trying to get the code to still consolidate everything but there is no quantity needed to add for this new code. Here is an example of what it would look like with the data. How it would look when cleaned with the code is in the bottom half of the image. Please let me know if I am still explaining this badly. Thank you!


Part numberManufacturerInternal part number
aMicroa1
aConna1
aConna1
aMicroa1

bConnb1
bMicrob1
bMicrob12
bMicrob12
cConnc1
cFoxtrotc1
 

Attachments

  • Capture33.PNG
    Capture33.PNG
    8.4 KB · Views: 16
Upvote 0
Sorry, I just saw the last table didn't format correctly. My data looks like this. After the VBA Code items like lines 2 and 3 would combine since they're all the same data on all 3 lines. Items 1 and 4 would combine since they're also the same data.


Part numberManufacturerInternal Number
aMicroa1
aConna1
aConna1
aMicroa1
bConnb1
bMicrob1
bMicrob2
cConnc1
cFoxtrotc1
 
Upvote 0
I am more confused now.

ter the VBA Code items like lines 2 and 3 would combine since they're all the same data on all 3 lines. Items 1 and 4 would combine since they're also the same data.
I have literally no idea what you mean when you say "combine". You have to show me, not tell me.

1697042684869.png
 
Upvote 0
I'm sorry! Consolidate, not combine. Please let me know if this is more helpful?

Part NumberManufacturerInternal Part NumberPart NumberManufacturerInternal Part Number
aMicroa1aMicroa1
aConna1aConna1
aConna1bConnb1
aMicroa1bMicrob1
bConnb1would consolidate intobMicrob2
bMicrob1bConnb2
bMicrob2cConnc1
bMicrob2cFoxtrotc1
cConnc1
cFoxtrotc1
 
Upvote 0
Perhaps this.
VBA Code:
Sub Consolidate2()
    Dim ws1 As Worksheet
    Dim ws2 As Worksheet

    Set ws1 = ActiveSheet
    Set ws2 = ActiveWorkbook.Worksheets.Add
    ws2.Name = "ONLINEINV3"

    With ws1
        .Range("A1:C" & .Range("A" & .Rows.Count).End(xlUp).Row).Copy ws2.Range("A1")
    End With

    With ws2
        .Range("A:C").RemoveDuplicates Columns:=Array(1, 2), Header:=xlYes
        .UsedRange.Columns.AutoFit
    End With
End Sub
 
Upvote 0
Solution
YES!!! You saved a week of my life! Thank you SO much.

Do you have Venmo? I'd like to send you something for helping me! I insist!
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,240
Members
452,621
Latest member
Laura_PinksBTHFT

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