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

updating summing in TOTAL row after adding new data

0

Hello

I  need help  for  this question  , when  I add new  data and  matching combobox1:combobox3 with columns B:D for  the  sheet DETAILS ,  then  will search  for  separated  range  and  add   the  data before  TOTAL row  . what  I  want   it  summing  the  values  in  column E  for  TOTAL row  should  sum start  from  the  third  row  not  the  second row  for  each  separated range . so  every  time  add  new  data before TOTAL row  by userform   should  updating  the  summing values  . I  put  the  formula  how  sum for  each  separated  range , but  I  don't  need  show  the  formula .

thanks

Answer
Discuss

Discussion

Hi Maklal

I assume you mean that the new row goes above the matchingrow starting TOTAL, right? Will TOTAL always exist in column A before the macro is run?
John_Ru (rep: 6152) Jan 5, '23 at 5:56 pm
Hi John,
yes should  be
Malkal (rep: 22) Jan 5, '23 at 10:20 pm
Add to Discussion

Answers

0
Selected Answer

Malkal

In the revised macro below (and file), I've just added another loop (like yours) to add the quantities in Column D- changes are in bold. Other users please note that (at Malka's request in the Discussion below), this Total deliberately ignores the first entry in each "block" (this seems to run contrary to all quantity columns I've worked with!)

Private Sub CommandButton1_Click()

Dim lRow As Long
Dim lValue As Variant
Dim Ttl As Double, i As Long, n As Long

lRow = Worksheets("DETAILS").Cells(Rows.Count, 1).End(xlUp).Row
With Worksheets("DETAILS")
For i = lRow To 2 Step -1
    If .Cells(i, 2).Value = UserForm1.ComboBox1.Value And .Cells(i, 3).Value = UserForm1.ComboBox2.Value And .Cells(i, 4).Value = UserForm1.ComboBox3.Value Then
      lValue = .Cells(i, 5).Value - UserForm1.TextBox1.Value
      UserForm1.TextBox2.Value = lValue
      .Rows(i).Copy
      .Rows(i + 1).EntireRow.Insert , xlFormatFromLeftOrAbove
      .Cells(i + 1, 1).Value = Format(Date, "dd/mm/yyyy")
      .Cells(i + 1, 5).Value = lValue
      Application.CutCopyMode = False
      'calculate Total BUT ignore first entry in each "block"
        For n = i To 1 Step -1
            If IsNumeric(.Cells(n, 5)) Then
                'add from E to total
                Ttl = Ttl + .Cells(n + 1, 5).Value
                Else
                'write total
                .Cells(i + 2, 5).Value = Ttl
                Exit For
            End If
        Next n
      Exit For
    End If
Next i
End With
End Sub

Note: I'm still nit sure about the number you add to the Qty column (when I put a QTY is the UserForm, your macro added the REMAINED value to the new row instead).

Hope this is what you want. 

Discuss

Discussion

thanks  John
sorry  I  said  that  , but  it  dosen't  show the Total  value in column E for Total row  start from row3 as  I asked  in OP.
Malkal (rep: 22) Jan 6, '23 at 7:35 am
Malkal. Sorry but I don't understand what you mean.

The revised macro worked corrrectly for me (I think). In the userform, I added CSS-100, INV-A123, ITTT-100/AS-2 with QTY 100. In row 12 it added a new line but QTY as 288 (the "REMAINDED" not QTY, as I questioned at the end of my Answer) and added 1376 as the total in E13, on the moved TOTAL row. 1376 is the total of the values 300, 400, 388 and (new) 288 in the Qty column E.   That seemed to be what your question meant but it was difficult to understand.
John_Ru (rep: 6152) Jan 6, '23 at 8:50 am
but it was difficult to understand.
I expected  that.
look about this  
1376 is the total of the values 300, 400, 388 and (new) 288 in the Qty column E
this  is  wrong , should  be =400+388+288=1076  should  ignore  300  because this  value  is  in row2   after row1 the  headers  and  I  have  ever  asked  should  sum  starting from row3  for  each separated range .
may be  you don't  understand  it  why . my  goal  300  represents  the  whole  value  and   ignore  it  when  summing   for  the  total  row  should  be   the  same  value  as  in  row2  to  match  final  value  if  it's  right  or  wrong .
in  reality  your  revised  macro  doesn't work  for  first separated range   (CSS-100 ,INV-A123, ITTT-100/AS-1 ) you  can  test  it  .
in  short  words  when  add  new  data and  when  sum the  values in TOTAL row should  be from row3 not row2  for  each  range . 
I  hope   this  help  you .
Malkal (rep: 22) Jan 6, '23 at 10:00 am
Malkal. Please see my revised answer and file (though I think your approach is most unusual)
John_Ru (rep: 6152) Jan 6, '23 at 10:44 am
(though I think your approach is most unusual)
without  any  doubt  yes  you're  right , but  I  have  to  use  this  way to  matching  the  whole  value  with  the  entering  data .
thanks  John ! this  is  what  I look for  it .
Malkal (rep: 22) Jan 6, '23 at 11:41 am
Greatl! Thanks for selecting my answer, Malkal. 
John_Ru (rep: 6152) Jan 6, '23 at 12:28 pm
Add to Discussion


Answer the Question

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