VBA Sumif using variable defined ranges

bggadish

New Member
Joined
Sep 3, 2014
Messages
2
Total Number of Periods5
This Period3
Budget CodeBudget DescriptionBACStart PeriodEnd Period12345Cumulatitive PV
1Example100015200200200200200

<tbody>
</tbody>










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

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
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,215,444
Messages
6,124,893
Members
449,194
Latest member
JayEggleton

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