SUMIF using named range

coop123

Board Regular
Joined
Dec 18, 2018
Messages
66
Office Version
  1. 365
Hi

I am trying to add a sumif formula to an existing macro. I am wanting to sum all minus values and all positive values. The start and end of the range being summed will change each time report is downloaded.

I have got to the top of my column of data as part of existing macro.

Dim Rng As Range
Set Rng = ActiveCell.End(xlDown)

Selection.End(xlDown).Select
ActiveCell.Offset(1, 0).Select
ActiveCell.Resize(4, 1).Select
Selection.Delete Shift:=xlUp

ActiveCell.Resize(1, 1).Select

ActiveCell.Offset(1, 1).Select

ActiveCell.FormulaR1C1 = "=SUMIF(Rng:R[-2]C2,"">0"",Rng:R[-2]C)"

Actual SUMIF formula is =SUMIF(B23:B40,"<0",B23:B40). Range B23:B40 will vary each time report is run.

Any help to correct my formula would very much be appreciated.

Thank you

Coop123
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
I think it would be very helpful for us to see an example of your data structure, and you can show us an example of exactly what it is you are trying to do (so many of these solutions are really dependent on how the structure of the sheet is laid out).

MrExcel has a tool called “XL2BB” that lets you post samples of your data that will allow us to copy/paste it to our Excel spreadsheets, so we can work with the same copy of data that you are. Instructions on using this tool can be found here: XL2BB Add-in

Note that there is also a "Test Here” forum on this board. This is a place where you can test using this tool (or any other posting techniques that you want to test) before trying to use those tools in your actual posts.
 
Upvote 0
As requested I have pasted sample of my spreadsheet using XL2BB.

Thanks coop123

SAMPLE.xlsx
ABCDE
1NumberStk QtyPro Rq QtyPro Rq Qty
2Oct-23Nov-23Dec-23
317001URK1-1918661518
417002URK2-242520120
517003URK1285317676
617201URK2244157126
720001URK1-440621021592
820201URK2186196292
925001URK04996277
1025201URK2-653334543219
1125202URK2-501267161
1225203URK2363212240
1325204URK2000
1425601URK24474040
1525602URK1-240133
1617004URK14401420
1725204URK2174040
1825603URK1564080
19
20-1360282507314
2147737
22
233801URK2-609424471710
243802URK24800
253803URK0-2504945
263801URK0742655968
273802URK1000
283802URK26500
293803URK025424481
303801URK1-224520120
313801URK1-511126972431
323804URK2-1700
333805URK0-231136104
343806URK0-31300
353801URK012980120
363802URK2165040
373803URK0-509855881
384501URK0-210120213
394502URK288040
403801URK1-484267161
41
42
43
Sheet1
Cell Formulas
RangeFormula
C20C20=SUM($C$3:$C18)
D20D20=SUM($D$3:$D18)
B20B20=SUMIF($B$2:$B18,"<0",$B$2:B18)
B21B21=SUMIF($B$2:$B19,">0",$B$2:B19)
Cells with Conditional Formatting
CellConditionCell FormatStop If True
B:DCell Value<0textYES
 
Upvote 0
So, you want to add the formulas after each section?
Are there always 4 rows in between each section?
If not, what does the data look like initially?
 
Upvote 0
I have split the data by adding 4 rows, and added formula for top section. I need to add the formula at the bottom of the second section. There will only be 2 sections of data.

My example starts at row 23 but it could be 33 or 13. Similarly end row 40 in my example but could be 50 or 30. I need to set this range each time and use in SUMIF formula. at the bottom of last section of data.

Thanks

coop123
 
Upvote 0
So, you only want the formulas in between the two sections, in the blank rows you previously created?
Do you only want the formulas shown in column B, or the ones for columns C and D also?
Also, I assume that it was by mistake you include row 2 (the date) in your formulas? You really do not to include the date in your SUMs, do you ?
 
Upvote 0
If all my assumptions are correct, then try this:
VBA Code:
Sub MyInsertFormulas()

    Dim lr As Long
    
'   Find last row in first block in column A
    lr = Range("A3").End(xlDown).Row
    
'   Insert formulas
    Range("B" & lr + 2).Formula = "=SUMIF(B3:B" & lr & ",""<0"",B3:B" & lr & ")"
    Range("B" & lr + 3).Formula = "=SUMIF(B3:B" & lr & ","">0"",B3:B" & lr & ")"
    Range("C" & lr + 2).Formula = "=SUM(C3:C" & lr & ")"
    Range("D" & lr + 2).Formula = "=SUM(D3:D" & lr & ")"
    
End Sub
 
Upvote 0
I want the formula to be in row 42 in my example. Summing data from row 23 to 40 only. At the bottom of the last section.

The formulas in rows 20 & 21 I have already taken care of.
 
Upvote 0
I want the formula to be in row 42 in my example. Summing data from row 23 to 40 only. At the bottom of the last section.

The formulas in rows 20 & 21 I have already taken care of.
Very similar code:
VBA Code:
Sub MyInsertBottomFormulas()

    Dim lr As Long
    Dim fr As Long
    
'   Find last row in last block of data in column A
    lr = Cells(Rows.Count, "A").End(xlUp).Row
    
'   Find first row in last block of data in column A
    fr = Cells(lr, "A").End(xlUp).Row
    
'   Insert formulas
    Range("B" & lr + 2).Formula = "=SUMIF(B" & fr & ":B" & lr & ",""<0"",B" & fr & ":B" & lr & ")"
    Range("B" & lr + 3).Formula = "=SUMIF(B" & fr & ":B" & lr & ","">0"",B" & fr & ":B" & lr & ")"
    Range("C" & lr + 2).Formula = "=SUM(C" & fr & ":C" & lr & ")"
    Range("D" & lr + 2).Formula = "=SUM(D" & fr & ":D" & lr & ")"
    
End Sub
 
Upvote 0
Solution
Thank you very much it had given me a problem trying to get the range into the formula. Your effort had been much appreciated.

Thanks again

coop123
 
Upvote 0

Forum statistics

Threads
1,215,096
Messages
6,123,074
Members
449,094
Latest member
mystic19

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