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

merging errors for quantity between two files

0

Hi,

I need help from experts to find out why code doesn't merge quantity correctly for some brands!

when merge QTY some brands as higlighted by yellow will not sum , will add numbers beside each other of them in MEG file  .
like 95 the correct sum 9+5=14 but add beside other of them like 95!!
the same thing about 21 should 2+1=3 but add beside other of them like 21!!

the code will run from DA file and pull data from KashfMabiatReport1 file to MEG file
I'm not sure what's the problem!!

any help experts.

Answer
Discuss

Answers

0
Selected Answer

Hi again Leopard

The problem arises in the third element of your array b, e.g. b(4,3). This is text (for some reason) so the first line in bold (in the code extract below) combines the text (e.g. "9" & "5" = "95") rather than add the numbers (e.g. 9 + 5 = 14).

This is easily fixed by converting each text in that line to the Double data type using the CDbl conversion command- see third line in bold below:

  For idx = 1 To wb2.Sheets.Count
    Set sh = wb2.Sheets(idx)
    a = sh.Range("A2", sh.Range("G" & Rows.Count).End(3)).Value

    For i = 1 To UBound(a, 1)

    If (a(i, 1) = "") + (IsNumeric(a(i, 1))) Then

        ky = a(i, 7) & "|" & a(i, 4)
        If Not dic.exists(ky) Then
          y = y + 1
          dic(ky) = y
        End If
        nRow = dic(ky)
        b(nRow, 1) = nRow
        b(nRow, 2) = a(i, 7)
        ' b(nRow, 3) = b(nRow, 3) + a(i, 5)
        ' replace the above with this...
        b(nRow, 3) = CDbl(b(nRow, 3)) + CDbl(a(i, 5))
        b(nRow, 4) = a(i, 4)
        b(nRow, 5) = b(nRow, 3) * b(nRow, 4)
        tQty = tQty + a(i, 5)
        tBal = tBal + (a(i, 4) * a(i, 5))
      End If
    Next
  Next

That change is made in the revised DA file attached and should now give correct sums.

Discuss

Discussion

Hi John,
sometimes simple details obstructs the project's work 
thank you for help me.
leopard (rep: 100) Nov 24, '25 at 1:32 pm
Glad that worked for you. Thanks for selecting my Answer, Leopard. 
John_Ru (rep: 6792) Nov 24, '25 at 3:39 pm
Add to Discussion


Answer the Question

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