+ Reply to Thread
Results 1 to 4 of 4

How to create a two way formula

Hybrid View

nleahcim How to create a two way... 01-07-2010, 11:20 AM
StephenR Re: How to create a two way... 01-07-2010, 11:32 AM
nleahcim Re: How to create a two way... 01-07-2010, 11:52 AM
StephenR Re: How to create a two way... 01-07-2010, 12:04 PM
  1. #1
    Registered User
    Join Date
    01-07-2010
    Location
    Boston, MA
    MS-Off Ver
    Excel 2007
    Posts
    25

    How to create a two way formula

    I want to be able to create formulas where the output can act as the input. For example, I tried to write a simple proof of concept script where you can convert between pounds and kilograms. So if you type in a value for pounds, it'll update the kilograms cell. And if you type in a value for kilograms - it'll update the pounds cell.

    This is what I came up with:

    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
        Static PreviousCell As Range
        
        If Not (PreviousCell Is Nothing) Then
            If PreviousCell = Range("H4") Then
                Range("I4").Value = Range("H4").Value * 2.20462262
            ElseIf PreviousCell = Range("I4") Then
                Range("H4").Value = Range("I4").Value / 2.20462262
            End If
        End If
        Set PreviousCell = Target
    End Sub
    Problem is that it doesn't fully work. It works perfectly as long as you don't enter in a value for pounds, then try to enter in the same value for kilograms. I can't figure out why though!

    Also - I'm very new to writing code for Excel (more of a C programmer myself...) - so if there are better ways of doing any of this I'd be all ears!

    Thanks!
    Last edited by nleahcim; 01-07-2010 at 11:53 AM.

  2. #2
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606

    Re: How to create a two way formula

    Does this work for you? The ratios may be the wrong way round.
    Private Sub Worksheet_Change(ByVal Target As Range)
    
    Application.EnableEvents = False
    If Target.Address = "$H$4" Then Target.Offset(, 1).Value = Target * 2.20462262
    If Target.Address = "$I$4" Then Target.Offset(, -1).Value = Target / 2.20462262
    Application.EnableEvents = True
    
    End Sub
    Last edited by StephenR; 01-07-2010 at 11:35 AM.

  3. #3
    Registered User
    Join Date
    01-07-2010
    Location
    Boston, MA
    MS-Off Ver
    Excel 2007
    Posts
    25

    Re: How to create a two way formula

    Quote Originally Posted by StephenR View Post
    Does this work for you? The ratios may be the wrong way round.
    Private Sub Worksheet_Change(ByVal Target As Range)
    
    Application.EnableEvents = False
    If Target.Address = "$H$4" Then Target.Offset(, 1).Value = Target * 2.20462262
    If Target.Address = "$I$4" Then Target.Offset(, -1).Value = Target / 2.20462262
    Application.EnableEvents = True
    
    End Sub
    Very cool! That works great! I had actually tried something similar to that - but it'd register a couple hundred changes before it would stop executing code. I didn't know about the EnableEvents value! That's just what I needed!

    By the way - why don't you need an end if statement? Are those only necessary when you break up the if/then statements on separate lines?

    Thanks so much!

  4. #4
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606

    Re: How to create a two way formula

    My pleasure.
    By the way - why don't you need an end if statement? Are those only necessary when you break up the if/then statements on separate lines?
    Yes, if you have only one statement after the Then you can put it on the same line and dispense with the End If.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1