VBA Sumif using variable defined ranges

bggadish

New Member
Joined
Sep 3, 2014
Messages
2
[TABLE="class: grid, width: 800, align: left"]
<tbody>[TR]
[TD]Total Number of Periods[/TD]
[TD]5[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]This Period[/TD]
[TD]3[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Budget Code[/TD]
[TD]Budget Description[/TD]
[TD]BAC[/TD]
[TD]Start Period[/TD]
[TD]End Period[/TD]
[TD]1[/TD]
[TD]2[/TD]
[TD]3[/TD]
[TD]4[/TD]
[TD]5[/TD]
[TD]Cumulatitive PV[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]Example[/TD]
[TD]1000[/TD]
[TD]1[/TD]
[TD]5[/TD]
[TD]200[/TD]
[TD]200[/TD]
[TD]200[/TD]
[TD]200[/TD]
[TD]200[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]










Using the table I want to do a sumif function in VBA. If (B2) This Period = 3, sum periods 1, 2, and 3. The issue comes with the fact that the total number of periods changes and the current period changes so am using the code below (there is a bunch of other stuff, but this is the code that affects this portion). It selects the proper cell, The ranges are defined properly, but when i do the sumif function it doesn't work at all.

The Active cell is always the last period number. in this case it is the cell with 5

Set r = ActiveCell.Offset(1, 2)
Set SourceRange = Range("G3", Cells(3, LastCol))
SourceRange.Select
Set FillRange = Range("G4", Cells(4, LastCol))
FillRange.Select
r.Formula = "=SUMIF(Range(SourceRange),""<=""&""B2"",Range(FillRange)))"

Thanks in advance for the help
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Hey bggadish,

Good visual example, but I am a bit confused.

The way that your example is set up you have it so that there is only one budget that you would enter information for, but the way your question is formed it makes it sound like you would be keeping a list of multiple budgets on one sheet.

If multiple budgets you would have to start off with a column for both the Total and Current periods and try to define a maximum of total periods in order to make things a bit easier.

Could you clarify. This may help one of the other 16 people that viewed your post provide a workable answer that is beyond my VBA knowledge.

later

Ty
 
Upvote 0

Forum statistics

Threads
1,223,231
Messages
6,170,884
Members
452,364
Latest member
springate

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