Results 1 to 7 of 7

Thread: Excel - Change cell formula from nested IFs to ??

  1. #1

    Thread Starter
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091

    Resolved Excel - Change cell formula from nested IFs to ??

    So I have this ugly formula on a bunch of rows that looks like...
    Code:
    = (H23*IF(D23="YT",0.39,IF(D23="M/C",0.4,IF(D23="KK",0.45,IF(D23="M",0.71,0)))))
    and I want to use VBA and change it over to a Select Case type of thing. I've never really worked with VBA before. What event(s) would I used, and so forth? Who wants to help me out? I'll send you five teeth for your trouble.

    Last edited by crptcblade; Apr 22nd, 2005 at 07:24 AM.
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  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 - Change cell formula from nested IFs to ??

    I need more info on what type of event you need to call this from. so I just made it a public function that can be
    called from any sheet or event. Place it in a Module and we shall see where it needs to be called from best.

    VB Code:
    1. Public Function crptcblade() As Double
    2.     Dim dblValue As Double
    3.     Select Case D23
    4.         Case "YT"
    5.             dblValue = 0.39
    6.         Case "M/C"
    7.             dblValue = 0.4
    8.         Case "KK"
    9.             dblValue = 0.45
    10.         Case "M"
    11.             dblValue = 0.71
    12.         Case Else
    13.             dblValue = 0
    14.     End Select
    15.     'H23 equals - Workbooks("Book1").Sheets("Sheet1").Cells(23, 8).Value
    16.     crptcblade = Workbooks("Book1").Sheets("Sheet1").Cells(23, 8).Value * dblValue
    17. End Function
    When can I expect my 5 teeth? Are they molars or fangs?

    Edit: Dont you mean 7 teeth?
    Last edited by RobDog888; Apr 21st, 2005 at 11:40 PM.
    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

    Thread Starter
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091

    Re: Excel - Change cell formula from nested IFs to ??

    Quote Originally Posted by RobDog888
    I need more info on what type of event you need to call this from.
    It would need to be a replacement for a formula in a range of cells. Basically, each cell in column K has that formula in it, but new values will be getting added regularly, and I don't want to have to maintain that big IF statement.

    The only event I found was Sheet_Calculate, but I don't know how to figure out which cell has changed in order to update it.


    As for the teeth, please transfer $458 for s/h to my Paypal and they are as good as yours.
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  4. #4

    Thread Starter
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091

    Re: Excel - Change cell formula from nested IFs to ??

    Resolved by me. I figured out how to link a function in a module to the formula.

    So I'll keep the teeth.

    :evil:
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  5. #5
    Addicted Member
    Join Date
    Jan 2002
    Location
    Glasgow, Scotland
    Posts
    202

    Re: Excel - Change cell formula from nested IFs to ??

    hi,

    can you post how you achieved the resolution?

    I would be interested to know.

    Thnx
    if you fail to plan, you plan to fail

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

    Re: Excel - Change cell formula from nested IFs to ??

    Quote Originally Posted by crptcblade
    Resolved by me. I figured out how to link a function in a module to the formula.

    So I'll keep the teeth.

    :evil:
    Geez, you welch on the teeth and you dont want to share the solution. Boooo *SLAP*

    Should I give you -Teeth then
    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

  7. #7

    Thread Starter
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091

    Re: Excel - Change cell formula from nested IFs to ??

    Quote Originally Posted by Br1an_g
    hi,

    can you post how you achieved the resolution?

    I would be interested to know.

    Thnx
    It actually turned out to be pretty easy. Add a module, and make a public function.
    Code:
     Public Function ReturnSomeValue(CellValue) As Double
       ReturnSomeValue = Val(CellValue) * 2.24
     End Function
    Then make the formula in the cell "=ReturnSomeValue(H4)" and it should work fine. I guess it gets a little hinky passing ranges of cells in, I only needed to worry about one cell at a time.
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

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