Is it possible to make a button that changes the formula?

Want2BExcel

Board Regular
Joined
Nov 24, 2021
Messages
112
Office Version
  1. 2016
Platform
  1. Windows
Is it possible to make a button that changes the formula?

Right now the formula is "pointing" to the expected.expenses. but when a month is over I import all the actual expenses in a new sheet. Now I, of course, know what I have to do to change the formula to point to the new sheet. But if I gave this budget to friends, they don't know what to do. So if there was a button, they just could click on and then a drop-down menu opens to choose witch month he/she would like to apply the "new" formula...voila :)

So from this:
Original in danish: =SUM.HVIS(ANSLÅET.UDGIFTER[Tekst];[@[Postering tekst]];ANSLÅET.UDGIFTER[Januar])
In english: =SUMIF(EXPECTED.EXPENSES[Text];[@[Postering text]];EXPECTED.EXPENSES[January])

The user pushes the button and a dropdown menu opens with all 12 months to choose from. Pick January and the formulas in all rows in that whole column (January) changes to...

To this:
Original in danish: =SUM.HVIS(BUDGET_JAN_22[Tekst];[@[Postering tekst]];BUDGET_JAN_22[Beløb])
In english: =SUMIF(BUDGET_JAN_22[Text];[@[Postering text]];BUDGET_JAN_22[Amount])

It would be amazing!
 
you did this with the macro-recorder, i suppose.
That one add too much unnecessary things, like select and activate, but, no doubt, it's a usefull tool.
Afterwards you can cleanup and reduce it to a more readable code like this, just by looking what is similar.
I didn't try the last challenge, the 12 months, but there is a way (drag or copy), once you have january to do it with the other 11 months.
But ???? Do you really need to make new formulas ? Are they different of the previous ?

VBA Code:
     Range("D5:P5,D10:P10,D16:P16,D22:P22,D36:P36,D43:P43,D49:P49,D52:P52,D57:P57,D61:P61,D68:P68,D84:P84,D86:P86,D87:P87,D102:P102,D104:P104,D105:P105,D108:P108,D109:P109").ClearContents 'all-in-one

     Range("D85:P85").FormulaR1C1 = "=SUM(R[-79]C:R[-10]C)"
     Range("D103:P103").FormulaR1C1 = "=SUM(R[-15]C:R[-2]C)"
     Range("D106:P106").FormulaR1C1 = "=SUM(R[-3]C+R[-21]C)"

     Range("D107").FormulaR1C1 = "=SUM(R[-1]C+'Budgetkonto 2021'!R[-14]C[10])"
     Range("E107").FormulaR1C1 = "=SUM(R[-1]C+[@Januar])"
     Range("F107").FormulaR1C1 = "=SUM(R[-1]C+[@Februar])"
     Range("G107").FormulaR1C1 = "=SUM(R[-1]C+[@Marts])"
     Range("H107").FormulaR1C1 = "=SUM(R[-1]C+[@April])"
     Range("I107").FormulaR1C1 = "=SUM(R[-1]C+[@Maj])"
     Range("J107").FormulaR1C1 = "=SUM(R[-1]C+[@Juni])"
     Range("K107").FormulaR1C1 = "=SUM(R[-1]C+[@Juli])"
     Range("L107").FormulaR1C1 = "=SUM(R[-1]C+[@August])"
     Range("M107").FormulaR1C1 = "=SUM(R[-1]C+[@September])"
     Range("N107").FormulaR1C1 = "=SUM(R[-1]C+[@Oktober])"
     Range("O107").FormulaR1C1 = "=SUM(R[-1]C+[@November])"

application.goto range("D6")
 
Upvote 0

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
you did this with the macro-recorder, i suppose.
That one add too much unnecessary things, like select and activate, but, no doubt, it's a usefull tool.
Afterwards you can cleanup and reduce it to a more readable code like this, just by looking what is similar.
I didn't try the last challenge, the 12 months, but there is a way (drag or copy), once you have january to do it with the other 11 months.
But ???? Do you really need to make new formulas ? Are they different of the previous ?

VBA Code:
     Range("D5:P5,D10:P10,D16:P16,D22:P22,D36:P36,D43:P43,D49:P49,D52:P52,D57:P57,D61:P61,D68:P68,D84:P84,D86:P86,D87:P87,D102:P102,D104:P104,D105:P105,D108:P108,D109:P109").ClearContents 'all-in-one

     Range("D85:P85").FormulaR1C1 = "=SUM(R[-79]C:R[-10]C)"
     Range("D103:P103").FormulaR1C1 = "=SUM(R[-15]C:R[-2]C)"
     Range("D106:P106").FormulaR1C1 = "=SUM(R[-3]C+R[-21]C)"

     Range("D107").FormulaR1C1 = "=SUM(R[-1]C+'Budgetkonto 2021'!R[-14]C[10])"
     Range("E107").FormulaR1C1 = "=SUM(R[-1]C+[@Januar])"
     Range("F107").FormulaR1C1 = "=SUM(R[-1]C+[@Februar])"
     Range("G107").FormulaR1C1 = "=SUM(R[-1]C+[@Marts])"
     Range("H107").FormulaR1C1 = "=SUM(R[-1]C+[@April])"
     Range("I107").FormulaR1C1 = "=SUM(R[-1]C+[@Maj])"
     Range("J107").FormulaR1C1 = "=SUM(R[-1]C+[@Juni])"
     Range("K107").FormulaR1C1 = "=SUM(R[-1]C+[@Juli])"
     Range("L107").FormulaR1C1 = "=SUM(R[-1]C+[@August])"
     Range("M107").FormulaR1C1 = "=SUM(R[-1]C+[@September])"
     Range("N107").FormulaR1C1 = "=SUM(R[-1]C+[@Oktober])"
     Range("O107").FormulaR1C1 = "=SUM(R[-1]C+[@November])"

