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 values for repeated data in all sheets

0

hello 

I need  way  to summing  values  for  repeating data   so  when  summing  value  to  know  repeating data  should  look  to  COL B,C,D,E    if  repeats again  then  should summing    in all sheets the  result  should  show  in sheet   summary  when  i select  from dropdown in d1  see  the  sheet "summary"

thanks

Answer
Discuss

Discussion

Leopard, 

Column F in the sheets is headed "BALANCE" so I don't know why you want to sum them (since generally the latest balance would be the figure or interest). Or is that figure really just a quantity (e.g a number imported) rather than the balnce unsold from that quantity?
John_Ru (rep: 6142) Mar 17, '21 at 7:15 am
John
thanks  for  your  interesting  it's really just a quantity 
leopard (rep: 88) Mar 17, '21 at 7:40 am
Add to Discussion

Answers

0
Selected Answer

Leopard

I've taken a different (=easier) approach to create your summary. Rather than populate tables in the SUMMARY, I've just summarised the sheets by rows (with the quantities in each sheet added together- note that I changed the headings from Balance to Quantity as per your Discussion point, and Total Quantity in SUMMARY).

On change of D1, the macro first clears the summary sheet (after row 4) then loops through the sheets adding values in the next free row SumNext (which is detected for the worksheet). The quanties per sheet are added via the second line in bold below):

Private Sub Worksheet_Change(ByVal Target As Range)

If Target.Address(0, 0) <> "D1" Then Exit Sub
Application.EnableEvents = False

Dim ws As Worksheet, Rg As Range, SumNext As Integer

Range("A4:F20").Clear ' clear results

   For Each ws In Sheets(Array("IMP", "EX", "PURRET", "SELRET"))
   SumNext = Cells(Rows.Count, 1).End(xlUp).Row + 1
   Cells(SumNext, 1) = ws.Name
   With ws
      Set Rg = .Range("B2:B" & .Cells(Rows.Count, 1).End(xlUp).Row)
        Cells(SumNext, 2) = Target.Value ' start a row (even if no quantities are found)
        For Each Cell In Rg
            If Cell.Value = Target.Value Then
             For n = 1 To 3
                Cells(SumNext, n + 2) = Cell.Offset(0, n).Value
             Next n
             Cells(SumNext, 6) = Cells(SumNext, 6) + Cell.Offset(0, 4).Value
            End If
        Next Cell
    End With
    Next ws

Cells(SumNext + 1, 1) = "Nett"
Cells(SumNext + 1, 6).Formula = "=F4-F5-F6+F7"

With Range("A8:F8")
    .Font.Bold = True
    .Font.Size = 14
    .Font.Color = vbWhite
    .Interior.Color = vbBlue
End With

Application.EnableEvents = True
End Sub
Revision 1: Corrected counter line above, read For n = 1 To 4 in error. Revised file attached. Revision 2: Added code (after Next ws) to add Nett row and formula (not value, for understanding of others).Revision 3: Made changes in italics above to allow for cases where the selected parts from D1 is not found on a sheet (blank entries are shown but the Nett calculation works). You can test by removing a part from one sheet or adding a blank entry in D1.

Please note that in the revised file attached, your data validation appears (transposed) in row 1 of SUMMARY (but the column are hidden).

Note to that Application.EnableEvents are disabled  (to allow SUMMARY to be modified without triggering the WorksheetChange event) then re-enabled at the end of the sub.

Hope this works for you.

Discuss

Discussion

John  many  thanks   I appreciate   your efforts and assistance  and  I like  your  way to showing   data .   it's better  but unfortintuly  it  doesn't   give  the right  value  so   let's  take  your  file  in sheet  SELRET  the  batch BT-TO-1003  the value should  be 10  ,  where comes 20 from ?   there  is  no  duplicated  data in  this  sheet  for  this  batch  and the  same  thing  with  the  rest of sheets
leopard (rep: 88) Mar 17, '21 at 3:27 pm
Leopard

Oops! My fault. A  line read For n = 1 To 4 (where 4 put the quantity in SUMMARY column F then the line after the loop added it again!)

See Revision 1 to my Amswer- works properly now.
John_Ru (rep: 6142) Mar 17, '21 at 4:21 pm
thanks  very  much   for  provide  me  your  solution 
leopard (rep: 88) Mar 17, '21 at 4:30 pm
Glad it worked, Leopard. Take care if you need to edit it (especially in the With section, where Summary cells are referred to) 
John_Ru (rep: 6142) Mar 17, '21 at 4:36 pm
John 
you  forgot   the   net   as  in  my   file  I 'm also  suprised  how  I  don't  noted as  in  your  case    should  show  the  result  in  the  last  row  so  please  check  the updating  file   and  the  formula  in lastrow   and  you will  understand   what  I  mean
leopard (rep: 88) Mar 17, '21 at 5:02 pm
Leopard

I've done that (but suspect that's something you could have worked out) and made that row quite different. See Revision 2 to my Answer.
John_Ru (rep: 6142) Mar 17, '21 at 5:49 pm
Leopard.
I realised overnight that there's a flaw on my approach (now you have the Nett line) . Will fix later today. 
John_Ru (rep: 6142) Mar 18, '21 at 2:17 am
Leopard. Please see Revision 3 to my Answer (I realised that if where the selected part from D1 was not found on a sheet, the Nett calculation would not work- it does now).
John_Ru (rep: 6142) Mar 18, '21 at 5:16 am
John . thank  you  so much  for  your  efforts  I appreciate  that  it's  a point important  about  not calculate if  selected item from D1 was not found on a sheet it  was  making  abig  problem after  long  time  if  I  don't  found out   it  this  prblem early   but  I tested  your  code  in  your  file C  I clear  brand  from sheet IMP and  I select  the  item from d1  I think  it calculates normally 
leopard (rep: 88) Mar 18, '21 at 5:51 am
Leopard, okay but you should use the version v0_d file instead (it's more reliable)
John_Ru (rep: 6142) Mar 18, '21 at 5:53 am
I  did   and  took your  advice    and  thank you  so  much for every thing
leopard (rep: 88) Mar 18, '21 at 6:54 am
Add to Discussion


Answer the Question

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