re-produce code to fixing error when have new item in one of sheets

KalilMe

Active Member
Joined
Mar 5, 2021
Messages
343
Office Version
  1. 2016
Platform
  1. Windows
Hello
I need help from experts to fix mismatch error in this line
VBA Code:
Dic(j) = Split(Dic(j), ";")(0) + SALE & ";" & Split(Dic(j), ";")(1) + RET
this case occures when there is new item in one of sheet and not in existed in others.
the code will bring data for all of the sheets and sum the values for columns SALE,RET for each sheet
VBA Code:
Sub test()

Dim ws As Worksheet, a, Dic As Object, j$, k$, SALE As Double, RET As Double
Set Dic = CreateObject("scripting.dictionary")
  
For Each ws In Sheets
   If ws.Name <> "COLLECTION" Then
      a = ws.[A1].CurrentRegion
      For x = 2 To UBound(a)
         j = Join(Array(a(x, 2), a(x, 3), a(x, 4)), ";")
         If UBound(a, 2) = 5 Then
            If Not IsNumeric(a(x, 5)) Then a(x, 5) = 0
            If a(1, 5) = "SALE" Then SALE = a(x, 5) Else RET = a(x, 5)
         Else
            If Not IsNumeric(a(x, 5)) Then a(x, 5) = 0
            If Not IsNumeric(a(x, 6)) Then a(x, 6) = 0
            SALE = IIf(a(1, 5) = "SALE", a(x, 5), a(x, 6))
            RET = IIf(a(1, 5) = "SALE", a(x, 6), a(x, 5))
         End If
         k = Join(Array(SALE, RET), ";")
         If Not Dic.exists(j) Then Dic.Add j, k Else _
            Dic(j) = Split(Dic(j), ";")(0) + SALE & ";" & Split(Dic(j), ";")(1) + RET
         SALE = 0: RET = 0
      Next
   End If
Next

With Sheets("COLLECTION").[A1].Resize(Dic.Count)
   .Parent.UsedRange.Clear
   .Resize(1, 7) = [{"ITEM","BR","TY","OR","SALE","RET","BALANCE"}]
   .Offset(1, 1) = Application.Transpose(Dic.keys)
   .Offset(1, 4) = Application.Transpose(Dic.items)
   .Offset(1, 1).TextToColumns .Offset(1, 1), semicolon:=True
   .Offset(1, 4).TextToColumns .Offset(1, 4), semicolon:=True
   .Offset(1, 0) = Evaluate("row(1:" & Dic.Count & ")")
   .Offset(1, 6) = "=E2-F2"
   .Offset(1, 6) = .Offset(1, 6).Value
   .Offset(1, 4).Resize(, 3).NumberFormat = "#,0;[red]-#,0;-"
   .Resize(Dic.Count + 1, 7).Borders.LineStyle = 1
End With

End Sub
can anybody find out how fixing the code or reproduce again ,please?
thanks
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK

Forum statistics

Threads
1,214,935
Messages
6,122,337
Members
449,077
Latest member
Jocksteriom

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