Resume Real Spent with Initial Budget

caluluaiol

New Member
Joined
Jul 26, 2022
Messages
10
Office Version
  1. 365
Platform
  1. Windows
To All,
First, thanks to everyone who helps and contribute to this forum. Although this is my first post, the true is that i use a lot, when i have a problem or need some kind of guidance.

Today, i have a difficulty, and i don´t even know how to look for the solution or find simple tags to find my problem.
I will try to explain next, hoping that someone at least tell me that this is possible, or give some kind of guidance from where to start.

I have an Initial Monthly Budget, and then i have the Real Spent, by month too.
What i need is to automate (vba, formula, or hidden sheet) a table, that compares the Real Spent with the Budget, and gives how much Spent is from which month of the budget.
I really believe this is more a question of math, than functions and vba. But i really cannot see what path should i take to pull this off, so any help will be appreciated.

2022-07-26 152808.png

I upload an image so, you it´s more easily to understand.
So i have a budget in YELLOW, and the expenses in BLUE. What i need it´s to automate the third table, that tells me that in Month 1, i spent 15.000€ from the JAN budget, and 10.000€ from the FEV budget, and so on.

Any thoughts?
Thank you in advance
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Welcome to the MrExcel forum!

You can probably do this with formulas, but they'd be complicated. I actually started down that path and decided I could write it in VBA much quicker, and it would be more maintainable.

Starting with this sheet:

Book1
ABCDEF
1
2BudgetREAL SPENT
3
4JAN25000M115000
5FEB35000M235000
6MAR50000M317000
7APR17500M440000
8M520500
9
10TOTAL127500TOTAL127500
Sheet2
Cell Formulas
RangeFormula
C10C10=SUM(C4:C7)
F10F10=SUM(F4:F8)


I got to this:

Book1
ABCDEFGHIJ
1
2BudgetREAL SPENTCOMPARE
3
4JAN25000M115000JANM115000
5FEB35000M235000JANM210000
6MAR50000M317000FEBM225000
7APR17500M440000FEBM310000
8M520500MARM37000
9MARM440000
10TOTAL127500TOTAL127500MARM53000
11APRM517500
12
13127500
Sheet2
Cell Formulas
RangeFormula
C10C10=SUM(C4:C7)
F10F10=SUM(F4:F8)


So to test it, open a new workbook and set up the sheet like the first mini-sheet I created. Then press Alt-F11 to open the VBA editor. Press Alt-IM to Insert a Module. Then paste this code in the window that opens:


VBA Code:
Sub CompareBudget()
Dim BudTab As Variant, SpnTab As Variant, ResTab(1 To 10000, 1 To 3)
Dim br As Long, sr As Long, rr As Long, amt As Double, tot As Double

    BudTab = Range("B4:C7").Value
    SpnTab = Range("E4:F8").Value
    
    br = 1
    sr = 1
    rr = 0
    tot = 0
    
    Do
        amt = IIf(BudTab(br, 2) < SpnTab(sr, 2), BudTab(br, 2), SpnTab(sr, 2))
        rr = rr + 1
        ResTab(rr, 1) = BudTab(br, 1)
        ResTab(rr, 2) = SpnTab(sr, 1)
        ResTab(rr, 3) = amt
        tot = tot + amt
        
        BudTab(br, 2) = BudTab(br, 2) - amt
        If BudTab(br, 2) = 0 Then
            br = br + 1
            If br > UBound(BudTab) Then Exit Do
        End If
        
        SpnTab(sr, 2) = SpnTab(sr, 2) - amt
        If SpnTab(sr, 2) = 0 Then
            sr = sr + 1
            If sr > UBound(SpnTab) Then Exit Do
        End If
        
    Loop
    
    ResTab(rr + 2, 3) = tot
    Range("H4").Resize(rr + 2, 3) = ResTab
    
End Sub

There are 3 places in the code that reference ranges (B4:C7, E4:F8, and H4). Change these to match your workbook. If you can explain how your sheet is actually laid out, I can probably get the macro to automatically determine the ranges. Once you have the ranges set, close the VBA editor with Alt-Q.

Back in Excel, press Alt-F8 to open the macro editor. Select CompareBudget and click Run.


Hope this helps!
 
Upvote 0
Solution
Thank you so much for your time and to be able to provide your help.
I will try your solution right away, and will be back to you.

Yesterdays i was trying to get to a result using a matrix, but always get stuck in some part. Even with an IF formula it´s just start to be too big, and difficult to track!
 

Attachments

  • 2022-07-27 091841.png
    2022-07-27 091841.png
    13.7 KB · Views: 5
Upvote 0
It works perfectly! Really amazing.
With your solution, i think i can adapt this to any variable that maybe appear in my need.
Thank you once again!

There are 3 places in the code that reference ranges (B4:C7, E4:F8, and H4). Change these to match your workbook. If you can explain how your sheet is actually laid out, I can probably get the macro to automatically determine the ranges. Once you have the ranges set, close the VBA editor with Alt-Q.

I have the budget in one sheet, that could cover N periods of time. Sometime i have for example 18 months, sometimes more or sometimes less.
And in another sheet, would be a field with the monthly resume of the real expenses. That may have more or less months than the budget.

I don´t know if you can automate this variable ranges, but to be honest, what you came across it´s amazing and truly helpful.
 
Upvote 0
Try with Matrix.
Yellow cells are hardcoded value, the rest are formulas.
Deciles.xlsx
ABCDEF
1JANFEBMARAPRTOTAL
2M115,000---15,000
3M210,00025,000--35,000
4M3-10,0007,000-17,000
5M4--40,000-40,000
6M5--3,00017,50020,500
7TOTAL25,00035,00050,00017,500127,500
Sheet2
Cell Formulas
RangeFormula
B2:E6B2=MIN($F2-SUM($A2:A2),B$7-SUM(B$1:B1))
F7F7=SUM(B7:E7)
Thank you for our time.
This works very well too! Thank you.

I was thinking in a matrix kind a solution, but i will never come across a "simple" formula like that!
 
Upvote 0
I have the budget in one sheet, that could cover N periods of time. Sometime i have for example 18 months, sometimes more or sometimes less.
And in another sheet, would be a field with the monthly resume of the real expenses. That may have more or less months than the budget.
It sounds like you have the ranges in different places. They could still be identified, but just so you know, here's how to set up the code to pull data (or save it) from a particular sheet:

VBA Code:
    BudTab = Sheets("Budget").Range("B4:C7").Value
    SpnTab = Sheets("Expenses").Range("E4:F8").Value

    Sheets("Results").Range("H4").Resize(rr + 2, 3) = ResTab

Glad we could help!
 
Upvote 0

Forum statistics

Threads
1,214,649
Messages
6,120,733
Members
448,987
Latest member
marion_davis

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