+ Reply to Thread
Results 1 to 4 of 4

Excel 2007 : Can I set up a cell to subtract other cells if the value in that cell is over 0

Hybrid View

  1. #1
    Registered User
    Join Date
    08-12-2011
    Location
    Halifax, Canada
    MS-Off Ver
    Excel 2007
    Posts
    2

    Can I set up a cell to subtract other cells if the value in that cell is over 0

    I have a situation where I need to be able to enter a number in a cell and have it subtract other cells automatically, but only if that number is greater than zero. I assume I'll need to set up some vb script to make this work the way I want, because any time I enter a number into a cell, the formula in that cell is deleted.

    Any idea what I can do? Any help is appreciated.

    Shaun

  2. #2
    Forum Expert Bob Phillips's Avatar
    Join Date
    09-03-2005
    Location
    Wessex
    MS-Off Ver
    Office 2003, 2010, 2013, 2016, 365
    Posts
    3,284

    Re: Can I set up a cell to subtract other cells if the value in that cell is over 0

    Option Explicit
    
    Private Sub Worksheet_Change(ByVal Target As Range)
    Const WS_RANGE As String = "H1"      '<<<< change to suit
    
        On Error GoTo ws_exit
        
        Application.EnableEvents = False
        
        If Not Intersect(Target, Me.Range(WS_RANGE)) Is Nothing Then
        
            With Target
            
                If .Value > 0 Then
                
                    Me.Range("A1").Value = Me.Range("A1").Value - .Value
                    Me.Range("B1").Value = Me.Range("B1").Value - .Value
                    Me.Range("C1").Value = Me.Range("C1").Value - .Value
                    Me.Range("N1").Value = Me.Range("N1").Value - .Value
                    'etc.
            End With
        End If
    
    ws_exit:
        Application.EnableEvents = True
    End Sub
    This is worksheet event code, which means that it needs to be
    placed in the appropriate worksheet code module, not a standard
    code module. To do this, right-click on the sheet tab, select
    the View Code option from the menu, and paste the code in.

  3. #3
    Registered User
    Join Date
    08-12-2011
    Location
    Halifax, Canada
    MS-Off Ver
    Excel 2007
    Posts
    2

    Re: Can I set up a cell to subtract other cells if the value in that cell is over 0

    Thanks for that Bob. Just one question though (still getting the hang of vb). What exactly does the WS_RANGE refer to?

    Shaun

    edit: for example - I need to be able to enter a value in BB5 and have it automatically subtract AY5 and AV5
    edit2: I also need to be able to apply this same code to multiple other cells in the same column
    edit3: I think I got it. Thanks again!
    Last edited by shaunn1812; 08-12-2011 at 11:15 AM.

  4. #4
    Forum Expert Bob Phillips's Avatar
    Join Date
    09-03-2005
    Location
    Wessex
    MS-Off Ver
    Office 2003, 2010, 2013, 2016, 365
    Posts
    3,284

    Re: Can I set up a cell to subtract other cells if the value in that cell is over 0

    [quote=shaunn1812;2578948]Thanks for that Bob. Just one question though (still getting the hang of vb). What exactly does the WS_RANGE refer to?/quote]

    WS_RANGE is just a string constant I use to define the range of cells to be checked. Just change it to whatever cells apply for you.

+ 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