+ Reply to Thread
Results 1 to 9 of 9

Dynamic cells

Hybrid View

  1. #1
    Registered User
    Join Date
    06-13-2012
    Location
    Earth, Gaia
    MS-Off Ver
    Excel 2003
    Posts
    7

    Dynamic cells

    Hi

    I am trying to dynamically link three cells (A1, A2, A3), where the third cell (A3)is the sum of cells(A1, A2) - totalling 100%. I need a formula which will automatically adjust A1 if A2 and vice versa and keep A3 fixed to 100%.

    I am new to writing formulas in Excel and any help with this will be greatly appreciated. I've been going round and round this with no progress.

  2. #2
    Forum Guru
    Join Date
    08-05-2004
    Location
    NJ
    MS-Off Ver
    365
    Posts
    13,582

    Re: Dynamic cells

    You will need to use VBA to achieve this if what you are asking is....
    When you enter a value in A1, A2 adjusts so that A3 (the sum of A1 and A2) remains the same AND
    when you enter a value in A2, A1 adjusts so that A3 remains the same.
    Formula's reside in cells so when you manually enter a value, you automatically remove the formula.
    ChemistB
    My 2?

    substitute commas with semi-colons if your region settings requires
    Don't forget to mark threads as "Solved" (Edit First post>Advanced>Change Prefix)
    If I helped, Don't forget to add to my reputation (click on the little star at bottom of this post)

    Forum Rules: How to use code tags, mark a thread solved, and keep yourself out of trouble

  3. #3
    Registered User
    Join Date
    06-13-2012
    Location
    Earth, Gaia
    MS-Off Ver
    Excel 2003
    Posts
    7

    Re: Dynamic cells

    Thanks ChemistB, that's what I mean. There is no way to do this without VBA? I don't know where to begin with that.

  4. #4
    Valued Forum Contributor
    Join Date
    05-07-2012
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    354

    Re: Dynamic cells

    You can either have user entered values or formulas in a cell but not both (unless you use VBA)

    To use equations:

    You could have user entries in A1 and A2 and calculations in B1 and B2

    B1 = if(trim(len(A1)) = 0, 1- A2, "")
    B2 = if(trim(len(A2)) = 0, 1- A1, "")

    Edit: Too slow in typing...ChemistB is faster than this Chemical Engineer
    Regards,
    Vandan

  5. #5
    Valued Forum Contributor
    Join Date
    05-07-2012
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    354

    Re: Dynamic cells

    with VBA:

    Use worksheet_change event.

    Press Alt + F11 to go to VBA editor, select your worksheet in the project browser on the left and paste following code

    Private Sub Worksheet_Change(ByVal Target As Range)
    
    On Error GoTo ErrorHandler
    Application.EnableEvents = False
    If Target.Address = "$A$1" Then Range("A2").Value = 1 - Target.Value
    If Target.Address = "$A$2" Then Range("A1").Value = 1 - Target.Value
    
    ErrorHandler:
    Application.EnableEvents = True
        
    
    End Sub

  6. #6
    Registered User
    Join Date
    06-13-2012
    Location
    Earth, Gaia
    MS-Off Ver
    Excel 2003
    Posts
    7

    Re: Dynamic cells

    Thanks Vandan. I have tried your equation but it doesn't work for me.

    I have just read about the Trim () and it says it's used for removing extra spaces? How does it work in this example please?

  7. #7
    Forum Guru
    Join Date
    08-05-2004
    Location
    NJ
    MS-Off Ver
    365
    Posts
    13,582

    Re: Dynamic cells

    To use VBA
    Right click on the worksheet tab and "View Code"
    Copy and paste this code into the now open VBA editor
    Private Sub worksheet_Change(ByVal Target As Range)
    
        If Target.Count = 1 And Target.Address = "$A$1" Then
            Application.EnableEvents = False
            Cells(2, 1).Value = Cells(3, 1).Value - Cells(1, 1).Value  'Cells(row,column) thus Cells(2,1) = A2
        End If
        
        If Target.Count = 1 And Target.Address = "$A$2" Then
            Application.EnableEvents = False
            Cells(1, 1).Value = Cells(3, 1).Value - Cells(2, 1).Value
        End If
        
        Application.EnableEvents = True
        
    End Sub
    Close the VBA editor (no need to save). Code is set for A1, A2 and A3 as per your example

  8. #8
    Valued Forum Contributor
    Join Date
    05-07-2012
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    354

    Re: Dynamic cells

    Book1.xls

    sorry I had trim and len swapped...that is why it didn't work


    see attached with VBA and non-vba solution

  9. #9
    Registered User
    Join Date
    06-13-2012
    Location
    Earth, Gaia
    MS-Off Ver
    Excel 2003
    Posts
    7

    Re: Dynamic cells

    Thank you both! They all work brilliantly!

+ 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