application.goto range("D6")
Yeah, I cleaned it up a little afterwards, but not as beautiful as yours ? ...it's alot learning by doing for me, at this point. But I find it both rewarding and comfortable (as long as there's progress ?)
The last one ? yeah, I couldn't figure that one out. Yes, it's a new formula every month, because it takes the result from the previous month and adding the results of the current month, giving the exact balance of your account, to the cent, at the end of a month. So I can't figure out how to clean that up, since it always has to look at the month before ??
But thank you for showing me a much nicer cleanup ??
 
Upvote 0
with your screenshots (except, the sheetname is "Anslåede.udgifter_" to make a difference with the table "Anslåede.udgifter", otherwise VBA gives an error)
VBA Code:
Sub NewMonth()
     Dim Answ, FL, ShName, sMon, cMyKol As Range, LO_Month As ListObject

     a = [transpose(row(A1:A12) &". " & text(row(A1:A12)*28,"[$-0406]mmmm"))]     'danish monthnames + index
     Answ1 = Application.InputBox("0. Stop, nothing to change" & vbLf & vbLf & Join(a, vbLf), UCase("What month do you want to change"), 0, 100, 100, , , 1)
     If Answ1 = 0 Then Exit Sub                                 '=stop

     Select Case Answ1
          Case 1 To 12
               smonth = Split(a(Answ1))(1)
               sMon = Left(smonth, 3)               'first 3 letters of choosen month
               For i = 1 To Worksheets.Count
                    If StrComp(Left(Worksheets(i).Name, 3), sMon, vbTextCompare) = 0 Then s = s & vbLf & i & ". " & Worksheets(i).Name     'string with all sheets starting with sMon
               Next
               If Len(s) = 0 Then MsgBox "no sheets starting with " & sMon & " found" & vbLf & "sorry", vbInformation: Exit Sub     'no sheets found = end of story
               Answ = Application.InputBox("0. Stop, nothing to change" & vbLf & vbLf & s, UCase("What sheet number do you want ?"), 0, 100, 100, , , 1)     'choice between 1 or several options
               If Answ = 0 Then Exit Sub                        '0=stop
               FL = Filter(Split(Mid(s, 2), vbLf), Answ & ". ", 1, vbTextCompare)     'filter selection of sheets with your answer; normally 1 option left
               If UBound(FL) <> 0 Then MsgBox "fatal error": Exit Sub     'normally only 1 sheet left, so why ???
               ShName = Trim(Mid(Replace(FL(0), " ", WorksheetFunction.Rept(" ", 100), 1, 1, vbTextCompare), 100))     'get rid of the indexnumber
               On Error Resume Next
               Set LO_Month = Sheets(ShName).Range("A1").ListObject     ' the wanted listobjects contains cell A1 of that sheet
               If LO_Month Is Nothing Then MsgBox "no listobject in A1 of " & ShName, vbCritical: Exit Sub     'listobject of that month not found

               Set cMyKol = Sheets("BudgetKonto 22").ListObjects(1).ListColumns(smonth).DataBodyRange
               If cMyKol Is Nothing Then MsgBox "your month " & smonth & " not found", vbCritical: Exit Sub
               On Error GoTo 0
          
               MsgBox cMyKol.Address
               cMyKol.FormulaR1C1 = "=SUMIF(" & LO_Month.Name & "[Tekst],[@[Postering tekst]], " & LO_Month.Name & "[Beløb])"

          Case Else: MsgBox "only 12 months in a year", vbInformation
     End Select

End Sub
Want2BeExcel.xlsm
ABCDEF
1BeskrivelseBetaling metodePostering tekstJanuarFebruarMarts
2Bolig 
3HuslejeBetalings ServiceUDLEJER-2.000,00 -5.700,00
4AffaldBetalings ServiceKOMMUNE-500,00-4.500,00 
5   
6
BudgetKonto 22
Cell Formulas
RangeFormula
D2:D5D2=SUMIF(Tabel3[Tekst],[@[Postering tekst]], Tabel3[Beløb])
E3:E5E3=SUMIF(ANSLÅET.UDGIFTER[Tekst],[@[Postering tekst]],ANSLÅET.UDGIFTER[Februar])
F3:F5F3=SUMIF(ANSLÅET.UDGIFTER[Tekst],[@[Postering tekst]],ANSLÅET.UDGIFTER[Marts])


Want2BeExcel.xlsm
Hey BSALV :) I have tried to include this in your code, so that it only affects the month I choose, but I can't get it to work. Where should I put it?
I have tried to use the line in bold, but I think I'm doing it wrong because it only affects the ActiveCell I was in when I ran the macro
Range("D6:D164").Select
Selection.NumberFormat = "#,##0.00_ ;[Red]-#,##0.00 "
 
Last edited:
Upvote 0
Hey BSALV :) I have tried to include this in your code, so that it only affects the month I choose, but I can't get it to work. Where should I put it?
I have tried to use the line in bold, but I think I'm doing it wrong because it only affects the ActiveCell I was in when I ran the macro
Range("D6:D164").Select
Selection.NumberFormat = "#,##0.00_ ;[Red]-#,##0.00 "
I finally figured it out ?
cMyKol.NumberFormat = "#,##0.00_ ;[Red]-#,##0.00 "
 
Upvote 0

Forum statistics

Threads
1,214,982
Messages
6,122,581
Members
449,089
Latest member
Motoracer88

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