Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

Exclude merging data based on specific condition

0

Hi guys.

I have code to merging data based on coumn B .

some duplicates ID contains diffrent price . so if any duplicate ID contains different price in column G should not merge totally .

so in short words don't merge any duplicate ID contains different price in column G also should show the Total row in last row as show in CA sheet.

I put the right result in RESULT sheet but when run the macro will not give what I want.

if it's possible adjust the original code because is really fast with big data.

thanks in advanced.

Answer
Discuss

Answers

0
Selected Answer

Hi again Maklil

In the attached revised file, I redefined the dictionary index to include the price from column G (7) e.g. CR CCR-1;12000; see comments and changes in bold below:

Sub test()
    Dim ws As Worksheet, a, i As Long, w, dic As Object
    Set dic = CreateObject("Scripting.Dictionary")
        Set ws = Sheets("CA")
        a = ws.Cells(1).CurrentRegion.Value

            For i = 2 To UBound(a, 1)
                If a(i, 2) <> "" Then
                    'check if dictionary item exists for Batch AND Price
                    If Not dic.exists(a(i, 2) & ":" & a(i, 7)) Then
                        ReDim w(1 To 8)
                        w(2) = a(i, 2)
                        w(7) = a(i, 7)
                    Else
                        ' if not, add to dictionary item for Batch AND Price
                        w = dic(a(i, 2) & ":" & a(i, 7))
                    End If
                    w(2) = a(i, 2): w(3) = a(i, 3): w(4) = a(i, 4): w(5) = a(i, 5): w(6) = w(6) + a(i, 6): w(7) = a(i, 7): w(8) = w(6) * w(7)
                    dic(a(i, 2) & ":" & a(i, 7)) = w
                End If
            Next

    With Sheets("Results1").Cells(1).CurrentRegion
        .Offset(1).ClearContents
        If dic.Count Then
            With .Rows(2).Resize(dic.Count)
                .Value = Application.Index(dic.items, 0, 0)

                .Columns(1) = Evaluate("row(1:" & .Rows.Count & ")")
            End With
        End If
    End With
End Sub

Note that the values are written to a new sheet called "Results1". You'll need to add code to calucalute the total and any formatting you want.

 Hope this fixes your problem- if so, please remeber to mark this Answer as Selected.

Discuss

Discussion

Really helpful !
many thanks buddy.
MAKLIL (rep: 36) Sep 11, '24 at 3:52 am
Great! Thanks for selecting my Answer. Maklil. 
John_Ru (rep: 6417) Sep 11, '24 at 5:34 am
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login