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

summing duplicated data for specific sheets

0

hello

I  want  some  help  about  this  code. the  code  collect  the  data  from all  sheets  into  sheet SUMMARY   but  my  problem  if  repeat  some  data   in sheet (sales,pur,returns )

it doesn't  sum duplicat data.I    highlighted  the  wrong values  for   some  repeted data  in sheet (sales,pur,returns )  which  show  in sheet summary and  highlighted  the  repeated  data  from three  sheets 

theses  values are  not  right(100,55,10). the  right  values   should  be 200, 110,21) 

so  it  should  summing  duplicates  values 

Sub CollateData_v2()
  Dim d As Object
  Dim ShList As Variant, a As Variant, vals As Variant
  Dim i As Long, j As Long
  Dim s As String

  Set d = CreateObject("Scripting.Dictionary")
  ShList = Split("stock|sales|pur|returns", "|")
  For j = 0 To UBound(ShList)
    With Sheets(ShList(j))
      a = .UsedRange.Value2
      For i = 2 To UBound(a)
        s = Join(Application.Index(a, i, Array(2, 3, 4)), ";")
        If Len(s) > 2 Then
          If Not d.exists(s) Then d(s) = ";;;"
          vals = Split(d(s), ";")
          vals(j) = a(i, 5)
          d(s) = Join(vals, ";")
        End If
      Next i
    End With
  Next j
  Application.ScreenUpdating = False
  With Sheets("summary")
    .UsedRange.EntireRow.Delete
    With .Range("B2:C2").Resize(d.Count)
      .Value = Application.Transpose(Array(d.Keys, d.Items))
      With .Columns(2)
        .TextToColumns DataType:=xlDelimited, ConsecutiveDelimiter:=False, Semicolon:=True, Comma:=False, Space:=False, Other:=False
        With .Offset(, 4)
          .FormulaR1C1 = "=RC[-4]-RC[-3]+RC[-2]+RC[-1]"
          .Value = .Value
        End With
        .Resize(, 2).EntireColumn.Insert
      End With
      .Columns(1).TextToColumns DataType:=xlDelimited, ConsecutiveDelimiter:=False, Semicolon:=True, Comma:=False, Space:=False, Other:=False
      With .Columns(0)
        .Cells(1).Value = 1
        .DataSeries Rowcol:=xlColumns, Type:=xlLinear, Step:=1
      End With
    End With
    With .Range("A1:I1")
      .Value = Array("item", "BRAND", "TYPE", "MONAFACTURE", "STOCK", "SALES", "PUR", "RETURNS", "BALANCE")
       .Font.Bold = True
  .Interior.Color = RGB(166, 166, 166)
      .EntireColumn.AutoFit
    End With
    With .UsedRange
      .BorderAround xlContinuous
      .Borders(xlInsideVertical).LineStyle = xlContinuous
      .Borders(xlInsideHorizontal).LineStyle = xlContinuous
    End With
  End With
  Application.ScreenUpdating = True
End Sub

any  help  ,please?

Answer
Discuss

Answers

0
Selected Answer

Leap

Try checking if vals(j) contains a number already , then if so add that number to the new number (otherwise make it just the new number). I've shown changes in bold in the extract below (also in attached file).

For j = 0 To UBound(ShList)
    With Sheets(ShList(j))
      a = .UsedRange.Value2
      For i = 2 To UBound(a)
        s = Join(Application.Index(a, i, Array(2, 3, 4)), ";")
        If Len(s) > 2 Then

        If Not d.exists(s) Then d(s) = ";;;"
          vals = Split(d(s), ";")
          If IsNumeric(vals(j)) Then
           vals(j) = vals(j) + a(i, 5)
           Else
           vals(j) = a(i, 5)
          End If

            d(s) = Join(vals, ";")

        End If
      Next i
    End With
  Next j
Hope this works for you.
Discuss

Discussion

excellent ! that's what I'm looking for.
much appreciated .
leap (rep: 46) Oct 14, '21 at 11:23 am
Add to Discussion


Answer the Question

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