VBA-Sumif across ranges

Miya

Well-known Member
Joined
Nov 29, 2008
Messages
662
Hi i am after a VBA code that will apply the below formula to very last Total. So the code looks for Total from bottom to Top, finds the first Total, applies the below formula and then fills across to Col S22

=SUMIF($A$2:$A$20,"TOTAL",B2:B20).

This data is cleared everyday therefore i want VBA to input formula

Excel Workbook
AB
2TeamNo.of items
3TEST15
4TEST244
5TEST31
6TEST40
7TEST55
8TOTAL477
9
10TEST60
11TOTAL644
12
13TEST70
14TOTAL0
15
16TEST80
17TOTAL0
18
19TEST90
20TOTAL0
21
22TOTAL1121
Sheet2
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Try this:

Code:
Sub Autofill()
Dim rCell As Range
Dim rRange As Range
Set rRange = Range("A1:A21")

For Each rCell In rRange
    If rCell = "TOTAL" Then
      rCell.Offset(0, 1) = "=SUMIF($A$2:$A$20,TOTAL,B2:B20)"
     rCell.Offset(0, 1).Autofill Destination:=Range(Cells(rCell.Row, 2), Cells(rCell.Row, 22))
    End If
Next
End Sub
 
Upvote 0
I would go for:

Code:
Sub Autofill_Wigi()
    Range("A" & Rows.Count).End(xlUp).Offset(, 2).Resize(, 17).Formula = "=SUMIF($A$2:$A$20,TOTAL,B2:B20)"
End Sub
 
Upvote 0
I would go for:

Code:
Sub Autofill_Wigi()
    Range("A" & Rows.Count).End(xlUp).Offset(, 2).Resize(, 17).Formula = "=SUMIF($A$2:$A$20,TOTAL,B2:B20)"
End Sub

Thanks that works but i am bit worried if the user either inserts a row or deletes a row then the code would not adjust to the new ranges since the ranges are hard-coded, is there a way around this?
 
Upvote 0
Then...

Code:
Sub Autofill_Wigi()
    With Range("A" & Rows.Count).End(xlUp)
        .Offset(, 2).Resize(, 17).Formula = "=SUMIF($A$2:$A$" & .Row - 1 & ",TOTAL,B2:B" & .Row - 1 & ")"
    End With
End Sub

Wigi
 
Upvote 0
Perfect, thanks :)

Then...

Code:
Sub Autofill_Wigi()
    With Range("A" & Rows.Count).End(xlUp)
        .Offset(, 2).Resize(, 17).Formula = "=SUMIF($A$2:$A$" & .Row - 1 & ",TOTAL,B2:B" & .Row - 1 & ")"
    End With
End Sub
Wigi
 
Upvote 0

Forum statistics

Threads
1,224,551
Messages
6,179,476
Members
452,915
Latest member
hannnahheileen

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