Results 1 to 18 of 18

Thread: Excel Formula Question

  1. #1

    Thread Starter
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Resolved Excel Formula Question

    If I enter 1 in column A I'd like column B to increase by $20.50.
    If I enter 2 in column A I'd like column B to increase by $9.70
    If I enter 3 in column A I'd like column B to increase by $4.30
    If I enter anything else in column A I'd like column B to decrease by $6.50

    I assume I can't do that with a simple formula so how would I go about it?

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Excel Formula Question

    You can do it with a complex nested If formula or some VBA code. Which would you prefer?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Excel Formula Question

    i did some code for it

    VB Code:
    1. Private Sub Worksheet_Change(ByVal Target As Range)
    2. If Target.Column = 1 Then
    3. Dim rn As Range
    4.     myr = Target.Row: myc = Target.Column + 1
    5.     Set rn = Me.Cells(myr, myc)
    6.     Select Case Target.Value
    7.        Case 1: myval = 20.5
    8.         Case 2: myval = 9.7
    9.         Case 3: myval = 4.3
    10.         Case Else: myval = -6.5
    11.     End Select
    12.     rn.Value = rn.Value + myval
    13. Set rn = Nothing
    14. End If
    15.  
    16. End Sub

    but i never figured out how to be able to include a cells value in it's formula, a

    pete

  4. #4
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343

    Re: Excel Formula Question

    Code:
    =IF(A2=1,B2+20.5,IF(A2=2,B2+9.7,IF(A2=3,B2+4.3,-6.5)))
    I did it like this in another cell as you would need a starting number to begin with (to change).


    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

  5. #5
    Frenzied Member
    Join Date
    May 2004
    Location
    Carlisle, PA
    Posts
    1,045

    Re: Excel Formula Question

    Marty ...

    If you haven't discovered it already, if you put your conditional calculation in a formula, you are going to bump up the "value in column B" EVERY TIME a recalculation is done. To check this out, hit <F9> a few times and see what happens. I would guess that you do NOT want that to happen. If you only want the action to occur 1 time it will take a VBA program, or you might want to think about adding a control to your worksheet.
    Blessings in abundance,
    All the Best,
    & ENJOY!

    Art . . . . Carlisle, PA . . USA

  6. #6
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Excel Formula Question

    [/QUOTE]If you only want the action to occur 1 time it will take a VBA program, or you might want to think about adding a control to your worksheet.[QUOTE]

    the code i posted only changes it once, untill you edit the code in column A

    pete

  7. #7
    Frenzied Member
    Join Date
    May 2004
    Location
    Carlisle, PA
    Posts
    1,045

    Re: Excel Formula Question

    I'm guessing that it actually could be done in a formula in the worksheet cell, but it would be a hairy formula! Use the background color or text color as a flag in a complex IF chain ...

    Which brings up the thought:

    Does anyone know how to run a macro from a formula? There is a "CALL" function for external functions, but I couldn't get it to run a Macro ... sort of like the following:

    =IF(logical_test, "Run Macro1", "Run Macro2")

    Thanks for any and all comments, suggestions, and assistance.
    Blessings in abundance,
    All the Best,
    & ENJOY!

    Art . . . . Carlisle, PA . . USA

  8. #8
    Frenzied Member
    Join Date
    May 2004
    Location
    Carlisle, PA
    Posts
    1,045

    Re: Excel Formula Question

    Marty ... are you still around?

    It would be 'expensive' in processing time for a very large worksheet ... but if you really want to do it, here are some additional comments on Pete's approach:

    In the Project Explorer, in "ThisWorkbook" module create a Public (Global) boolean flag (or an array of flags) and Initialize it, say to "False", in the Event handler "Private Sub Workbook_Open".

    In the "Sheet" Module for the appropriate sheet, set up the Worksheet_Change event handler function. Everytime something on the sheet changes, the following code will run:
    Code:
    Private Sub Worksheet_Change(ByVal Target As Range)
      'TEST TEST TEST TEST
      MsgBox("What changed? " & Target.Address)   'Show 'Target' operation
      'END TEST
      If Not myFlag Then
        If Range("A1").Value > 0 Then
          'Put your conditional calculations here
          MsgBox ("Sheet Changed and A1 is Greater Than ZERO")
          myFlag = True
        End If
      End If
    End Sub
    This is just a simple 'proof of concept' prototype to show that it can be done automatically ... without requiring the user to run a Macro ... if you really want to do it. It would get kind of hairy if you want to be able to "Undo" changes to your 'causal' variables. If you get stuck, I can get more explicit ...
    Last edited by Webtest; Jun 20th, 2005 at 10:44 AM. Reason: clarification - Credit to Pete; Added Target Test
    Blessings in abundance,
    All the Best,
    & ENJOY!

    Art . . . . Carlisle, PA . . USA

  9. #9

    Thread Starter
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: Excel Formula Question

    Quote Originally Posted by Ecniv
    Code:
    =IF(A2=1,B2+20.5,IF(A2=2,B2+9.7,IF(A2=3,B2+4.3,-6.5)))
    I did it like this in another cell as you would need a starting number to begin with (to change).

    Thanks, I didn't know you could do nesting in a formula. I put the following in column B (row 18). I changed the -6.5 part and it works.

    =IF(A18=1,B17+20.5,IF(A18=2,B17+9.7,IF(A18=3,B17+4.3,B17-6.5)))

  10. #10
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Excel Formula Question

    You guys are up early.

    I didnt post any code last night because of the reasons posted. If you dont want the values to keep incrementing everytime any value is
    changed then a second column will be needed.

    Kind of like a 'Rate' range or sheet. Then in your formula for column 2 or "B" you can do something like this in the Formula bar...

    VB Code:
    1. Column A:
    2. 1,2,3 or ?
    3.  
    4. Column B:
    5. Your numeric value
    6. 50 for ex.
    7.  
    8. Column C Formula:
    9. =IF(A1=1,20.5,IF(A1=2,9.7,IF(A1=3,4.3,-6.5)))
    10.  
    11. Column D Formula:
    12. =SUM(B3+C3)
    13.  
    14. Column A | Column B | Column C | Column D
    15.     3         50        4.3        54.3
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  11. #11
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Excel Formula Question

    the routine i posted is in the code window for sheet1, in the worksheet change event, no other handlers or boolean were needed, for it to run each time a cell in column A was changed, any value in column B was incremented appropriately,
    which was what was originally asked for, going back to a cell in column A would only increment it again if it was edited

    but i understand now that it was the total value of column b you wanted to increment

    pete

  12. #12
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Excel Formula Question

    Yes I know it wasnt requested but I was reading between the lines.

    Logically there would not be too much use for a condition where a value changes each time the rate value is changed. What if you made
    a mistake in entering in a rate? Entered 1 and the value, say 50, was incremented by 20.50 to equal 70.50, but then you correct it to 2
    and the value is incremented again to 80.20. Now the correct result without the mistake should be 59.70.

    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  13. #13

    Thread Starter
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: Excel Formula Question

    Quote Originally Posted by westconn1
    ...but i understand now that it was the total value of column b you wanted to increment

    pete
    That's correct. Sorry I wasn't clear.

  14. #14

    Thread Starter
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: Excel Formula Question

    Quote Originally Posted by RobDog888
    Yes I know it wasnt requested but I was reading between the lines.

    Logically there would not be too much use for a condition where a value changes each time the rate value is changed. What if you made
    a mistake in entering in a rate? Entered 1 and the value, say 50, was incremented by 20.50 to equal 70.50, but then you correct it to 2
    and the value is incremented again to 80.20. Now the correct result without the mistake should be 59.70.

    This is a spreadsheet that I use to track my online poker winnings from little (one-table) tournaments. The cost to enter is $6.50 and the prizes are: 1st = $27, 2nd = $16.20 and 3rd = $10.80. So each time I create a new row with the place I finished in column A, column B is incremented (usually ).

    As I indicated I have what I need.

  15. #15
    Frenzied Member
    Join Date
    May 2004
    Location
    Carlisle, PA
    Posts
    1,045

    Re: Excel Formula Question

    Pete ... Your lead was GREAT! I just added the boolean as a tool to play with in situations like RobDog and I pointed out ... the "Oops!" case ... where you might want to go back and change a previously set value in Column A. It was just a quick exploration.
    Blessings in abundance,
    All the Best,
    & ENJOY!

    Art . . . . Carlisle, PA . . USA

  16. #16
    Frenzied Member
    Join Date
    May 2004
    Location
    Carlisle, PA
    Posts
    1,045

    Re: Excel Formula Question

    Martin:

    Another way to do this is to use the VLOOKUP function. Just make a table:

    1 20.50
    2 9.70
    3 4.30
    4 -6.50

    If the range of this array is say D1:E4 then in cell B1:

    =VLOOKUP(A1,$D$1:$E$4,2,TRUE)

    Now it is trivial to change the rewards table. Just a thought.
    Blessings in abundance,
    All the Best,
    & ENJOY!

    Art . . . . Carlisle, PA . . USA

  17. #17
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Excel Formula Question

    Art, the VLOOKUP using the additional range is the same logic as I suggested in adding a colum for the 'Rate'. Either
    one will work well.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  18. #18
    New Member
    Join Date
    Jun 2005
    Posts
    7

    Re: Excel Formula Question

    Use hidden columns and base your formulas on those hidden amounts. Or use the "scenario" function of Excel

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width